Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'logname' Command In Linux

2024-04-17 - By Robert Elder

     I use the 'logname' command to print the name of the currently logged in user:

logname
robert

     The 'logname' command is similar to the 'whoami' command which prints out the name of current effective user:

whoami
robert

Difference Between 'whoami' And 'logname'

     If I run this echo statement:

echo "whoami: $(whoami), logname: $(logname)"

     you can see that both of these commands produce the same output:

whoami: robert, logname: robert

     However, if I run the same command through sudo:

sudo sh -c 'echo "whoami: $(whoami), logname: $(logname)"'

     you can see that the logname command outputs robert while the whoami command outputs root:

whoami: root, logname: robert

     This difference is due to that fact the 'whoami' command outputs the effective user, as described in the 'info' page:

info whoami
...
20.3 ‘whoami’: Print effective user ID
======================================

‘whoami’ prints the user name associated with the current effective user
ID.  It is equivalent to the command ‘id -un’.
...

     In contrast, the 'logname' command outputs the user returned by the 'getlogin' function, as described in the POSIX specification.

     According to the documentation, the 'getlogin' function returns the user that's logged in on the controlling terminal of the process, or a null pointer if this information cannot be determined:

man getlogin
...
       getlogin()  returns  a pointer to a string containing the name of the user logged in on the
       controlling terminal of the process, or a null pointer if this information cannot be deter‐
       mined.   The string is statically allocated and might be overwritten on subsequent calls to
       this function or to cuserid().
...

     The 'logname' command appeared as early as 1980 in Unix release version 3.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use