Home > Back-end >  python get normal username from the root user?
python get normal username from the root user?

Time:09-29

When I want a user in Python3, from terminal

sudo su

python3

>>> import getpass

>>>getpass.getuser()

'root'

how can I get the normal username?

CodePudding user response:

If you are running the script interactively, you should be using sudo to run it and os.getlogin() will give the correct user.

CodePudding user response:

I recommend the os.getlogin() from @bromate's answer.

Here is another way:

import os
user = os.environ['SUDO_USER'] # KeyError if not run under sudo
  • Related