- 10 ‘who’ Command Examples for Linux Newbies
- How to see Logged in Users in Linux
- 4 Commands to see logged users on Linux
- 1. Use w command to see logged in users in Linux
- 2. Check who is logged in with who command
- 3. Just get logged in users with users command
- 4. Using finger command to see logged in users
- Bonus Tip: see who logged on your system since last reboot
- 4 Ways to Identify Who is Logged-In on Your Linux System
- 1. Get the running processes of logged-in user using w
- 2. Get the user name and process of logged in user using who and users command
- Users Command
- 3. Get the username you are currently logged in using whoami
- 4. Get the user login history at any time
10 ‘who’ Command Examples for Linux Newbies
In our earlier article, we have explained 11 ways to find user account info and login details in Linux. One of the various commands we mentioned was the who command which displays users who are currently logged on to a Linux system, including the terminals they are connecting from.
This article will explain some useful examples of who command for Linux newbies.
The basic syntax for using who command is as follows.
$ who who [OPTION]. [ FILE | ARG1 ARG2 ]
1. If you run who command without any arguments, it will display account information (user login name, user’s terminal, time of login as well as the host the user is logged in from) on your system similar to the one shown in the following output.
$ who ravi tty1 2018-03-16 19:27 tecmint pts/0 2018-03-16 19:26 (192.168.56.1) root pts/1 2018-03-16 19:27 (192.168.56.1)
2. To print the heading of the columns displayed, use the -H flag as shown.
$ who -H NAME LINE TIME COMMENT ravi tty1 2018-03-16 19:27 tecmint pts/0 2018-03-16 19:26 (192.168.56.1) root pts/1 2018-03-16 19:27 (192.168.56.1)
3. To print the login names and total number of logged on users, use the -q flag.
$ who -q ravi tecmint root # users=3
4. In case you want to show only hostname and user associated with stdin, use the -m switch.
$ who -m tecmint pts/0 2018-03-16 19:26 (192.168.56.1)
5. Next, to add user’s message status as + , — or ? , use the -T option.
$ who -T ravi + tty1 2018-03-16 19:27 tecmint + pts/0 2018-03-16 19:26 (192.168.56.1) root + pts/1 2018-03-16 19:27 (192.168.56.1)
The who command also helps you to view some useful system information such as last boot time, current runlevel (target under systemd), print dead processes as well as processes spawned by init.
6. To view the time of last system boot, use the -b flag and adding the -u option allows for listing of logged on users in the same output.
$ who -b system boot 2018-01-19 02:39
$ who -bu system boot 2018-03-16 19:25 ravi tty1 2018-03-16 19:27 00:33 2366 tecmint pts/0 2018-03-16 19:26 . 2332 (192.168.56.1) root pts/1 2018-03-16 19:27 00:32 2423 (192.168.56.1)
7. You can check the current runlevel with the -r option.
$ who -r run-level 3 2018-03-16 02:39
8. The following command will print dead processes.
$ who -d pts/1 2018-03-16 11:10 9986 term=0 exit=0
9. Furthermore, to see active processes spawned by init, use the -p option.
10. Last but not least, the -a flag allows for printing of default output combined with information from some of the options we have covered.
$ who -a system boot 2018-06-16 02:39 run-level 3 2018-01-19 02:39 LOGIN tty1 2018-01-19 02:39 3258 ttyS0 2018-01-19 02:39 3259 + pts/0 2018-03-16 05:33 . 20678 (208.snat-111-91-115.hns.net.in) pts/1 2018-03-14 11:10 9986 term=0 exit=0
You can find more options by consulting the who man page.
In this article, we have explained 10 who command examples for Linux newbies. Use the comment section below to ask any questions or give us your feedback.
How to see Logged in Users in Linux
Check who is logged in your Linux system with these simple commands. You can also get additional information about logged in users such as their log in time.
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 202.91.87.114 09:37 21.00s 0.00s 0.00s -bash abhi pts/1 202.91.87.114 09:47 0.00s 0.00s 0.00s w rohini pts/2 157.43.53.142 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 (202.91.87.114) abhi pts/1 Aug 6 09:47 (202.91.87.114) rohini pts/2 Aug 6 09:48 (157.43.53.142)
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.
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:
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 (202.91.87.114) rohini Rohini Rachita pts/2 13 Aug 6 09:48 (157.43.53.142) root root *pts/0 Aug 6 09:37 (202.91.87.114)
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 157.43.53.142 Tue Aug 6 10:05 - 10:05 (00:00) rohini pts/2 157.43.53.142 Tue Aug 6 09:48 still logged in abhi pts/1 202.91.87.114 Tue Aug 6 09:47 still logged in root pts/0 202.91.87.114 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.
4 Ways to Identify Who is Logged-In on Your Linux System
This article is written by Hari Haran.
As a system administrator, you may want to know who is on the system at any give point in time. You may also want to know what they are doing. In this article let us review 4 different methods to identify who is on your Linux system.
1. Get the running processes of logged-in user using w
- Name of the user
- User’s machine number or tty number
- Remote machine address
- User’s Login time
- Idle time (not usable time)
- Time used by all processes attached to the tty (JCPU time)
- Time used by the current process (PCPU time)
- Command currently getting executed by the users
Following options can be used for the w command:
- -h Ignore the header information
- -u Display the load average (uptime output)
- -s Remove the JCPU, PCPU, and login time.
$ w 23:04:27 up 29 days, 7:51, 3 users, load average: 0.04, 0.06, 0.02 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ramesh pts/0 dev-db-server 22:57 8.00s 0.05s 0.01s sshd: ramesh [priv] jason pts/1 dev-db-server 23:01 2:53 0.01s 0.01s -bash john pts/2 dev-db-server 23:04 0.00s 0.00s 0.00s w $ w -h ramesh pts/0 dev-db-server 22:57 17:43 2.52s 0.01s sshd: ramesh [priv] jason pts/1 dev-db-server 23:01 20:28 0.01s 0.01s -bash john pts/2 dev-db-server 23:04 0.00s 0.03s 0.00s w -h $ w -u 23:22:06 up 29 days, 8:08, 3 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ramesh pts/0 dev-db-server 22:57 17:47 2.52s 2.49s top jason pts/1 dev-db-server 23:01 20:32 0.01s 0.01s -bash john pts/2 dev-db-server 23:04 0.00s 0.03s 0.00s w -u $ w -s 23:22:10 up 29 days, 8:08, 3 users, load average: 0.00, 0.00, 0.00 USER TTY FROM IDLE WHAT ramesh pts/0 dev-db-server 17:51 sshd: ramesh [priv] jason pts/1 dev-db-server 20:36 -bash john pts/2 dev-db-server 1.00s w -s
2. Get the user name and process of logged in user using who and users command
who command is used to get the list of the usernames who are currently logged in. Output of the who command contains the following columns: user name, tty number, date and time, machine address.
$ who ramesh pts/0 2009-03-28 22:57 (dev-db-server) jason pts/1 2009-03-28 23:01 (dev-db-server) john pts/2 2009-03-28 23:04 (dev-db-server)
To get a list of all usernames that are currently logged in, use the following:
$ who | cut -d' ' -f1 | sort | uniq john jason ramesh
Users Command
users command is used to print the user name who are all currently logged in the current host. It is one of the command don’t have any option other than help and version. If the user using, ‘n’ number of terminals, the user name will shown in ‘n’ number of time in the output.
$ users john jason ramesh
3. Get the username you are currently logged in using whoami
whoami command is used to print the loggedin user name.
$ whoami john
whoami command gives the same output as id -un as shown below:
$ id -un john
who am i command will display the logged-in user name and current tty details. The output of this command contains the following columns: logged-in user name, tty name, current time with date and ip-address from where this users initiated the connection.
$ who am i john pts/2 2009-03-28 23:04 (dev-db-server) $ who mom likes john pts/2 2009-03-28 23:04 (dev-db-server) Warning: Don't try "who mom hates" command.
Also, if you do su to some other user, this command will give the information about the logged in user name details.
4. Get the user login history at any time
last command will give login history for a specific username. If we don’t give any argument for this command, it will list login history for all users. By default this information will read from /var/log/wtmp file. The output of this command contains the following columns:
- User name
- Tty device number
- Login date and time
- Logout time
- Total working time
$ last jason jason pts/0 dev-db-server Fri Mar 27 22:57 still logged in jason pts/0 dev-db-server Fri Mar 27 22:09 - 22:54 (00:45) jason pts/0 dev-db-server Wed Mar 25 19:58 - 22:26 (02:28) jason pts/1 dev-db-server Mon Mar 16 20:10 - 21:44 (01:33) jason pts/0 192.168.201.11 Fri Mar 13 08:35 - 16:46 (08:11) jason pts/1 192.168.201.12 Thu Mar 12 09:03 - 09:19 (00:15) jason pts/0 dev-db-server Wed Mar 11 20:11 - 20:50 (00:39
This article is written by Hari Haran. He is working in bk Systems (p) Ltd, and interested in contributing to the open source. The Geek Stuff welcomes your tips and guest articles.