I am trying to run a script on remote server as a root user. I have this line in my shell script.
cat /home/myuser/create_user_updated.sh | ssh myuser@$myip "sudo sh"
I get the following error.
sudo: sorry, you must have a tty to run sudo
The script "create_user_updated.sh" works ok if copy the file to remote server and execute it there locally. But it does not work from remote server.
You have two options:
You can disable tty requirements by sudo: run
visudo
and either remove "requiretty" option globally if it is set, or on per-user basis:Defaults:username !requiretty
You can force ssh to allocate pseudo-tty:
ssh -t -t myuser@$myip "sudo sh"
Note the double -t options, both are needed. Moreover, you need to add
exit
as a last line of yourcreate_user_updated.sh
script. Otherwise, ssh session might not end properly (because of the forced pseudo-tty creation).Please use option #2 only if you don't have root rights on the target machine, and cannot change sudo config. #1 is much more clean solution. In my opinion, enforcing tty via sudo does very little from security standpoint.
Use
visudo
to edit the/etc/sudoers
file and insert the below line: