5

I would like to get the parent user after a sudo command in Python For example, from the shell I can do:

# Shows root; *undesired* output
$ sudo whoami
root

# Shows parent user sjcipher, desired output
$ sudo who am i
sjcipher

How do I do this in Python without using an external program?

14
  • 1
    without calling external command please read my question. Commented Jul 1, 2014 at 18:50
  • See this answer: stackoverflow.com/a/2899055/2073595 Commented Jul 1, 2014 at 18:51
  • that is different, please check the difference of whoami and who am i Commented Jul 1, 2014 at 18:52
  • whats up with who am i thats just the command who Commented Jul 1, 2014 at 18:52
  • 2
    os.getlogin() will work if there's a controlling terminal. Commented Jul 1, 2014 at 19:00

6 Answers 6

8

SUDO_USER environmental variable should be available in most cases:

import os

if os.environ.has_key('SUDO_USER'):
    print os.environ['SUDO_USER']
else:
    print os.environ['USER']
Sign up to request clarification or add additional context in comments.

Comments

3

who am i gets it's information from utmp(5); with Python you can access with information with pyutmp;

Here's an example, adapted from the pyutmp homepage:

#!/usr/bin/env python2

from pyutmp import UtmpFile
import time, os

mytty = os.ttyname(os.open("/dev/stdin", os.O_RDONLY))

for utmp in UtmpFile():
    if utmp.ut_user_process and utmp.ut_line == mytty:
        print '%s logged in at %s on tty %s' % (utmp.ut_user, time.ctime(utmp.ut_time), utmp.ut_line)


$ ./test.py
martin logged in at Tue Jul  1 21:38:35 2014 on tty /dev/pts/5

$ sudo ./test.py
martin logged in at Tue Jul  1 21:38:35 2014 on tty /dev/pts/5

Drawbacks: this is a C module (ie. it requires compiling), and only works with Python 2 (not 3).

Perhaps a better alternative is using of the environment variables that sudo offers? For example:

[~]% sudo env | grep 1001
SUDO_UID=1001
SUDO_GID=1001

[~]% sudo env | grep martin
SUDO_USER=martin

So using something like os.environ['SUDO_USER'] may be better, depending on what you're exactly trying to do.

1 Comment

ah this is the easiest os.environ['SUDO_USER'], thats perfectly make sense.
0

Depending on the setup you could use the environment variables that have been set. Note, that this may not work in all cases but may in yours. Should return the original user before su.

import os
print os.environ["USER"]

6 Comments

this will give the current user not the one before switch user.
Worked fine for me after switch user. What OS are you targeting if I may ask?
i am running on RHEL 6.3
Odd, I tested it on RHEL 5.5 and it worked. Oh well, seems like you found an answer with the os module anyway.
if i do sudo python and go to interactive shell and did it gave me 'root' but i am expecting to see the original user who run sudo. btw thank you i learnt something.
|
0

Here's a one-liner that will do it.

user = os.environ['SUDO_USER'] if 'SUDO_USER' in os.environ else os.environ['USER']

Comments

0

Either os.getlogin() or os.getenv('SUDO_USER') look like good choices.

[vagrant@localhost ~]$ sudo python3.6
Python 3.6.3 (default, Oct 11 2017, 18:17:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getlogin()
'vagrant'
>>> os.getenv('SUDO_USER')
'vagrant'
>>> os.getenv('USER')
'root'
>>> import getpass
>>> getpass.getuser()
'root'

Comments

-1

as referenced before, you can use getpass module

>>> import getpass
>>> getpass.getuser()
'kostya'

2 Comments

this doest give the original user before sudo or switch user. i am trying to figure out the first user.
i think you have to make a bold note about this. everyone giving answers like me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.