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)
echo $VAR_REMOTEROOTPASS | ssh -tt -i $PATH_TO_KEY/id_mykey $VAR_REMOTEUSER@$varRemoteHost
echo \"$varCommand\" | sudo bash
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.
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.
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!
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'
NOPASS
in the configuration on your target machine is the solution. Continue reading at http://maestric.com/doc/unix/ubuntu_sudo_without_password