I created a (system) user, eg: mysysuser
, and some sudo rules so it can do some maintenance in other users folders.
This script needs to run as other user but also needs some parameters.
Therefore, I created the following in /etc/sudoers.d/mysysuser
example:
Cmnd_Alias FIRST = /usr/bin/su -l user1 -c '/home/*/www/*/deploy.sh "$0" "$1" "$2"' * * *
Cmnd_Alias SECOND = /usr/bin/su -l user2 -c '/home/*/www/*/deploy.sh "$0" "$1" "$2"' * * *
mysysuser ALL=(ALL) NOPASSWD: FIRST,SECOND
Then I run:
sudo /usr/bin/su -l user1 -c '/home/user1/www/site1/deploy.sh' "param 1" "param 2" "param 3"
this does not work and asks for password.
This version without parameters works fine and executes without asking password:
Cmnd_Alias FIRST = /usr/bin/su -l user1 -c /home/*/www/*/deploy.sh * * *
But... the script does not receive the args.
I know the sudoers file needs it to be very literal with the exception for the wildcard:*
I am wondering why it does not work, is it the:
"$0" "$1" "$2"
part?
Escaping the dollar signs makes visudo
barf.
UPDATE:
As per the comments, it is advised to use sudo directly without the su command.
I tried this but now I can't get it to work, sudo asks for the password although it shouldn't.
/etc/sudoers.d/mysysuser:
Cmnd_Alias FIRST = /home/*/www/*/deploy.sh "$0" "$1" "$2" * * *
mysysuser ALL=(ALL) NOPASSWD: FIRST
the command I execute as mysysuser:
sudo -u user1 /home/user1/www/site1/deploy.sh "param 1" "param 2" "param 3"
result:
We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things:
#1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility.
[sudo] password for mysysuser:
sudo su ...
you are in effect saying, "Hellosudo
please can I have root access ... thank you. Hellosu
, although I'm already root please would you give me root access ... thank you`. – Chris Davies Mar 05 '20 at 18:00