Amazon ec2 says “sudo: sorry, you must have a tty to run sudo”

0

I’ve got a script on an Amazon EC2 64bit Linux which I’d like to remotely trigger using ssh. But when I call this script the following error message appears:

"sudo: sorry, you must have a tty to run sudo"

Here’s a quick and I believe secure way to invoke a remote command via ssh which uses sudo without completely disabling requiretty
Note the following when running visudo

#
# Disable "ssh hostname sudo ", because it will show the password in clear.
#         You have to run "ssh -t hostname sudo ".
#
Defaults    requiretty

Adding -t allows the command to execute without requiring tty, according to the ssh doc -t…

[Forces] pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can bevery useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Here’s a sample SSH command…

# run an arbitrary command
$ ssh -i my.pem -t ec2-user@127.0.0.1 'sudo reboot'

or
# run my script which has some sudo function"
$ ssh -i my.pem -t ec2-user@127.0.0.1 '/path/to/script/doit'

 

"Amazon ec2 says “sudo: sorry, you must have a tty to run sudo”" was published on December 27th, 2011 and is listed in My Work.

Follow comments via the RSS Feed | Leave a comment | Trackback URL