Stuck with the error, sudo: sorry you must have a tty to run sudo? We can help you.
Sudo is a command-line program that allows users to run programs with the security privileges of another user or group which default to the superuser.
As part of our Server Management Services, we assist our customers with several Command-line queries.
Today, let us see how to fix this error on Linux or Unix based systems
sudo: sorry you must have a tty to run sudo
Here, TTY stands for teletypewriter. It is a command we use to provide the file name of the terminal connected to the standard input.
For example, /dev/ttys001.
We found that this error occurs when we run the following command:
ssh user@bob.example.com sudo command1 /path/to/file
The error message stated:
sudo: sorry, you must have a tty to run sudo
OR
sudo: no tty present and no askpass program specified
This is common to Fedora, RHEL, CentOS, many other Linux distribution, and Unix-like systems for security concerns as it shows the password in clear text format.
Moving ahead, this error occurs because the sudo command tries to execute a command that requires a tty.
This happens mostly because we run on a Linux distribution with sudo configured by default to require a tty.
Generally, this was a matter of security concern. However, in reality, this does not provide any real security benefit.
We can enforce it by setting Defaults requiretty in the /etc/sudoers.
How to solve the error?
- Use pseudo tty
In order to avoid this error, we have to run ssh command as follows:
ssh -t hostname sudo command
ssh -t user@hostname sudo command
ssh -t user@bob.example.com sudo command1 /path/to/file
The -t option forces pseudo-tty allocation.
We can use it to execute arbitrary screen-based programs on a remote machine, which can be very useful, for example, when implementing menu services. Multiple -t options force tty allocation, though the ssh has no local tty.
If we encounter the error “Pseudo-terminal will not be allocated because stdin is not a terminal”, then we use -tt to force a pseudo-terminal. If we do not need a pseudo-terminal, then use -T to simply disable pseudo-tty allocation.
- The requiretty option in the sudoers file
The requiretty if set in sudo config file sudoers, will only run when the user is logged in to a real tty.
When this flag is set, sudo can only run from a login session and not via other means such as cron, shell/perl/python, or cgi-bin scripts.
Edit /etc/sudoers, file:
# visudo
Find the line that read as follows:
Defaults requiretty
Either comment it out or delete the line:
#Defaults requiretty
Eventually, save and close the file.
To run the command without using the -t option, we can use the su command instead of the sudo command:
## NOTE: RHEL/CentOS specific syntax ##
su –session-command=”/path/to/command1 arg1 arg2″
## others ##
su -c ‘/path/to/command1 arg1 arg2’
OR
ssh user@bob.example.com su –session-command=”/path/to/command1 arg1 arg2″
OR
ssh user@bob.example.com su -c ‘/path/to/command1 arg1 arg2’
We can run /scripts/job1 as bob user using the same syntax:
ssh user@bob.example.com su –session-command=”/scripts/job1 /nas” bob
OR
ssh user@bob.example.com su bob -c “/scripts/job1 /nas”
Another option is to use the following syntax:
echo -e “\n”|sudo -S command
[Need help with the resolution? We’d be happy to assist]
Conclusion
In short, the error sudo: sorry you must have a tty to run sudo occurs because the sudo command tries to execute a command that requires a tty. Today, we saw how to solve this.
0 Comments