I was recently working on a Perl script that would SSH to another server and run a sudo command on the remote server that was failing. The error that was received is below.
Error: sudo: sorry, you must have a tty to run sudo
The reason for this is an update along the way with sudo locked it down further by adding the below line to /etc/sudoers configuration file.
Defaults requiretty
To allow a remote script to login and run a command via sudo simply comment out that line as shown below.
# Commented out so remote script can login and run a command without a tty # Defaults requiretty
I would suggest making a comment in the sudoers file along with the actual script that is running just in case there is another systems administrator that is tasked with working on this server at a later date. Now when your script runs it will not throw that error and should be able to run the remote command that was initially required.
- sudo: unable to resolve host I ran in to the weirdest error today. “sudo: unable...
- Compiling ruby on OS X 10.5: readline.c:703: error: ‘filename_completion_function’ undeclared When compiling Ruby 1.8.6 (in this case, p287) on OS...
- NGINX – SSL received a record that exceeded the maximum permissible length When configuring NGINX with SSL the configuration stated you could...
- Authentication refused: bad ownership or modes for file .ssh/authorized_keys When configuring SSH keys to be able to login to...
- TFTP: Error code 1: File not found You may get the below error code when attempting to...
Tags: error, perl, requiretty, script, SSH, sudo, sudoers, tty
























Entries (RSS)
thanks. it works for me.
[Reply]
alex Reply:
March 24th, 2009 at 11:43 AM
Cool. Glad it helped.
[Reply]
Instead of commenting it out for everybody, you can just turn it off for certain users (or user groups)
Defaults:alex !requiretty
[Reply]
alex Reply:
May 26th, 2009 at 7:16 PM
Hello Jay,
Great advice. I wasn’t aware of that syntax. Thanks for posting a response man!~
[Reply]
Great Help!!!!!!!!!
[Reply]
alex Reply:
July 20th, 2009 at 11:11 AM
Thanks for the feedback!
[Reply]
The “-t” switch on the ssh command will allocate a pseudo tty. You won’t nned to change yor sudoers file.
[Reply]
alex Reply:
July 21st, 2009 at 4:06 PM
Hello Nathan,
Thanks for the addition.
[Reply]
Is there a way to include “-t” option from perl script?
From command line, we can say “ssh -t user@host”
As of now, my code is as follows:
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $password);
my($stdout1, $err, $ext1) = $ssh->cmd(”sudo su – geneindex”, $password);
[Reply]
alex Reply:
August 14th, 2009 at 1:15 AM
Hello Neel,
Sorry for the delayed response. Below is the response from a friend of mine that knows way more Perl than I.
net::ssh takes the ‘use_pty’ option, which should have the same effect as -t for the cli command
http://search.cpan.org/dist/Net-SSH-Perl/lib/Net/SSH/Perl.pm
Hope that helps.
[Reply]
Thanks mate
[Reply]
alex Reply:
October 18th, 2009 at 5:04 PM
Hello nate,
No problem. Thanks for leaving feedback.
Thanks.
alex
[Reply]
Я бы кое-чего добавил конечно, но по сути сказано все.
[Reply]
Очень хороший и актуальный блог! Стабильный житель моего RSS ридера :)
[Reply]
alex Reply:
December 27th, 2009 at 2:00 AM
Hello мepтвeц,
Thanks for the compliment. Glad you use the RSS functionality to read the blog and hope you continue to find it useful.
Thanks.
alex
[Reply]