Run a command on remote host as root but via normal user

S
sxiii

Imagine you don't have remote root access to a machine that you need to execute root commands on.

How to install sshpass in linux debian 8 - Umang Mittal - Medium

But what you might have is a normal user account that's allowed to execute root commands via sudo, which is the situation quite often nowadays.

So, you need to connect to remote machine as a normal user via SSH, then, "upgrade" your privileges, run a command and quit.

The problem is, neither SSH nor sudo support plain-text password entry (which is a safety feature, of course).

But what if you're using this technique from inside of your local network, in a totally isolated environment? That may make sense.

In this way, you could control, for example, multiple docker machines and ask them to run and stop containers automatically from the scripts without using powerful-but-buggy docker-swarm or k3s or k8s.

I used some time to find how I do this but finally I managed. Check out this:

```

sshpass -p "mypass1" ssh -o StrictHostKeyChecking=no [email protected] -t 'echo mypass2 | sudo -S docker ps'

```

Running this command requires you to have `sshpass` program (but only on YOUR host! so only 1 machine requires this!). So go pull it with `sudo pacman -S sshpass` for Arch/Manjaro or `sudo apt install sshpass` for Debian/Ubuntu or `sudo dnf install sshpass` for Fedora/CentOS.

If you want full explaination on this, you might want to check out my github gist in here:

https://gist.github.com/sxiii/bb48fdc9b6577eba6328f90ffd63a64e

Hope it would be useful for some of you :)

Stay tuned,

@sxiii aka Den from O.

0.000 SRY
Global

Comments