I have 2 users. test1 that i created it with /sbin/nologin and has no password. test2 that has sudo privilege and has passworsd.

I have this script: /data/tempWork/fazitst that must be run under test2. now i want to run this script with test1 but i want it to be run under test2.

I tried this as test1:
/bin/su -c "/data/tempWork/fazitst " - test2

but it didn't work. i was running this command from an application since i can't use ssh i can't debug this properly. there is no log. I tried this command with root and it worked.

but i guess that it requires password. so how i can run my script with test1 under test2 with no password?


That is exactly what sudo was designed for.

First, someone with root access will need to make a configuration line like this to /etc/sudoers (or /etc/sudoers.d/*, if supported by your OS version):

test1 ALL=(test2) NOPASSWD: /data/tempWork/fazitst

This will authorize user test1 (on ALL hosts using a copy of that sudoers file) to execute as user test2, without a password request, the command /data/tempWork/fazitst with any command-line parameters (or none at all).

Then, user test1 can execute it with:

sudo -u test2 /data/tempWork/fazitst

If the $HOME environment variable needs to point to the home directory of user test2 while executing the command, you may or may not need to add the -H option to the sudo command, depending on the default configuration of sudo in your OS version. In that case, the command for user test1 to run would be:

sudo -Hu test2 /data/tempWork/fazitst

If it is important that the environment is set exactly the same as when running su - test2, then the -i option of sudo is needed:

sudo -iu test2 /data/tempWork/fazitst
  • it worked perfectly. i had to use full path /usr/bin/sudo in my application. – BlackCrystal May 13 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.