1. 程式人生 > >How to execute sudo command in remote host via SSH

How to execute sudo command in remote host via SSH

sed exec rac base should -s mach back sage

Question:

I have an interactive shell script, that at one place needs to ssh to another machine (Ubuntu based) and execute something as root (the user should enter his password, but the remote command should run like noted in the script):

# ...
ssh remote-machine ‘sudo ls‘
# ...

However, I always get this error message back:

sudo: no tty present and no askpass program specified

OK, that‘s quite clear. But how can I circumvent this? Something like this should happen:

$ ssh remote-machine ‘sudo ls /‘
[sudo] password for user1:

/bin
/etc
/var

Answer:

The wondrous 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‘
 

How to execute sudo command in remote host via SSH