How to Determine the Current User Account in Linux

Bash shell on the Unity desktop conceptFatmawati Achmad Zaenuri / Shutterstock.com

If Linux means something, it means choice. You can even perform a simple task, like identifying the current user in several ways. This tutorial will show you how to use some of the fastest 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 becoming too existential, he probably knows himself. Perhaps, but it is also common for people to create additional user accounts to allow family members to have access to the computer. In addition, if you are connected to a remote shell on a server, you may need a quick reminder of the name of the user with whom you are logged on. If you see a connected session in the absence of anyone, how do you identify the current user from the command line?

Let's try the easiest option first. All we have to do is look at the command prompt. By default, Linux distributions have the user name in the prompt. Simple. We do not even have to type something.

User name displayed in the command prompt

If the user has changed his prompt to another format, we must try something else. The who command will give us the information we are looking for.

who

Release of the who command

The output of which gives you the name of the current user, the terminal to which they are connected, the date and the time of their connection. If it is a remote session, it also tells us where they are connected.

In comparison, the whoami command provides a very simple answer:

who am I

Release of the whoami command

You can get the same answer in one word by returning the $ USER environment variable to the screen.

echo $ USER

Use echo to display the user environment variable

The command of a letter w requires less typing and provides more information.

w

Output of the w command

The w command provides us with the name of the user that corresponds to what we were looking for, as well as a set of additional data for that user. Note that if multiple users are connected to the Linux system, the w command will list them all. You must know the terminal on which the user you were interested in was connected. If they have connected directly to the Linux computer itself, it will be pts / o, so look for: 0 in the result of w.

The w command provides the start time, availability, and average load for the previous five, ten, and fifteen minutes, as well as the following information about the current user.

USER: The identifier.
ATS: The type of terminal to which they are connected. This will usually be a pts (a pseudo-teletype). : 0 means the physical keyboard and the screen connected to this computer.
OF: Name of the remote host if it is a remote connection.
S & # 39; @ IDENTIFY: Time when the user is logged on.
IDLE: Time of inactivity. It shows? Xdm? in the screenshot because we are running under an X-windows display manager, which does not provide this information.
JCPU: CPU time joint, it is the CPU time used by all processes that have been attached to this terminal. In other words, the total CPU time of this user in this connected session.
JEPP: 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 get more information about them. The id command is a good starting point. Type id, a space, the name of the user and press enter.

id dave

Output of the id command

This gives us their user ID (uid), group ID (gid) and the groups of which they are members. You can get a less cluttered view of groups by using the groups command.

dave groups

Exit the groups command

A good summary is provided by the finger command. Use apt-get to install this package on your system if you are using Ubuntu or another Debian-based distribution. On other Linux distributions, use the package management tool of your Linux distribution instead.

sudo apt-get install finger

Once you have installed finger, you can use it to display information about the user in question.

dave finger

Output of the finger command

On most Linux systems, some of these fields will be empty. The desktop, the display name and phone numbers are not filled in by default. The "No Plan" field refers to an old scheme in which you could provide notes to anyone who was interested, 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 added to the finger output.

To quickly reveal the name of the logged in user from the GNOME desktop used on Ubuntu and many other Linux distributions, click on the system menu in the upper right corner of your screen. The bottom entry in the drop-down menu is the user name. Other Linux desktop environments should display your user name in a similar, easy-to-find menu.

System menu displaying the user name

It was easy, just a click away. But where is the pleasure in all this?

You do not feel like a digital detective in the same way as when you use the Bash shell.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.