LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script that changes user (https://www.linuxquestions.org/questions/linux-general-1/shell-script-that-changes-user-161328/)

davholla 03-23-2004 08:38 AM

Shell script that changes user
 
I want to write a shell script that switches user runs another script and then exits the user account.
The only problem I am having is that the script askes for the password but I want the password to be in the script (the script will be execute only so the user will not be able to see the password).

jcspray 03-23-2004 12:57 PM

A script like this is probably not a very good idea. Perhaps you'd like to mention what it is you're trying to accomplish and a suggestion can be made.

If you're hell-bent on doing it like this, it's probably easiest to setuid the script to run as root, in which case "su username" will not prompt for a password.

looseCannon 03-23-2004 09:18 PM

Re: Shell script that changes user
 
Quote:

Originally posted by davholla
I want to write a shell script that switches user runs another script and then exits the user account.
The only problem I am having is that the script askes for the password but I want the password to be in the script (the script will be execute only so the user will not be able to see the password).


Quickes, easiest, least messy, and least security compromising way to do this is with SUDO. it should already be installed on your system, if not it's pretty easy to add. Use the command 'visudo' to edit the configuration.

You'll need to add a command alias:
Cmnd_Alias SOMECOMMAND=/path/to/the/script

And you'll need to add a line for the user to run this script
userName ALL=(userToRunAs) NOPASSWD: SOMECOMMAND


Then in your script to execute the command as the other user just write the command as:
sudo -u userToRunAs /path/to/the/script

All done.


All times are GMT -5. The time now is 05:42 AM.