Search code examples
linuxrhel

RHEL - can't sudo from script


I have a script where I'm trying to change user (from root) on boot. In /etc/rc.d/rc.local, I've changed it to cd into the script dir and execute it. It all works except for when it tries to execute:

sudo -u newuser ./myscript.sh

I get an error message:

sorry, you must have a tty to run sudo

So I went and looked in my /etc/sudoers file, and it is already set to !requiretty, which should turn that off. I have also tried alternatives such as gksudo, but apparently RHEL 6.5 only supports basic sudo.

I know the script works because I can execute it myself from terminal, it's just when I try and execute it from a boot script.


Solution

  • Just changed:

    sudo -u newuser ./myscript.sh
    

    To:

    /bin/su -c ./myscript.sh newuser
    

    And it seems to work. I guess because sudo is normally there to elevate your privileges but since it starts as root I don't really need it and can use su instead. Will try this out in various places to make sure it works 100%.