How to execute sudo command in remote host via SSH

Posted mouseleo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How to execute sudo command in remote host via SSH相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

How to Run Sudo Command Without Password

How to Execute Page_Load() in Page's Base Class?

How to keep Environment Variables when Using SUDO

sudo: unable to execute ./xxx.py: no such file or directory

How to fix 'sudo: no tty present and no askpass program'以及硬盘序列号的读写

java 来自http://www.mkyong.com/java/how-to-execute-shell-command-from-java/