rc.local and sudo on CentOS

If you have a sudo command you’re trying to run at startup in your /etc/rc.local on a CentOS system, it might not work. The reason is the following error that’s buried in the /var/log/boot.log file:

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

It might be hard to find, but you can always add a ‘sleep 30’ before your sudo command in your /etc/rc.local. This way, the message will show up at the end of the boot.log

Anyhow the reason this is happening is for security. To allow sudo on boot, run ‘visudo’ as root and comment out ‘Defaults requiretty’ as seen below:

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
#         You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults    requiretty

Now your sudo command should run just fine at boot time.

——————————

Here’s an example of why you might want to run sudo at boot.

sudo -u minesrv -i /home/minesrv/scripts/startMinecraft

In the above command, I’m running the script startMinecraft, as the user minesrv. startMinecraft has some screen magic, and I want it tied to the minesrv user.

Leave a Reply

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