2

I'm trying to allow a specific user to run a specific command with sudo but not be prompted for a password in CentOS 7. So I've used visudo tried added the following line as both the 1st and last line in the file

testuser ALL=(ALL) NOPASSWD: /home/testuser/script.sh

I've also tried

testuser ALL= NOPASSWD: /home/testuser/script.sh

then, as root, I run su -u testuser to switch to the user and then sudo ./script.sh and sudo /home/testuser/script.sh and am still prompted for a password. Is there something else I need to do to allow running this script without a password?

5
  • I suspect this won't be possible (but I am not sure). The problem is that when you run /home/testuser/script.sh, you aren't running the script, you are running the shell (e.g. bash) and passing the script as an argument to the shell.
    – terdon
    Jan 28, 2021 at 14:29
  • I found a thread on another forum that had a working solution. It said not to edit the sudoers file, but instead, add a new file to /etc/sudoers.d/xxxx. So in my case do "sudo visudo -f /etc/sudoers.d/testuser" and then I added "testuser ALL=NOPASSWD: /home/testuser/script.sh" to that file and was finally able to run sudo on the script without a password. Jan 28, 2021 at 14:40
  • 1
    great! Can you please post that as an answer and accept it so the question can be marked as answered?
    – terdon
    Jan 28, 2021 at 14:45
  • 2
    I am surprised that it made a difference (I expected both to work. I would have had to look up the syntax though). Can you confirm that it is the same line of text. And yes put that comment into an answer. It may be useful to someone, and you may get some up-votes. Jan 28, 2021 at 14:49
  • @ctrl-alt-delor yes, I tested too. I have no idea why having it in a separate file would make it work, but it does on my Arch too!
    – terdon
    Jan 28, 2021 at 14:54

1 Answer 1

0

Reposting answer from my comment:

I found a thread on another forum that had a working solution. It said not to edit the sudoers file, but instead, add a new file to /etc/sudoers.d/xxxx. So in my case do

sudo visudo -f /etc/sudoers.d/testuser 

and then I added

testuser ALL=NOPASSWD: /home/testuser/script.sh

to that file and was finally able to run sudo on the script without a password.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .