I have a script which runs another script via SSH on a remote server using sudo. However, when I type the password, it shows up on the terminal. (Otherwise it works fine)

ssh user@server "sudo script"

What's the proper way to do this so I can type the password for sudo over SSH without the password appearing as I type?

share|improve this question
1  
as for me, the reason to look for a way of sudoing through ssh was that it wasn't working when trying something like ssh <user@server> sudo <script>, as I was getting the error sudo: no tty present and no askpass program specified – knocte Jul 11 '16 at 8:58
up vote 137 down vote accepted

Another way is to use the -t switch to ssh:

ssh -t user@server "sudo script"

See http://linuxcommand.org/man_pages/ssh1.html:

 -t      Force pseudo-tty allocation.  This can be used to execute arbi-
         trary screen-based programs on a remote machine, which can be
         very useful, e.g., when implementing menu services.  Multiple -t
         options force tty allocation, even if ssh has no local tty.
share|improve this answer
2  
Nice, I knew of the -t option, just didn't know it worked for sudo prompts. – user507484 Oct 1 '13 at 15:28
3  
what is the -t option for? – Vince Aug 22 '14 at 10:22
    
@Vince see go2linux.garron.me/linux/2010/11/… – givemesnacks Aug 27 '14 at 19:28
2  
It's not working here: ssh -t localhost <<< "sudo touch file;" EDIT Apparently it's important that you actually provide the command as a parameter, not through standard in (which makes sense in hindsight). – Limited Atonement Aug 10 '15 at 15:10
    
the -t method will also show colored output of commands which normally do so. – karmakaze Jun 3 '16 at 18:33

NOPASS in the configuration on your target machine is the solution. Continue reading at http://maestric.com/doc/unix/ubuntu_sudo_without_password

share|improve this answer
4  
In this case I actually want a password, and to type it directly, so this doesn't work for me. – darkfeline Apr 25 '12 at 15:59

The best way is ssh -t user@server "sudo <scriptname>", for example ssh -t user@server "sudo reboot". It will prompt for password for user first and then root(since we are running the script or command with root privilege.

I hope it helped and cleared your doubt.

share|improve this answer

Depending on your usage, I had success with the following:

ssh root@server "script"

This will prompt for the root password and then execute the command correctly.

share|improve this answer
18  
Yikes, SSH as root? That's a bad idea for all sorts of reasons. – darkfeline Aug 16 '13 at 9:09
11  
Remember ssh is not telnet. It isn't any more dangerous to ssh as root than it would be to ssh as another user and run sudo. Your password is encrypted the same way over the ssh connection. – Stéphane Feb 14 '14 at 1:53
17  
Stéphane is absolutely correct as far as password security goes. However, by using root instead of sudo, you lose the audit trail that goes with sudo. Additionally, root access may not be available. – djeikyb Jul 2 '14 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.