You might have seen “sudo: Sorry, you must have a tty to run sudo” Error on a Linux and Unix machine while trying to run some command or when trying to start some services. In this article, we will look into the error and the various methods which can be used to solve this error.
What is sudo
sudo is a command Line utility in Linux based systems which allows a command to be run with privileged user access i.e with root access. For any command to be run with sudo access you need to provide this access to the User through /etc/sudoers file. You can check more on Step by Step: How to Add User to Sudoers to provide sudo access to the User.
What is TTY
TTY is basically known as teletypewriter also known as terminal. It is an interface to run Linux/Unix based commands. You can always find multiple number of TTY in your System. Whenever you run a command in your Linux/Unix Systems you need to provide input through the terminal and then you get the output in the same terminal.
“sudo: Sorry, you must have a tty to run sudo” Error
You will usually encounter this error when sudo does not have a terminal to run some command. If you are trying to start a service which in turn is running some command in backend with sudo access then you will be asked to allocate a tty to run the command through sudo access.
Another scenario could be when you are trying to run some sudo command on remote host which requires a password to run that command but there is no terminal assigned so it will again throw
"sudo: Sorry, you must have a tty to run sudo" error as shown below. It is worth noting here that we are trying to connect remote host through root user and not through non-privileged user still we are able to see the same error.
[root@localhost ~]# ssh firstname.lastname@example.org sudo ls -l sudo: sorry, you must have a tty to run sudo
This error could also occur locally whenever you try to run some command with sudo access as shown below.
[root@localhost ~]# su -c 'sudo -l' centos sudo: sorry, you must have a tty to run sudo
There are basically two different methods which you can use to solve this “sudo: Sorry, you must have a tty to run sudo” error as shown below.
Method 1: Use -t option with ssh command
If you are getting
"sudo: Sorry, you must have a tty to run sudo" error while trying to run sudo command on remote host then you need to use
-t option with ssh command to provide pseduo terminal to run sudo command as shown below.
[root@localhost ~]# ssh -t email@example.com sudo ls -l total 1737405 -rw-r--r-- 1 root root 2048 Dec 23 2019 --exclude-file=hello.txt -rw-r--r-- 1 root root 888888898 Jun 7 08:35 file.txt -rw-r--r-- 1 root root 1798 Dec 23 2019 files.tar.bzip2 -rw-r--r-- 1 root root 3893 Dec 23 2019 hello.txt -rwxr-xr-x 1 root root 4 Jun 9 21:48 pr.sh -rw-r--r-- 1 root root 3893 Dec 23 2019 test.txt -rw-r--r-- 1 root root 28 Jun 19 08:44 testfile.txt Connection to 192.168.0.104 closed.
Method 2: Remove or Comment out “Defaults requiretty” Line in /etc/sudoers file
Second method which is also a permanent solution is to remove or comment
"Defaults requiretty" Line in
/etc/sudoers file as shown below.
[root@localhost ~]# grep -i requiretty /etc/sudoers Defaults requiretty
You can simply put
# mark at the beginning of
Defaults requiretty Line to disable the
requiretty option from
/etc/sudoers file as shown below.
[root@localhost ~]# vi /etc/sudoers .................................................... ## Allows members of the users group to mount and unmount the ## cdrom as root # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom ## Allows members of the users group to shutdown this system # %users localhost=/sbin/shutdown -h now ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment) #includedir /etc/sudoers.d #Defaults requiretty