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
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
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
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|