How to determine the current user account on Linux

If Linux means something, it means choice. There are many ways in which you can complete a simple task such as identifying the current user. This tutorial will show you how to use some of the quickest, easiest methods.

Why do you need to determine the identity of the current user? In many cases, the computer owner is the only user and, without becoming too existential, likely knows himself or herself. Perhaps, but it is also common for users to create additional user accounts to allow family members to access the computer. And if you are connected to a remote 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 with no attendees, how can you identify the current user from the command line?

Let’s try the simplest option first. We just need to look at the command prompt. By default, Linux distributions have the username in the command prompt. Simple. We didn’t even have to type anything.

If the user changed their command prompt to a different format, we’ll have to try something else. the who Command gives us the information we are looking for.


Output the who command

The output of who gives you the name of the current user, the terminal they logged on to, the date and time they logged on. If it’s a remote session, we’ll also be told where he’s logged in from.

In comparison, the whoami Command provides a very concise answer:


Output the whoami command

You can get the same one word answer by repeating that $USER Environment variable on the screen.

                      echo $USER

Using echo to display the user environment variables

The one-letter command w requires less input and provides more information.



Output of the w command

the w Command gives us the username we wanted and a bonus record for that user. Note that if multiple users are logged on to the Linux system, the w Command lists them all. You need to know which terminal the user you are interested in logged on to. If you logged in directly to the Linux machine itself, that’s pts / o, so look for: 0 in the output of w .

the w The command provides the boot time, uptime, and average load for the last five, ten, and fifteen minutes, and the following information about the current user.

  • USER : The username.
  • TTY : The type of terminal you are logged on to. This will usually be a pts (a pseudo teletype). : 0 means the physical keyboard and display associated with this computer.
  • FROM : The name of the remote host if the connection is remote.
  • SIGN [email protected] : The time the user logged in.
  • IDLE : Waiting period. This shows? Xdm? in the screenshot because we are running an X-windows display manager that does not provide this information.
  • JCPU : Shared CPU time, this is the CPU time used by all processes assigned to this TTY. In other words, the total CPU time of this user in this logged on session.
  • PCPU : CPU time of the process, this is the CPU time used by the current process. The current process is named in the WAS 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 him. the id Command is a good place to start. Type id , a space, the name of the user and press Enter.

                      id dave

Output of the id command

This will give us their user ID (uid), group ID (gid) and the groups they are a member of. You can get a less cluttered representation of the groups by using the groups Command.

                      groups dave

Output of the group command

That provides a nice summary finger Command. Use apt-get to install this package on your system if you are using Ubuntu or any other Debian based distribution. For other Linux distributions, use your Linux distribution’s package management tool instead.

                      sudo apt-get install finger

As soon as you have finger installed, you can use it to display some information about that user.

Finger dave

Output of the finger command

On most Linux systems, some of these fields are blank. The office, full name, and phone numbers are left blank by default. The “No plan” field refers to an old scheme where you could give anyone interested a few notes about what you were working on or about to do. If you edit the .plan file in your home folder, the contents of this file will be appended to the output of finger .

To quickly see 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 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 username in a similarly easy-to-find menu.

System menu with user name

It was very easy, just one click. But where is the fun in it?

You don’t feel like a digital detective like you do when using the bash shell.

Linux commands
Files tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processes alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networking netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts