os.getlogin() Error

Chris Angelico rosuav at gmail.com
Thu May 4 17:46:32 EDT 2017


On Fri, May 5, 2017 at 6:03 AM, Wildman via Python-list
<python-list at python.org> wrote:
> I will try to explain...
> The program reports system information based on the user's name.
> Things such as passwd, groups and shadow info.  However, the
> program must have elevated privileges to get the shadow info so
> the program has the option to 'restart as root' so the shadow
> information will be obtainable.
>
> If the program is restarting as root, the work-arounds report
> the user as 'root'.  Then the system information for passwd,
> groups and shadow will be reported for 'root' and not the
> user that ran the program.  The actual user name that ran
> the program is needed for the program to report correct
> information.
>
> It seems that only os.getlogin() reports the true user name no
> matter if the program is run as a normal user or restarted as
> root.
>
> Is there a way to get the actual user name or is there a fix
> or a better work-around for the os.getlogin() function?

That depends on exactly how the program "restarts as root". One very
common way to do this is to invoke itself through sudo(1). If that's
how you're flipping yourself to root, check os.environ["SUDO_USER"] or
os.environ["SUDO_UID"], both of which will be set by sudo before it
invokes the program.

ChrisA



More information about the Python-list mailing list