Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

16
  • 43
    There is nothing wrong with getpass.getuser(), since does not claim to authenticate the user. The purpose of the function is to determine who the user is claiming to be without asking the user directly. Commented Oct 17, 2011 at 3:47
  • 10
    That does not work if you've done a su. $ echo $USER hithwen $ su julia Password: $ echo $USER julia $ python >>> import getpass >>> getpass.getuser() 'hithwen' Commented Jan 23, 2013 at 9:56
  • 8
    @GregHewgill raises a very good point, but indeed, finding out the username in a simple unauthenticated way like this does have some applications. My current use-case: tagging some shared testing resources with a username for easy cleanup later. (So it's easier to figure out whose mess is whose.) Commented Aug 16, 2013 at 15:35
  • 23
    And I agree with @GregHewgill for the access control purposes, but completely disagree with 'any other purpose' - that's like saying "Don't use $USER in a shell script". Great point about access control but there are plenty of other uses that don't involve auth. Commented Feb 26, 2014 at 8:27
  • 4
    @hithwen That's working as intended. It depends on which arguments you give to su. Perhaps you wanted to use su - julia. Commented Oct 27, 2015 at 13:01