sudo: Sorry, you must have a tty to run sudo Error and Soution

by on August 8, 2007 · 7 comments· LAST UPDATED December 8, 2013

in , ,

I'm trying to run the following command:

ssh user@box.example.com sudo command1 /path/to/file

But, an error is displayed follows:

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

How do I fix this problem on Linux or Unix based systems?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time2m
This is done in Fedora, RHEL, CentOS, many other Linux distribution, and Unix-like systems for security concern as it will show the password in clear text format.

Fixing: Sorry, you must have a tty to run sudo

You have to run your ssh command as follows to avoid this error:

 
ssh -t hostname sudo command
ssh -t user@hostname sudo command
ssh -t user@box.example.com sudo command1 /path/to/file
 

The -t option 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.

The requiretty option in sudoers file

The requiretty if set in sudo config file sudoers, sudo will only run when the user is logged in to a real tty. When this flag is set, sudo can only be run from a login session and not via other means such as cron, shell/perl/python or cgi-bin scripts. This flag is set on many distores by default. Edit /etc/sudoers, file, enter:
# visudo
Find line that read as follows:

Defaults    requiretty

Either comment it out the line or delete the line:

#Defaults    requiretty

Save and close the file.

How Do I run command without using the -t option?

You can use the su command instead of the sudo command as follows:

 
su --session-command="/path/to/command1 arg1 arg2"
 

OR

 
ssh user@server1.nixcraft.in su --session-command="/path/to/command1 arg1 arg2"
 

You can run /scripts/job5143 as vivek user using the same syntax:

 
ssh user@server1.nixcraft.in su --session-command="/scripts/job1 /nas" vivek
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 7 comments… read them below or add one }

1 Geoff August 25, 2010 at 6:26 pm

Next response:
“Pseudo-terminal will not be allocated because stdin is not a terminal”
Your thoughts?

Reply

2 Geoff August 25, 2010 at 6:33 pm

– Remove and ampersand after the command and it works fine.

Reply

3 acid child April 17, 2012 at 7:57 pm

This worked perfectly for me thanks for the tip!

Reply

4 nkiran November 1, 2012 at 5:04 am

One more way of doing this –
Open /etc/sudoers
comment out: #Default requiretty

Done!

Reply

5 benwing October 23, 2013 at 6:52 pm

modifying sudoers can be taken a step further and only be made a requirement for specific users.
ie. leave Defaults requiretty uncommented and add

Defaults:username    !requiretty

Reply

6 Abhinav December 27, 2013 at 7:16 am

Good one. Needed to do it today. and that error was irritating the hell out of me. Thanks for the post.

Reply

7 andy April 1, 2014 at 7:51 am

I think we can try sudo with -S parameter. As from man page of 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. So maybe:

echo -e "\n"|sudo -S 

And here’s more info about this.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: