| 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
|
Posted: Sun Jan 28, 2007 1:03 pm Post subject: Shell Scripting - different users. |
|
|
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 |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Sun Jan 28, 2007 3:28 pm Post subject: RE: Shell Scripting - different users. |
|
|
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 |
|
 |
GMorgan LXF regular
Joined: Thu Jan 12, 2006 6:58 pm Posts: 684 Location: South Wales, UK
|
Posted: Sun Jan 28, 2007 4:34 pm Post subject: RE: Shell Scripting - different users. |
|
|
| 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 |
|
 |
nordle LXF regular

Joined: Fri Apr 08, 2005 10:56 pm Posts: 1497
|
Posted: Sun Jan 28, 2007 5:21 pm Post subject: RE: Shell Scripting - different users. |
|
|
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 |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Sun Jan 28, 2007 5:45 pm Post subject: Re: RE: Shell Scripting - different users. |
|
|
| 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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Sun Jan 28, 2007 10:12 pm Post subject: Re: RE: Shell Scripting - different users. |
|
|
| 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 |
_________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|