Have you ever been curious about who is logged in to your Linux system? You can always list all the users on your Linux system but not all of them would be logged in all the time.
If you are on a multi-user Linux environment like a Linux server, checking logged in users could be useful and fun at the same time.
In this tutorial, I’ll show you various ways you list logged in users in Linux.
4 Commands to see logged users on Linux
Almost all these commands rely on the data stored in the /var or /proc directory. If you know a little about the directory structure in Linux, you know that these two directories contains data about the running processes on your system.
1. Use w command to see logged in users in Linux
Can it get any simpler than this? Just type a single letter command in the terminal and it will show the currently logged users in Linux.
And here is the output for the w command:
[email protected]:~$ w 09:54:54 up 26 min, 3 users, load average: 0.00, 0.00, 0.00 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 22.214.171.124 09:37 21.00s 0.00s 0.00s -bash abhi pts/1 126.96.36.199 09:47 0.00s 0.00s 0.00s w rohini pts/2 188.8.131.52 09:48 6:13 0.00s 0.00s -bash
Let me explain a few terms in the output of the w command. The same terms would be used in other commands as well.
TTY gives you information about the terminal used to log on. pts means pseudo terminal slave which indicates that the user logged in via SSH connection.
It shows the IP address of the user’s computer and login time. You can also see for how long a user has been idle (slacking at work? :D).
JCPU is the time used by all processes attached to the TTY and PCPU is the time used by the current process running by the user. You can see this current process under the WHAT column.
2. Check who is logged in with who command
Who command is another simple one. Just type who and it will show who is logged on to your Linux system currently.
You can also see the login time and the IP address of the logged on user.
[email protected]:~$ who root pts/0 Aug 6 09:37 (184.108.40.206) abhi pts/1 Aug 6 09:47 (220.127.116.11) rohini pts/2 Aug 6 09:48 (18.104.22.168)
3. Just get logged in users with users command
All the commands you saw so far give you a lot of information about the logged in users. If you are working on a script and want to know just the name of the logged in users, parsing the output of those commands would be an additional and somewhat complicated task.
This is where the users command can help you. This command only outputs the logged in users, nothing else.
[email protected]:~$ users abhi rohini root
4. Using finger command to see logged in users
You may need to install finger command first because not all Linux distributions have it installed by default.
It is available in the universe repository of Ubuntu and you can install it with this command:
sudo apt install finger
Once installed, just type finger in terminal:
And you’ll see who is logged in on your Linux system.
[email protected]:~$ finger Login Name Tty Idle Login Time Office Office Phone abhi Abhishek pts/1 Aug 6 09:47 (22.214.171.124) rohini Rohini Rachita pts/2 13 Aug 6 09:48 (126.96.36.199) root root *pts/0 Aug 6 09:37 (188.8.131.52)
Bonus Tip: see who logged on your system since last reboot
What you saw so far was about the currently logged in users. How would you know if a user logged out?
The last command in Linux gives you information about all the users who logged in to the system since the last reboot. It will also show the log in and log out time of the logged out users.
Here’s the output of the last command which is self-explanatory I believe.
[email protected]:~$ last rohini pts/3 184.108.40.206 Tue Aug 6 10:05 - 10:05 (00:00) rohini pts/2 220.127.116.11 Tue Aug 6 09:48 still logged in abhi pts/1 18.104.22.168 Tue Aug 6 09:47 still logged in root pts/0 22.214.171.124 Tue Aug 6 09:37 still logged in reboot system boot 4.15.0-52-generi Tue Aug 6 09:28 still running wtmp begins Tue Aug 6 09:28:43 2019
I hope this quick tutorial helped you in finding the users currently logged on to your Linux system. If you know some other way to do it, please share your trick with us in the comment section.