ssh has a cure for everything. The point is to add the
-t flag to force ssh to allocate a pseudo-tty:
ssh -t remote-server 'sudo ls'
This method will run a single script using sudo after ssh:
Let’s assume we have a “remote” user with sudo capabilities and we have a script we want it to execute as root.
1) Set a script in /etc/passwd to be used on login:
remote:x:1100:1100:Some Remote User,,,:/home/remote:/home/remote/login.sh
2) Inside “login.sh”, we execute the script we want to run as “sudo”:
#!/bin/bash if [ "$(id -u)" != "0" ]; then #Not running as root, so we execute "sudo" sudo /usr/local/bin/script.sh else #We are already rooted, so "sudo" is not required. /usr/local/bin/script.sh fi exit;
Normally it will ask the password twice: ssh login + sudo. If you want to input it only once, try sudo without password (not recommended). <– please read Boldewyn comment.
The main advantage is that “ssh” does not require any other parameter (as -t), and sudo is forced on server side. Also, once the script exits, the user is logged out as well.
It may not be that elegant, but its simple and works.
You can run following command get root access without interactivity:
ssh server " sudo command" < sudopassword.txt