Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm running some non-interactive ssh commands. The ssh authentication is taken care of fine through the ssh agent, but if I run a command that requires sudo then the password prompt in my terminal is plain text. For example:

ssh remotemachine "sudo -u www mkdir -p /path/to/new/folder"

will prompt me for the password in plain text. Does anyone know how I can get it to use the normal secure prompt or that I can pass the password via a switch? (as then I can set up a secure prompt on this side before I send the command)

Any help is much appreciated.

share|improve this question
up vote 11 down vote accepted

Use ssh -t:

man ssh

-t   Force pseudo-tty allocation. This can be used to execute arbitrary 
     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.

So your command will be

ssh remotemachine -t "sudo -u www mkdir -p /path/to/new/folder"

If you don't want to enter password, you can (if you are allowed to) modify sudoers using command visudo.

Add parameter NOPASSWD:, for example

username ALL=(ALL) NOPASSWD: /bin/mkdir

If you can't edit /etc/sudoers, you can use sudo -S:

man sudo

-S      The -S (stdin) option causes sudo to read the password from
        the standard input instead of the terminal device.  The
        password must be followed by a newline character.

With that, command would be

echo "your_password" | ssh remotemachine -t \
     "sudo -S -u www mkdir -p /path/to/new/folder"

Remember that this will add your password to command history of your shell (with bash, that would be ~/.bash_history file).

share|improve this answer
thanks, that worked, really appreciate it. – Iain Mar 13 '11 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .