Change user on UNIX

Preston Landers planders at
Thu Mar 20 22:02:12 CET 2008

On Mar 20, 9:46 am, Jonathan Gardner <jgard... at>

> In the unix world, this is highly discouraged. You shouldn't have to
> change your user. The only user who can change roles---and who should
> change roles for security reasons---is root.

IMHO this statement is a bit too broad.  The original poster didn't
specify that he wanted to become root.

Running a command as a different user is useful for other cases
besides running as root.  For instance, your web server's documents
directory may be owned by a www user who doesn't have a normal login
shell.  If you're on your 'regular' user and need to edit a document
it's quite handy to do this:

sudo -u www emacs index.html

As for the original poster, you could use the subprocess module
combined with sudo to do what you want - spawn a subprocess which runs
sudo and the other program, which could itself be a python script or
anything else.


More information about the Python-list mailing list