1. Overview
What does the “sudo: no tty present and no askpass program specified” error in Linux mean?
In this tutorial, we’ll answer this question by examining the sudo, tty, and askpass commands. Additionally, we’ll learn how to fix this error.
2. Basics of sudo, tty and askpass
First, we’ll take a look at sudo. The sudo or “superuser do” command executes other commands that require the privileges of a superuser:
$ cat /etc/shadow
cat: /etc/shadow: Permission denied
Let’s imagine a scenario where we want to check the contents of the sensitive /etc/shadow file without logging in as the root user. The best decision in this situation is to execute commands using sudo instead of a root user. This is key because a root user has permission to do anything and that can be risky.
To start, we open the terminal and type in the following command:
$ sudo cat /etc/shadow
[sudo] password for peter:
We can see from the above input that a prompt appears to insert the user password. Once we input the password, the system checks in the /etc/sudoers configuration file if the currently logged-in user has permission to make use of the sudo command.
It’s important to note that the /etc/sudoers file is a special file that configures which users can use sudo and which cannot.
If the user peter has the required superuser privileges to run the command above, it’s executed:
$ sudo cat /etc/shadow
[sudo] password for peter:
root:!:19177:0:99999:7:::
daemon:*:19101:0:99999:7:::
bin:*:19101:0:99999:7:::
sys:*:19101:0:99999:7:::
sync:*:19101:0:99999:7:::
games:*:19101:0:99999:7:::
The other command we’ll take a look at is tty. tty displays information on the terminal connected to our Linux system.
This terminal enables us to write commands as input and get output. A good example is when we had to input our user password when trying to view sensitive information earlier:
$ tty
/dev/pts/0
Finally, let’s take a look at askpass. askpass is not a specific program, but any program that can request a user for their passphrases or log-in passwords without exposing their input in readable plain text. Askpass facilitates automatic input of passwords and is applicable mainly during SSH sessions.
3. Fixing “sudo: no tty present and no askpass program specified” Error
It’s important to note that this error occurs when a user with no superuser privileges tries to execute a command with sudo.
Let’s see an example that might cause the error:
$ ssh remote_username@remote_host "sudo reboot"
sudo: no tty present and no askpass program specified
Here, the sudo command tries to execute a system reboot. This requires the user’s password input to validate if they are the superuser*.* Next, sudo looks for a terminal and then askpass to trigger a prompt, but fails in both. Due to this failure, the user remote_username does not get validated as a superuser to log in to the remote machine remote_host.
Now, let’s take a look at the various solutions we can try out. We’ll have to log in as a user with superuser privileges.
First, let’s open our Linux terminal and type in the command below:
$ sudo visudo
[sudo] password for peter:
Before we continue, let’s keep in mind that mistakes in the /etc/sudoers file can be very costly. This is because we can inadvertently lock out a user or all users out of the system. That’s why we use the visudo program to open a text editor for checking errors in the configuration file during a file save.
Now, we insert the user password to get access to the /etc/sudoers configuration file. Next, we scroll to the end of the file and add this line:
peter ALL = (ALL) NOPASSWD: ALL
This ensures the user peter doesn’t get a prompt to input a password while using sudo. Hence, a terminal is not needed.
What if we want a user’s password input? For this reason, we would require a terminal. Hence, we’ll take a look at the second solution which is to use the ssh -t option:
$ ssh -t remote_username@remote_host "sudo reboot"
From the example above, we are rebooting the remote machine remote_username@remote_host via SSH. The -t option makes sure a terminal is provided for this process.
4. Conclusion
In this article, we’ve looked at a few solutions we can implement to resolve the error “sudo: no tty present and no askpass program specified“*.* These will come in handy to improve our productivity in our day-to-day interactions with the Linux system.