Why would you need to find the identity of the current user? In many cases the owner of the computer is the only user and, without getting too existential, they probably know themselves. Perhaps, but it is also common for people to create additional user accounts to allow family members to have access to the computer. And, if you’re connected to a remote shell on a server somewhere, you may need a quick reminder of the username you’re logged in with. If you see a logged in session with no one in attendance, how do you identify the current user from the command line?
Let’s try the easiest option first. All we need to do is look at the command prompt. By default, Linux distributions have the username in the prompt. Simple. We didn’t even have to type a thing.
If the user has changed their prompt to some other format we need to try something else. The who command will give us the information we are looking for.
The output from who gives you the name of the current user, the terminal they are logged in at, the date and time when they logged in. If it is a remote session, it also tell us where they are logged in from.
By comparison, the whoami command provides a very pithy answer:
You can get the same one-word answer by echoing the $USER environment variable to the screen.
The one-letter command w requires less typing and provides more information.
The w command provides us with the user name which is what we wanted, and a bonus set of data for that user. Note that if there are multiple users logged into the Linux system, the w command will list them all. You’d need to know which terminal the user you were interested in had logged in on. If they’ve logged directly onto the Linux computer itself, that’ll be pts/o, so look for :0 in the output from w .
The w command provides the boot time, uptime and average load for the previous five, ten and fifteen minutes, and the following information regarding the current user.
USER: The user name. TTY: The type of terminal they are logged in at. This will usually be a pts (a pseudo-teletype). :0 means the physical keyboard and screen connected to this computer. FROM: The name of the remote host if this is a remote connection. LOGIN@: The time at which the user logged in. IDLE: Idle time. This shows ?xdm? in the screenshot because we’re running under an X-windows Display Manager, which does not provide that information. JCPU: Joint CPU time, this is the CPU time used by all processes that have been attached to this tty. In other words, the total CPU time of this user in this logged in session. PCPU: Process CPU time, this is the CPU time used by the current process. The current process is named in the WHAT column. WHAT: The command line of this user’s current process.
Now that we know who this user is, we can obtain more information about them. The id command is a good place to start. Type id, a space, the name of the user and press enter.
This gives us their user ID (uid), group ID (gid) and the groups they’re a member of. A less cluttered display of the groups can be obtained by using the groups command.
A nice summary is provided by the finger command. Use apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.
Once you have finger installed, you can use it to display some information about the user in question.
finger dave
On most Linux systems, some of these fields will be blank. The office, full name, and phone numbers are not populated by default. The “No Plan” field refers to an old scheme where you could provide a few notes for whoever was interested, about what you were working on, or planning to do. If you edit the .plan file in your home folder, the contents of that file are appended to the output from finger .
To quickly reveal the name of the logged in user from the GNOME desktop used on Ubuntu and many other Linux distributions, click the system menu in the top-right corner of your screen. The bottom entry in the drop-down menu is the user name. Other Linux desktop environments should show your username in a similarly easy-to-find menu.
That was easy, just one click. But where’s the fun in that?
You don’t get to feel like a digital detective in the same way as you do when you use the Bash shell.