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

by on August 8, 2007 · 4 comments· last updated at July 15, 2011

I'm trying to run the following command:
ssh user@box.example.com sudo command1 /path/to/file

But It give me an error which read as follows:

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

How do I fix this problem?

This is done in Fedora, RHEL, CentOS and many other Linux distribution for security concern as it will show the password in clear text format. 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.

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
 


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 4 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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

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

Previous Faq:

Next Faq: