Change user in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change user in script
# 1  
Old 05-01-2013
Change user in script

i have a script that needs to be run as a specific user. lets call this specific user "skysmart".

sure, i can check the username of the person running the script and if it isn't "skysmart", simply abort with a descriptive error message. but then, that would require the user to have to "sudo su -" to skysmart and then rerun the script. i'd like to avoid this.

so, if this script is run as root, i want it to automatically "sudo su -" to "skysmart", and then run. without any further intervention from the user.

sample code:
Code:
if [ "${LOGNAME}" = "root" ] ; then

su - skysmart

........
.......
.......
.......

fi


Last edited by SkySmart; 05-01-2013 at 10:44 AM..
# 2  
Old 05-01-2013
That's not going to work. su interrupts the execution of the shell script until you log out of the "su session".

If you are logged in as root all the time, and are using the script as a shortcut to execute commands as skysmart, that's not a good idea. Instead, just stay logged in as skysmart. When you need to do something special (usually not that often), login as root, or use sudo.
# 3  
Old 05-01-2013
The script can call itself:
Code:
if [ "${LOGNAME:-$USER}" = "root" ] ; then
 echo "rerunning $0 as user skysmart"
 sleep 1 # for safety
 su - skysmart -c "$0 $@" fi
# this is run as user skysmart
...

# 4  
Old 05-01-2013
Quote:
Originally Posted by MadeInGermany
The script can call itself:
Code:
if [ "${LOGNAME:-$USER}" = "root" ] ; then
 echo "rerunning $0 as user skysmart"
 sleep 1 # for safety
 su - skysmart -c "$0 $@" fi
# this is run as user skysmart
...

this could work. is this a typo:

Code:
[ "${LOGNAME:-$USER}" = "root" ]

should it be:
Code:
[ "${LOGNAME}:-${USER}" = "root" ]

# 5  
Old 05-01-2013
No just a habit, to try $USER if $LOGNAME is not present.
There might be shells or conditions where LOGNAME is not set...
Code:
man sh
...
     ${parameter:-word}      If parameter is set and is non-null,
                             substitute its value; otherwise sub-
                             stitute word.

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 05-01-2013
like if you replace the "$0" with the actual path of to the script, it hangs.
# 7  
Old 05-01-2013
If you call the script with a relative pathname,
it is safer to replace $0 by the abolute path to this script.
Then I forgot an exec.
Then the fi somehow went to the end of the previous line
Code:
if [ "${LOGNAME:-$USER}" = "root" ] ; then
 echo "rerunning $0 as user skysmart"
 sleep 1 # for safety
 exec su - skysmart -c "/path/to/this/script $@"
fi
# this is run as user skysmart
echo "hello I am $LOGNAME"

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

script for password change for exiting user

Dear Forum, In our organization 100 user is existing. Now i want to change password for 100 user in linux server. Pls help to write script for changing password. Regads// Taifur (3 Replies)
Discussion started by: taifurakhand
3 Replies

2. UNIX for Dummies Questions & Answers

Change user passwords using shell script

Hi, I want to change the password of unix users on a number of servers.My plan was to ssh to all the servers in a shell script and use the passwd command. I tried to do so but everytime i run it i get this error. ssh -x -n -l user1 host passwd Changing password for "user1" 3004-709 Error... (3 Replies)
Discussion started by: poojabhat
3 Replies

3. Shell Programming and Scripting

Shell Script to change a user password using script

Hi Experts, I had tried to executes this script to change the user password through script: No lines in buffer #!/bin/ksh cat /etc/passwd | grep -v userid >> /tmp/pass.tmp1 cat /etc/passwd | grep userid >> /tmp/pass.tmp2 PASS1=`cat /tmp/pass.tmp2 | cut -d ":" -f2` PASS2=`q2w3e4r5` sed... (3 Replies)
Discussion started by: indrajit_renu
3 Replies

4. Shell Programming and Scripting

Change to user root without end the script

Hello there! I need help: I have this script: #!/bin/bash #chage to root user sudo su #Insert actual date echo -n "Ingrese fecha actual: " #Read the actual date read fecha #clean RAM memory sync ; echo 3 > /proc/sys/vm/drop_caches ; swapoff -a && swapon -a #backup opennms... (2 Replies)
Discussion started by: bobbasystem
2 Replies

5. Shell Programming and Scripting

script to change passwords for the same user on multiple servers

I am trying to write a script to change passwords for the same user on multiple servers. My environment runs purely ssh / scp not rsh / rcp and therefore coping using rcp is not an option. I have been playing with expect to perform tasks but think there must be a better way. Has anyone got... (7 Replies)
Discussion started by: stolz
7 Replies

6. Shell Programming and Scripting

Change user on remote machine and execute script!

Hi, I need to login into remote server and execute a shell script over there. As of now i am making use of ssh command ssh primUser@135.254.242.2 sh /poll.sh I am logging in as primUser but unless i change the user to root the script execution on the remote machine is not possible. ... (5 Replies)
Discussion started by: goutham4u
5 Replies

7. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

8. UNIX for Dummies Questions & Answers

change user through shell script

hi, my problem is that i am calling a script from my perl program. the script checks wether a particular process is running or not if the process is not running then it should start the process. the problem here is that the front end logs into backend with a user which does not have the... (0 Replies)
Discussion started by: raviraushanjha
0 Replies

9. UNIX for Dummies Questions & Answers

How would I telnet & change user password automatically in a script

I would like to create a script that logs into a list of several servers 50+ and changes my password all at once. Every 60 days we are required to login to each system and change our passwords or else they expire and our login account is deleted. I have an idea on how I could do this but... (4 Replies)
Discussion started by: darthur
4 Replies
Login or Register to Ask a Question

Featured Tech Videos