Situatie
Linux distributions normally display the username of the current user in the Terminal as (Username)@ComputerName. If your Linux distribution doesn’t, run “who” or “whoami” in the Terminal to get the username instead. You can use the “w” command to get even more detailed information.
If Linux means anything, it means choice. You can achieve even a simple task like identifying the current user in many ways. This tutorial will show you how to use some of the quickest and easiest methods.
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?
Solutie
Find Your Current User by Looking at the Terminal
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. You can see the username — Dave, in this case — next to “@howtogeek”. Generally this will be displayed YourName@YourPCName
If the user has changed their prompt to some other format we need to try something else.
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.
Use whoamii to Find the Current User
The whoami command will also display the current user.
By comparison to the who, the whoami command provides a very pithy answer. It only displays the username and nothing else.
You can get the same one-word answer by echoing the $USER environment variable to the screen.
Use w to Find the Current User and Extra Information
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.
Leave A Comment?