The Linux logname command is a simple utility that is part of the GNU Core Utilities. It has a single purpose, to print the name of the current user. There are no options and the command takes no arguments. You simply call it and it prints the current users login name.
$ logname
mecherisi
The logname command should not be confused with the LOGNAME environmental variable. The LOGNAME enviornmental variable can be changed by the user as it is part of their enviornment. This is in contrast to the logname command, which uses the utmp/wtmp login records to pull the name of the current user.
There are plenty of tools that allow you to print the currently logged in user or users. Some offer a lot of options and switches, while others offer "effective" user identification. Some similar tools are whoami, who and w. Let's take a quick look at these commands and how they differ from the logname command.
The w command shows a lot more information about the user. It also displays a header and several columns of information. It has options and switches to change the base output. This is all good information, but sometimes you just need a simple answer. Here is an example of the output of the w command.
[mecherisi@putor ~]$ w
00:12:11 up 4:16, 1 user, load average: 0.88, 1.02, 1.27
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
mecherisi :1 19:57 ?xdm? 59:02 0.00s /usr/libexec/gdm-x-session --run-script /usr/bin/gnome-session
Similarly, the who command also shows additional information. It also shows ALL the users logged into the system. This and the amount of options make it very different from the logname command.
[mecherisi@putor ~]$ who
mecherisi :1 2019-11-27 19:57 (:1)
xguest :2 2019-11-27 13:45 (:2)
savona :3 2019-11-27 12:33 (:3)
The whoami command actually shows you the "effective" user (euid). You can see that when we use sudo with whoami it returns root as the user. This is because when you run sudo, you are "effectively" root.
[mecherisi@putor ~]$ whoami
mecherisi
[mecherisi@putor ~]$ sudo whoami
root
The logname command simply prints the name of the current user to STDOUT, nothing else. This comes in very handy if you are using it in a bash script and you just need the current user name.
[mecherisi@putor ~]$ logname
mecherisi
[mecherisi@putor ~]$ sudo logname
mecherisi
Conclusion
Sometimes less it more. If you are writing a script and just need to get the current users name, logname is your friend. Of course you can do it with one of the other commands and use cut to trim the additional information. But, logname is the most direct, simplest and pure way to get that singular piece of information.
Resource and Links
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
4 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (51)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
On my Arch box :
$ logname
logname: no login name
$ echo $USER
username
I guess it depends on what packages you installed on Arch. On my arch devel system:
[savona@archdev]: ~>$ logname
savona
coreutils should be installed with the base installation of Arch Linux. If not you can install it using "pacman -Sy coreutils" and then logname and many other utilities will be available.
coreutils is indeed installed on that machine. There should be some other missing library, or something weird in env.
I tried on another Arch box, and logname does output $USER.