Galaxin
Galaxin

Reputation: 484

Is it possible to run a script as a different user (other then root) in unix without prompting for a pwd?

I have a requirement wherein I am running a shell script from java code. I have been using rsh for this purpose of running a script as a different user and getting back the control to the previous user after successful execution of the script.

Syntax used is :

rsh -l <username> unix cmd(script here)

Now due to some security reasons I have to use only sh for the above purpose.

My Requirements are:

  1. Different users might run this script other than root and all have permissions to execute the same.

  2. Users executing the script are many and the passwords cannot be obtained from the code and sent to the script to be run successfully.

  3. The command should not prompt for the user password while executing this command.

  4. I cannot update any config files with username to stop it from prmpting the password.

I have gone through the alternatives such as ssh, su, sudo etc. but in vain.

Please suggest some alternative to this requirement that will fulfill all the above conditions.

Upvotes: 3

Views: 902

Answers (1)

fedorqui
fedorqui

Reputation: 289735

You can use a *NIX feature called setuid by setting

chmod +s your_file

You can find a great explanation in Jonathan Leffler's answer on A usage of setuid?.

To check if the script is being executed by root or not, you can check $EUID, that contains the ID of the user executing the script. If it is 0, it means root (and other users with root permissions).

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

From How to: Check the bash shell script is being run by root or not

Upvotes: 3

Related Questions