How to Run a Command That Requires sudo via SSH

How to Run a Command That Requires Sudo Via Ssh

How to Run a Command That Requires sudo via SSH

Home » News » How to Run a Command That Requires sudo via SSH
Table of Contents

Safe Shell consists of plenty of tips, lots of which might make your admin’s life exponentially simpler. One such trick is the flexibility to run instructions on distant servers, with out logging in.

Certain, you’ll be able to take the time to log into the server, run the command, and log off, however why not simply do it multi functional fell swoop? Not solely is that this helpful, it’s fairly simple.

SEE: High Instructions Linux Admins Must Know (roosho Premium)

What you want

The one factor you want for that is two extra Linux machines, together with the openssh-server up and operating (and accepting connections). You are able to do this from the usual repositories when you don’t have the SSH daemon put in. As an example, on the Ubuntu Server platform, the command to put in the SSH daemon is:

sudo apt-get set up openssh-server -y

As soon as put in, you’ll need to allow the server with the instructions:

sudo systemctl begin sshd
sudo systemctl allow sshd

Be aware that on Ubuntu programs, the service for the OpenSSH server is known as ssh, not sshd. Due to this fact, the instructions to start out and allow the SSH server can be:

sudo systemctl begin ssh
sudo systemctl allow ssh

Now that you’ve got the SSH daemon operating in your distant servers, you’ll be able to ship instructions to them. Let’s learn how.

SEE: How you can View Your SSH Keys in Linux, macOS, and Home windows (roosho)

Working a fundamental command

Let’s get 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"

The place USER is a distant username, and SERVER_IP is the IP deal with of the distant server. When 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.

Straightforward-peasy.

SEE: How you can Shortly Give Customers sudo Privileges in Linux (roosho)

Working a command that requires sudo

However what if you’ll want to run a command that requires sudo privileges on a distant server? In the event you try this, you’ll see a tty error.

heres a tty error picture jack wallen

How do you get round that? Happily, there’s somewhat change you’ll be able to add to the command. Stated change is -t. What does -t do? It forces pseudo-terminal allocation, so ssh has no concept it doesn’t have an area terminal to make use of.

So, to run a distant command, by way of ssh, that requires sudo privileges, the ssh command seems like:

ssh -t USER@SERVER_IP "sudo COMMAND"

Say, as an example, you need the consumer jack to improve a distant server at 192.168.1.201. This command is:

ssh -t [email protected] "sudo apt-get improve -y"

You’ll first be requested for the consumer’s password for the SSH connection, adopted by a second request for the consumer’s password for sudo privileges.

a request for the consumers password for sudo privileges picture jack wallen

The command will run as if it was executed on the native machine (solely it’s operating on the distant machine). When the command completes, you’ll return to the native immediate, able to hold working.

And that’s all there may be to operating instructions that require sudo privileges on a distant machine, by way of SSH.

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

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
share this article.

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name