Technology

How to run a command that requires sudo through SSH

How to run a command that requires sudo through SSH

Secure Shell consists of many methods, lots of which might make your administrator’s life exponentially simpler. One such trick is the flexibility to run instructions on distant servers, with out logging in.

Sure, you may take the time to log into the server, run the command, and sign off, however why not do it multi function fell swoop? Not solely is it sensible, it is also fairly easy.

SEE: Top instructions Linux directors must know (TechRepublic Premium)

What do you want

The solely factor you want are two extra Linux machines, together with the openssh server up and working (and accepting connections). You can do that from the usual repositories if you do not have the SSH daemon put in. For instance, on the Ubuntu Server platform, the command to put in the SSH daemon is:

sudo apt-get set up openssh-server -y

Once put in, we advocate enabling the server with the instructions:

sudo systemctl begin sshd
sudo systemctl allow sshd

Note that on Ubuntu programs the service for the OpenSSH server is named ssh, not sshd. Therefore, the instructions to start out and allow the SSH server could be:

sudo systemctl begin ssh
sudo systemctl allow ssh

Now that you’ve the SSH daemon working in your distant servers, you may ship instructions to them. Let’s learn the way.

SEE: How to view SSH keys in Linux, macOS, and Windows (TechRepublic)

Running a fundamental command

Let’s take an inventory of recordsdata on a distant /and so forth listing. To do that the command is:

ssh USER@SERVER_IP "ls /and so forth"

Where USER is a distant username and SERVER_IP is the IP deal with of the distant server. Once you efficiently enter the distant consumer’s password, you’re going to get an inventory of the /and so forth/ listing on the distant server.

Easy Come Easy Go.

SEE: How to shortly give customers sudo privileges in Linux (TechRepublic)

Running a command that requires sudo

But what if it is advisable run a command that requires sudo privileges on a distant server? If you do that, you will notice a tty error.

Here’s a tty error. Image: Jack Wallen

How to get round the issue? Luckily, there’s just a little swap you may add to the command. Said swap is -t. What does not he do? It forces pseudo-terminal allocation, so ssh has no concept that it does not have an area terminal to make use of.

So, to run a distant command, through ssh, which requires sudo privileges, the ssh command appears to be like like:

ssh -t USER@SERVER_IP "sudo COMMAND"

Suppose, for instance, you need consumer jack to replace a distant server at 192.168.1.201. This command is:

ssh -t jack@192.168.1.201 "sudo apt-get improve -y"

You will first be prompted for the consumer’s password for the SSH connection, adopted by a second immediate for the consumer’s password for sudo privileges.

sshremoteb
A immediate for the consumer’s password for sudo privileges. Image: Jack Wallen

The command will run as if it have been run on the native laptop (solely it’s working on the distant laptop). Once the command completes, you may be returned to the native immediate, able to proceed working.

And that is every little thing it is advisable learn about working instructions that require sudo privileges on a distant machine, through SSH.

This article was initially revealed in July 2019. It was up to date by Antony Peyton in January 2025.

Source Link

Shares:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *