Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shell Scripting - different users.

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Sun Jan 28, 2007 1:03 pm    Post subject: Shell Scripting - different users. Reply with quote

I'm wondering if a single shell script can run processes as different users.

I want to run the following as root
/sbin/ipw3945d
sleep 2
/usr/sbin/NetworkManager

then run
nm-applet

as a normal user.

I'm using FC6.
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Sun Jan 28, 2007 3:28 pm    Post subject: RE: Shell Scripting - different users. Reply with quote

Yep, assuming you're running the script as the normal user you can run commands as root with

sudo /sbin/ipw3945d
and
sudo /usr/sbin/NetworkManager

You'll need to have appropriate entries in /etc/sudoers, something like

fred ALL= NOPASSWD: /sbin/ipw3945d , /usr/sbin/NetworkManager



If your script is running as root and you want to issue a command as a normal user you need to do

su - fred -c /usr/bin/nm-applet
_________________
I have been touched by his noodly appendage.
Back to top
View user's profile Send private message
GMorgan
LXF regular


Joined: Thu Jan 12, 2006 6:58 pm
Posts: 684
Location: South Wales, UK

PostPosted: Sun Jan 28, 2007 4:34 pm    Post subject: RE: Shell Scripting - different users. Reply with quote

Will that second option keep the script running as root or will all subsequent commands be run as fred. Personally I'd prefer to avoid NOPASSWDing it.
Back to top
View user's profile Send private message
nordle
LXF regular


Joined: Fri Apr 08, 2005 10:56 pm
Posts: 1500

PostPosted: Sun Jan 28, 2007 5:21 pm    Post subject: RE: Shell Scripting - different users. Reply with quote

If the script is run by root user there is no need to know the passwords of the other users.

Each command issued within
su username -c "echo hello world"
will be run as username. Once that command is finished the script carries on as the user who called it, root.
_________________
I think, therefore I compile
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Sun Jan 28, 2007 5:45 pm    Post subject: Re: RE: Shell Scripting - different users. Reply with quote

GMorgan wrote:
Will that second option keep the script running as root or will all subsequent commands be run as fred. Personally I'd prefer to avoid NOPASSWDing it.


If you omit the NOPASSWD: bit from the entry in /etc/sudoers then when your script does a 'sudo command' you'd need to enter the password of the account running the script (not the root password), however any subsequent sudo commands within X minutes will remember that you've authenticated yourself and won't ask for the password again (I can't remember what X is though!).

If you were to use the NOPASSWD: field as in my example that would allow user fred to only run the two commands listed without providing a password.

Another trick you can do if you want user fred to run something as bert without knowing bert's password is as fred do

sudo su - bert -c "some command"

and then have an appropriate entry in /etc/sudoers
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8457
Location: Warrington, UK

PostPosted: Sun Jan 28, 2007 10:12 pm    Post subject: Re: RE: Shell Scripting - different users. Reply with quote

MartyBartfast wrote:
Another trick you can do if you want user fred to run something as bert without knowing bert's password is as fred do

sudo su - bert -c "some command"

and then have an appropriate entry in /etc/sudoers


You mean give fred permission to run su as root in /etc/sudoers? That's a very bad idea. A muchmore sensible option would be

Code:
sudo -u bert some command

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast