Running python as a different user

Hyperblaze

Lifer
May 31, 2001
10,027
1
81
I'm trying to run an app in python under a different user

for example.

I want to load up gdesklets on start up. I've got the bash script written up to do it....but unfortunately, it automatically runs as root.

is there any way to make python run as a different user even if executed as root?

ie, root#python /usr/share/gdesklets/gdesklets start (is what i have so far)

but I'd like run gdesklets as someone else (ie, user) but executed as root?

does this make sense?

 

drag

Elite Member
Jul 4, 2002
8,708
0
0
If you want to have a command executed with root permissions, but with a normal user you have several choices.

The main ones are:
1. use sudo
2. use setuid bit

However for scripts the setuid bit is ignored (check out man chmod for details) because it's almost impossible to make a secure bash script.

To use sudo you go:
sudo python /usr/share/gdesklets/gdesklets start

Then that will do what you want it to, hopefully.

Now with sudo you can tailor it to a certain extent. Normally it doesn't allow users to use it on most systems. So to set it up you have to use the visudo command, which opens a editor for /etc/sudo (there are problems with editing the file while it is in use, so visudo opens it in a special safe way, but if you don't have any users using sudo you can probably safely edit with any editor.)

But if you want you can set it up so only users belonging to a certain group can only execute certain commands, but without being prompted for a password, for instance.

Normally sudo is setup so that any user belonging to group "wheel" can execute any command as super user with only being asked their password.

All that is pretty generic though. It's pretty rare that you actually need to give roots rights to users for them to accomplish certain tasks for day to day use.