Howto remove “sudo: sorry, you must have a tty to run sudo” on Linux Systems

undefined

In this mini post I’ll show you how to enable sudo command to run remotely on Linux systems (RPM Family “Redhat / CentOS  / Scientific Linux  releases 7 / 6 / 5”). This mini post will solve this error message “sudo: sorry, you must have a tty to run sudo” which appears when you try to use sudo command either with ssh command when you try to execute command remotely or when you use sudo command in a shell script on RPM family of Linux systems.

First time I saw this error, when I was trying to use sudo command with rsync command, I was trying to backup some configuration files from a remote server to my local machine.

Exactly I was trying to run the following command:

rsync -av -e "ssh" --rsync-path="sudo rsync" normal-user@remote-server-IP:/etc/nginx ~/Mybackup

It works fine with Debian Linux systems, but it does not work with redhat Linux systems and gives the following error:

sudo: sorry, you must have a tty to run sudo
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [Receiver=3.0.9]

Don’t panic, there is an easy fix!

This because we are trying to access Linux systems (RPM Family “Redhat / CentOS  / Scientific Linux  releases 7 / 6 / 5”) remotely, which configure  sudo  to require a tty. This is generally enforced by having Defaults requiretty in the /etc/sudoers.

If you check the man pages for sudoers, you will find the following:

.$ man sudoers | grep -i requiretty -A 5
 requiretty     If set, 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(8) or cgi-bin scripts. This flag is off by
                    default.

From the man pages help, we can see that sudo can only be run from a login session and not via other means such as cron(8) or cgi-bin scripts, and with ssh for remote login.

How we fix this error?

We need to disable requiretty  to fix this error, we have two options:

  • Option 1: globally disable requiretty

We need to edit sudoers file as follow:

# sudoedit /etc/sudoers

Find line that read as follows:

Defaults    requiretty

Either comment it out the line or delete the line:

#Defaults    requiretty

Save and exit. now everything is fine for all users.

  • Option 2: disable requiretty for a specific user

We need to edit sudoers file as follow:

# sudoedit /etc/sudoers

And append the following line “you should replace myuser with your user“:

Defaults:myuser !requiretty

Save and exit. now everything is fine for a single user.

At this point you can use sudo in a shell script and can use it  with ssh command.

Hint:
1.we can disable requiretty per command in /etc/sudoers. Just append the following line to your sudoers file and replace the command with any command you want.
                            Defaults!/path/to/my/bin !requiretty

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *