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.

link|improve this question
feedback

1 Answer

up vote 5 down vote accepted

Use -t:

 -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.

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 sudoers, you can use -S:

   -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.

So it will be

echo "your_password" | ssh remotemachine -t "sudo -S -u www mkdir -p /path/to/new/folder"
link|improve this answer
thanks, that worked, really appreciate it. – Iain Mar 13 '11 at 12:33
feedback

Your Answer

 
or
required, but never shown

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