Changing User in Python

Changing user in python

you could use:

os.system('sudo -u hadoop bin/hadoop-daemon.sh stop tasktracker')

or if you dont have sudo, but have su

os.system('su hadoop -c "bin/hadoop-daemon.sh stop tasktracker"')

Changing user within python shell

Generally, there is no way to masquerade as another user without having root permission or knowing the second user's login details. This is by design, and no amount of python will circumvent it.

Python-ey ways of solving the problem:

  • If you have the permission, you can use os.setuid(x), (where x is a numerical userid) to change your effective user id. However, this will require your script to be run as root. Have a look at the os module documentation.

  • If you have the permission, you can also try su -c <command> instead of sudo. su -c will require you to provide the password on stdin (for which you can use the pexpect library)

Unix-ey ways of solving the problem:

  • Set the group executable permission on the script and have both users in the same group.
  • Add the setuid bit on the script so that user1 can run it effectively as user2:

    $ chmod g+s script

    This will allow group members to run the script as user2. (same could be done for 'all', but that probably wouldn't be a good idea...)

[EDIT]: Revised question

The answer is that you're being far too promiscuous. Project A and project B probably shouldn't interact by messing aground with each other's files via Popening each other's scripts. Instead, the clean solution is to have a web interface on project A that gives you the functionality that you need and call it from project B. That way, if in the future you want to move A on a different host, it's not a problem.

If you insist, you might be able to trick sudo (if you have permission) by running it inside a shell instead. for example, have a script:

#!/bin/sh
sudo my/problematic/script.sh

And then chmod it +x and execute it from python. This will get around the problem with not being able to run sudo outside a terminal.

Python change users' directory

Sorry, you can't do that with a program that's launched in the normal way. At least, you can't in a POSIX-compliant OS.

When you run a script or program in the normal way it gets run in a new process, and of course any changes made in that new process don't affect the parent process: child processes inherit environment from their parent, the parent's environment can't by affected by any changes the child makes to its environment.

There's kind of a way around this: you can put a cd command into a script and then source that script, which executes the script in the current process rather than running it in a new process. I guess your Python code could create a tiny shell script that changes to the desired directory, but you will still need the user to source that script to make the actual directory change. When I need to do this for my own use, I just print the desired cd command to the shell so I can easily copy & paste it and then hit Enter. :)

Take a look here for more details about why cd is like this.

How to change the user and group permissions for a directory, by name?

import pwd
import grp
import os

uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
path = '/tmp/f.txt'
os.chown(path, uid, gid)


Related Topics



Leave a reply



Submit