Full width home advertisement

Post Page Advertisement [Top]


If Linux means anything, it means choice. You can accomplish even a simple task such as identifying the current user in many ways.

This tutorial will show you how to use some of the fastest and easiest methods.
Why do you need to find the identity of the current user? In many cases, the owner of the computer is the only user, and without being too existential, they probably know themselves. Maybe, but it's also common for people to create additional user accounts to give family members access to the computer.

And if you are connected to an external shell on a server somewhere, you may need a quick reminder of the username you are logged in with. If you see a logged in session without anyone present, how do you identify the current user from the command line?

Let's try the simplest option first. All we need to do is look at the prompt. By default, Linux distributions have the username in the command prompt. Simple. We do not even have to enter anything.

If the user has changed the command prompt to another format, we need to try something else. The 
who command will give us the information we are looking for.
  • who
The output from  who gives you the name of the current user, the terminal they are logged in to, the date and time when they logged in. If there is an external session, it also tells us where they are logged in from.

For comparison whoami command gives a very small answer:
whoami
You can get the same answer in one word by rendering the $USER environment variable to the screen.
echo $USER
The one-letter command w requires less typing and provides more information.
w
The w command gives us the username which is what we wanted and a bonus set of data for that user. Note that if there are multiple users logged in to the Linux system, the w command will list them all. You need to know which terminal the user you were interested in had logged on to. If they have logged in directly to the Linux computer itself, it will be pts / o, so look for: 0 in the output from w.
The w command provides startup time, uptime and average load for the previous five, ten and fifteen minutes, and the following information regarding the current user.
  • USER : Username.
  • TTY : Type of terminal they are logged in to. This will usually be a pts (a pseudo-teletype). : 0 means the physical keyboard and monitor connected to this computer.
  • OFF : The name of the remote host if this is a dial-up connection.
  • LOG IN @ : The time when the user logged in.
  • EMPTY : Inactive time. This shows? Xdm? in the screenshot because we are running under an X-windows Display Manager, which does not provide that information.
  • JCPU : Decrease CPU time, this is the CPU time used by all processes associated with this tty. In other words, the total CPU time of this user in this logged 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 for this user's current process.

Now that we know who this user is, we can get 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.
id dave
This gives us their user ID (uid), group ID (gid) and the groups they are a member of. A less cluttered view of the groups can be achieved by using groups command.
groups dave
A nice summary is given by finger command.
Use apt-getto install this package on your system if you are using Ubuntu or another Debian-based distribution.
Use other Linux distributions instead of the Linux package management tool.
sudo apt-get install finger
Once finger installed, you can use it to view some information about the current user.
finger dave
On most Linux systems, some of these fields will be blank. Office, full name and phone numbers are not filled by default. The field "No plan" refers to an old scheme where you can give a few notes to the person who was interested, about what you worked on or plan to do. If you edit the .plan file in your home directory, the contents of that file will be added 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 at the top right of the screen. The bottom entry in the drop-down menu is the username. Other Linux desktops will display your username in a similar menu that is easy to find.
It was easy, just one click. But where's the fun in that?
You must not feel like a digital detective in the same way as you do when using the Bash shell.

No comments:

Post a Comment

Bottom Ad [Post Page]