Python equivalent of "logname" command

822 views
Skip to first unread message

Jason Hsu

unread,
Oct 31, 2011, 12:17:37 AM10/31/11
to PyMNtos
In GNU Bash, the "logname" command returns your username, even if you
invoke the command as root. (The "whoami" command returns "root" if
you invoke it as root.)

What's the Python equivalent of "logname"?

Matt Anderson

unread,
Oct 31, 2011, 9:37:59 AM10/31/11
to pym...@googlegroups.com
What do you mean "as root"? Sudo, su, logging in directly on console?

Try os.getuid() and os.geteuid() -- do they return different things? Do they return the same thing, neither of which is your normal userid?

You can turn a UID into a name with pwd.getpwuid(); returns a structure, but the pw_name field on the structure should be a user name.

Tony Collen

unread,
Oct 31, 2011, 9:52:06 AM10/31/11
to pym...@googlegroups.com
It's in the docs:

http://docs.python.org/library/os.html

$ sudo su -
# python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import os, pwd
>>> pwd.getpwuid(os.getuid())[0]
'root'


- T

Tony Wildfarmer

unread,
Jun 25, 2016, 1:54:42 PM6/25/16
to PyMNtos

Peter Farrell

unread,
Jun 25, 2016, 1:55:42 PM6/25/16
to pym...@googlegroups.com

--
Meetings Schedule / RVSP on our Meetup at http://python.mn
---
You received this message because you are subscribed to the Google Groups "PyMNtos" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymntos+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages