129

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?

  • 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
216

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

ssh -t user@server "sudo script"

See man ssh:

 -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.
  • 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
  • 3
    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
10

I was able to fully automate it with the following command:

echo pass | ssh -tt user@server "sudo script"

Advantages:

  • no password prompt
  • won't show password in remote machine bash history

Regarding security: as Kurt said, running this command will show your password on your local bash history, and it's better to save the password in a different file or save the all command in a .sh file and execute it. NOTE: The file need to have the correct permissions so that only the allowed users can access it.

  • 6
    It will show up on the local system's bash history, including the password. Better to store the password in a file and cat the file. – Kurt Fitzner Nov 19 '18 at 0:44
  • 2
    Man, I knew about -t, but I hadn't read the manual carefully enough to see that you could pass it twice! This is what I've been looking for for months! As a security addition, I simply set a password variable before running with read -s -p "Password: " pw, then did echo "$pw" | ..... I've now rolled this into a handy script for myself :). – kael Jan 31 at 22:00
4

Assuming you want no password prompt:

ssh $HOST 'echo $PASSWORD | sudo -S $COMMMAND'

Example

ssh me@localhost 'echo secret | sudo -S echo hi' # outputs 'hi'
  • 7
    This is /very/ dangerous since the plaintext password ends up on the command line. Command lines are public and can be seen by every other user and process on the system. For example, "ps auxwwwww" by an unprivileged user will show every process running and the command lines that ran them. – Kurt Fitzner Nov 19 '18 at 0:35
  • 2
    Not to mention putting your password (in plaintext) into the bash_history file. – Layne Bernardo Feb 22 at 2:18
2

Sudo over SSH passing a password, no tty required:

You can use sudo over ssh without forcing ssh to have a pseudo-tty (without the use of the ssh "-t" switch) by telling sudo not to require an interactive password and to just grab the password off stdin. You do this by using the "-S" switch on sudo. This makes sudo listen for the password on stdin, and stop listening when it sees a newline.

Example 1 - Simple Remote Command

In this example, we send a simple whoami command:

$ ssh user@server cat \| sudo --prompt="" -S -- whoami << EOF
> <remote_sudo_password>
root

We're telling sudo not to issue a prompt, and to take its input from stdin. This makes the sudo password passing completely silent so the only response you get back is the output from whoami.

This technique has the benefit of allowing you to run programs through sudo over ssh that themselves require stdin input. This is because sudo is consuming the password over the first line of stdin, then letting whatever program it runs continue to grab stdin.

Example 2 - Remote Command That Requires Its Own stdin

In the following example, the remote command "cat" is executed through sudo, and we are providing some extra lines through stdin for the remote cat to display.

$ ssh user@server cat \| sudo --prompt="" -S -- "cat" << EOF
> <remote_sudo_password>
> Extra line1
> Extra line2
> EOF
Extra line1
Extra line2

The output demonstrates that the <remote_sudo_password> line is being consumed by sudo, and that the remotely executed cat is then displaying the extra lines.

An example of where this would be beneficial is if you want to use ssh to pass a password to a privileged command without using the command line. Say, if you want to mount a remote encrypted container over ssh.

Example 3 - Mounting a Remote VeraCrypt Container

In this example script, we are remotely mounting a VeraCrypt container through sudo without any extra prompting text:

#!/bin/sh
ssh user@server cat \| sudo --prompt="" -S -- "veracrypt --non-interactive --stdin --keyfiles=/path/to/test.key /path/to/test.img /mnt/mountpoint" << EOF
SudoPassword
VeraCryptContainerPassword
EOF

It should be noted that in all the command-line examples above (everything except the script) the << EOF construct on the command line will cause the everything typed, including the password, to be recorded in the local machine's .bash_history. It is therefore highly recommended that for real-world use you either use do it entirely through a script, like the veracrypt example above, or, if on the command line then put the password in a file and redirect that file through ssh.

Example 1a - Example 1 Without Local Command-Line Password

The first example would thus become:

$ cat text_file_with_sudo_password | ssh user@server cat \| sudo --prompt="" -S -- whoami
root

Example 2a - Example 2 Without Local Command-Line Password

and the second example would become:

$ cat text_file_with_sudo_password - << EOF | ssh va1der.net cat \| sudo --prompt="" -S -- cat
> Extra line1
> Extra line2
> EOF
Extra line1
Extra line2

Putting the password in a separate file is unnecessary if you are putting the whole thing in a script, since the contents of scripts do not end up in your history. It still may be useful, though, in case you want to allow users who should not see the password to execute the script.

  • In the ssh remote command, why do you need to execute cat and pipe the results into sudo? Can you just use sudo as the main ssh remote command? – joanpau May 21 at 20:31
  • @joanpau The initial cat is used to pipe the user's password through to sudo on the other side. If the user can run sudo without a password, then it's not required, but I don't suggest this configuration. The reason it is piped is to prevent the password from showing up on the remote sysrtem's command line. Command lines are not secure, any user can see every command line with ps auxwww. – Kurt Fitzner May 23 at 1:53
  • I am asking for the cat in the ssh command cat \| sudo --prompt="" -S.... If the -S forces sudo to read the password from stdin, are the cat and pipe necessary at all? Could the ssh command simply be sudo --prompt="" -S...? – joanpau Jun 4 at 18:00
  • @jonpau That cat command is taking stdin and piping it through sudo in order to pass it the sudo password. It's showing how you can pipe the sudo password through ssh via stdin safely. – Kurt Fitzner Jun 5 at 22:21
0

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

  • 6
    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
0

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.

-1

I faced a problem,

user1@server1$ ssh -q user1@server2 sudo -u user2 rm -f /some/file/location.txt

Output:
sudo: no tty present and no askpass program specified

Then I tried with

#1
vim /etc/sudoers
Defaults:user1    !requiretty

didn't work

#2
user1   ALL=(user2)         NOPASSWD: ALL

that worked properly!

  • This doesn't really accomplish what is being asked. The idea is to still require the password, but allow it to be typed over ssh. What you've done is just given user1 full sudo access to user2 without a password. This is fine for specific applications, but not the best idea as a general solution. – Possum Apr 17 at 21:50
-5

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.

  • 22
    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
  • 20
    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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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