207

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
  • 3
    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, 2016 at 8:58

8 Answers 8

314

Another way is to use the -t open for 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.
7
  • 2
    Nice, I knew of the -t option, just didn't know it worked for sudo prompts.
    – user507484
    Oct 1, 2013 at 15:28
  • 4
    what is the -t option for?
    – Vince
    Aug 22, 2014 at 10:22
  • 4
    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). Aug 10, 2015 at 15:10
  • the -t method will also show colored output of commands which normally do so.
    – karmakaze
    Jun 3, 2016 at 18:33
  • The same thing can be accomplished without the need of the -t option by telling sudo not to require a terminal. In this case, it takes its imput from stdin. See answer below for details and examples. Nov 19, 2018 at 2:59
48

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.

9
  • 8
    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. Nov 19, 2018 at 0:44
  • 6
    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, 2019 at 22:00
  • Worked like charm for me!
    – MiKr13
    Sep 12, 2019 at 11:29
  • 1
    Why don't you just set up passwordless sudo for whatever specific users and commands you need to run? This isn't an SSH problem...
    – nomen
    Feb 8, 2020 at 14:38
  • 5
    Regarding preventing password from appearing in local history, 1) that's not an issue if it is a line in a script, 2) for interactive shell, local history is easily avoided by beginning the command with a SPACE if HISTCONTROL is set to ignorespace or ignoreboth (assuming bash, similar for other shells). A different security concern is that the password might be shown in the process table. echo is usually a shell built-in so it is probably OK as is, but I'd rather use a here-string: <<< pass. Mar 9, 2021 at 19:07
13

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.

8
  • 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, 2019 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. May 23, 2019 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, 2019 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. Jun 5, 2019 at 22:21
  • cat text_file_with_sudo_password | ssh user@server "cat - | sudo --prompt='' -S -- 'whoami' " I needed to use " - " or to leave cat without parameters to receive input from stdin, at least on MacOS. Mar 1, 2021 at 18:25
7

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'
5
  • 23
    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. Nov 19, 2018 at 0:35
  • 6
    Not to mention putting your password (in plaintext) into the bash_history file. Feb 22, 2019 at 2:18
  • 1
    Depending on your shell configuration (default in bash), putting a space before the command skips adding it to history. you could also keep the $PASSWORD and do a space-prefixed ` export PASSWORD=secret` before. Just make sure to exit the session after to not let the env var hang around.
    – Legogris
    Oct 18, 2020 at 4:30
  • Also, you can make bash history to not record any command with a certain string, I have prohibited any export command to be stored to avoid any important token or secret end up there
    – theist
    Nov 10, 2020 at 10:37
  • This is not always dangerous. But thanks for the input.
    – Leathan
    Jun 10 at 7:16
4

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

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

1
  • 8
    In this case I actually want a password, and to type it directly, so this doesn't work for me.
    – darkfeline
    Apr 25, 2012 at 15:59
1

confirming that the answer of @ofirule is working like a charm. I try ot even with sshpass & it works. This is how to use it with sshpass:

echo $pass | sshpass -p $pass ssh -tt cloud_user@$ip "sudo su -"

you will find yourself in the root shell directly

0
echo $VAR_REMOTEROOTPASS | ssh -tt -i $PATH_TO_KEY/id_mykey $VAR_REMOTEUSER@$varRemoteHost 
echo \"$varCommand\" | sudo bash
1
  • 3
    ... and please provide an explanation for your code.
    – pppery
    Sep 14, 2019 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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