After trying modules os
, subprocess
, getpass
, I realised that the problem is not whether the user is set. The user gets set or gets changed using os.setuid
, however, the methods from modules to get username like os.getlogin()
, getpass.getuser()
, actually does not get username properly. If you run a shell command whoami
or id
using subprocess.Popen()
or os.system()
, you will get the changed user. These are little puzzled outputs for me. Below is script which shows all these weird behaviours.
import os import subprocess import pwd import getpass #os.chdir("/tmp") #uid = pwd.getpwnam('newuser').pw_uid os.setuid(500) # newuser 's id found from shell cmd line print os.getuid() p = subprocess.Popen(['id'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) out, err = p.communicate() # print os.system('useradd newuser1') # Try this commenting, it will not create, and then try commenting above line of setuid.i.e.it will become root, and then see the change. # print os.getcwd() print out, err p = subprocess.Popen(['whoami'], stdout = subprocess.PIPE, stderr = subprocess.PIPE) out, err = p.communicate() print out, err print getpass.getuser() print os.getlogin() print os.system('whoami')
After trying modules os, subprocess, getpass, I realised that the problem is not whether the user is set. The user gets set or gets changed using os.setuid, however, the methods from modules to get username like os.getlogin(), getpass.getuser(), actually does not get username properly. If you run a shell command whoami or idusing subprocess.Popen() or os.system(), you will get the changed user. These are little puzzled outputs for me. Below is script which shows all these weird behaviours.,getpass.getuser() doesn't use getuid() or geteuid() to get the current user.,This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string. If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an exception is raised.,http://docs.python.org/3/library/getpass.html#getpass.getuser
After trying modules os
, subprocess
, getpass
, I realised that the problem is not whether the user is set. The user gets set or gets changed using os.setuid
, however, the methods from modules to get username like os.getlogin()
, getpass.getuser()
, actually does not get username properly. If you run a shell command whoami
or id
using subprocess.Popen()
or os.system()
, you will get the changed user. These are little puzzled outputs for me. Below is script which shows all these weird behaviours.
import osimport subprocessimport pwdimport getpass #os.chdir("/tmp") #uid = pwd.getpwnam('newuser').pw_uidos.setuid(500) # newuser 's id found from shell cmd lineprint os.getuid()p = subprocess.Popen([' id '], stdout=subprocess.PIPE, stderr=subprocess.PIPE)out, err = p.communicate()# print os.system(' useradd newuser1 ') # Try this commenting, it will not create, and then try commenting above line of setuid. i.e. it will become root, and then see the change.# print os.getcwd()print out,errp = subprocess.Popen([' whoami '], stdout=subprocess.PIPE, stderr=subprocess.PIPE)out, err = p.communicate()print out,errprint getpass.getuser()print os.getlogin()print os.system(' whoami ')
Last Updated : 25 Jun, 2019
Real user ID of the current process: 1000
Real user ID of the current process: 0 Real user ID changed Real user ID of the current process: 1500