non-interactive ssh sudo... prompts for the password in plain text
Solution 1:
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).