Skip to main content
Commands

Use logname Command in Linux

One of the simplest commands, logname gives you the name of the currently logged in user in Linux.

Sagar Sharma

As the name suggests, the logname command is used to print the name of the currently logged-in user:

Use the logname command in Linux to find the currently logged-in user

You might be thinking "The whoami command does the same thing, so is it any different from it?".

Yes. It is.

So in this tutorial, I will walk you through how you can use the logname command and how it differs from the whoami command.

Using the logname command

Unlike other Linux commands, using the logname command is quite simple because all you have to do is execute the logname in the terminal:

logname

Sure, you have two options: --help and --version but they won't help you modify the default behavior of the logname command.

So let's jump to what is the difference between the logname and the whoami command.

First, let me show you the output of the logname and the whoami command:

comparing logout and whoami command

Looks the same. Right?

Well, the best way to differentiate between them is to work with the elevated privileges (use sudo):

difference between whoami and logname command in linux

Saw that?

When I used the logname command with sudo, it still displayed the name of the logged-in user.

Whereas when I used the sudo with the whoami command, it displayed root rather than the username itself.

The main differentiating factor (the negative one)

The major difference between the logname and whoami commands is even if you switch to a different account using the su command, it will still show the username by which you logged in to your system.

For example, here, I used the su command to switch from sagar to a different user milan and then when I used the logname command, it still showed the earlier user name:

The logname command showing the username of previous user

So in my opinion, if you have multiple users, you may use the logname command to show the username who first logged in to the system.

What I use to find logged-in users

Personally, I don't use the logname command as I switch between multiple users, and if I were to use the $logname value, it may end up showing a different username in the script.

I personally use the who command to list all the logged-in users and whoami to know the current user.

But there are multiple commands with additional benefits so we wrote a detailed guide explaining different ways to find the logged-in user in Linux:

How to see Logged in Users in Linux [4 Simple Ways]
Check who is logged in your Linux system with these simple commands. You can also get additional information about logged in users such as their log in time.

I hope you will find this guide helpful.

Sagar Sharma