Beginners Guide for Logname Command on Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The logname command gives you the username of the currently logged-in user by reading the “/var/run/utmp” file, which is identical to the whoami command with one difference.

Stick with this article to learn the difference between the logname and whoami commands, the usage of the logname command, and how to use it in shell scripts.

Tutorial Details

DescriptionLogname
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteslogname
Internet RequiredNo

Syntax of the Logname Commands

The logname command takes only one argument as an option (optional).

$ logname [OPTION]

The following is the list of all known options accepted by this command.

OptionDescription
--helpDisplay the help section.
--versionDisplay version information.

Usage of the Logname Command in the Terminal

This command can be directly executed without specifying any options to get the currently logged-in username.

$ logname

Output:

Printing the username using the logname command

As you can see, the usage is pretty simple, but you should also know the few differences between this and the whoami command.

Difference Between the Logname and Whoami Commands

Most of the time, both commands work the same, but when both are run with sudo privilege, the logname command only shows the user’s name, while the whoami command shows the username that is actually in charge, which will be root.

Executing both the commands without sudo privilege.

$ logname
$ whoami

Output:

Executing both commands without sudo privilege

Executing both the commands with sudo privilege.

$ sudo logname
$ sudo whoami

Output:

Executing both commands with sudo privilege

Furthermore, if you have multiple user accounts on your system and use the su command to switch to a different account.

When you attempt to validate the username, it will still return the username that initiated the first interactive login-shell, in my case “linuxtldr”.

$ su jake
$ logname

Output:

Checking the username after switching to a different account using the logname command

After learning the difference, it’s debatable whether this command should be used when writing a shell script, as whoami is more efficient for this task.

Yet I have given you an example of this command in the shell script, and the answer to this question is up to you (leave it in the comment section).

Usage of the Logname Command in Shell Script

The following is a simple script that will check whether the username is “backup” or not; if it is “backup“, it will start the backup process or ask for the backup account.

if [[ "$(logname)" = 'backup' ]]
then
	echo "Starting the backup process..."
else
	echo "Require the backup user account."
fi

Output:

Starting backups using the shell script

What is $LOGNAME and $USER Shell Variable

The $LOGNAME is the same as the $USER environment variable set by your system to give you the name of the currently logged-in user.

There is a small difference between this variable and the logname command, followed by

  • The $LOGNAME variable gives the name of the currently logged-in user in the terminal.
  • Whereas, “logname” prints the name of the interactively logged-in user who initiated the session.

The following is an example of the differences between both commands:

$ echo $LOGNAME
$ logname
$ sudo su
$ echo $LOGNAME
$ logname

Output:

Difference between the logname variable and logname command

As I’ve told you earlier, this variable is set by your system and can be easily modified, so it’s better to avoid using it as a medium to find the username of a logged-in user.

That’s all for this article for now.

If you have any questions or queries related to this topic, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.