How to use the command 'who' (with examples)
The ‘who’ command in Linux is used to display information about currently logged-in users and related data such as processes and boot time. It provides an overview of the system’s utilization and helps in identifying the active user sessions.
Use case 1: Display the username, line, and time of all currently logged-in sessions
Code:
who
Motivation: This use case is helpful when you want to know who is currently logged in to the system and their corresponding terminal sessions. It provides a quick check to see which users are actively using the system.
Explanation: The command ‘who’ without any options or arguments displays the username, line (terminal device), and time of the currently logged-in users.
Example output:
username1 tty1 2021-09-30 10:00
username2 tty2 2021-09-30 11:30
Use case 2: Display information only for the current terminal session
Code:
who am i
Motivation: When you are logged in to a Linux system using multiple terminals or SSH sessions, you might sometimes want to know the details of the current terminal session you are working on. This command provides information about the current session only.
Explanation: The ‘who am i’ command displays the username, line (terminal device), and time of the current terminal session, excluding information about other logged-in sessions.
Example output:
username1 tty1 2021-09-30 10:00
Use case 3: Display all available information
Code:
who -a
Motivation: In some cases, you might want to retrieve additional information about the users and their sessions, such as idle time and login process ID. This can be useful for system monitoring and troubleshooting purposes.
Explanation: The ‘-a’ option with the ‘who’ command displays all the available information about currently logged-in users, including their login processes, idle time, and the time since their last activity.
Example output:
username1 tty1 2021-09-30 10:00 5432 00:20 1.3 3.2
username2 tty2 2021-09-30 11:30 9856 01:50 2.9 5.1
Use case 4: Display all available information with table headers
Code:
who -a -H
Motivation: When you need to present the output of the ‘who’ command in a structured format, with table headers, you can use this option. It makes the output more readable and provides a clear representation of the displayed information.
Explanation: The ‘-H’ option, along with ‘-a’, adds table headers to the output of the ‘who’ command. This option is useful when you want to export or present the information in a structured manner.
Example output:
USER TTY DATE IDLE PID IDLE TIME COMMAND
username1 tty1 2021-09-30 10:00 00:20 5432 1.3 3.2
username2 tty2 2021-09-30 11:30 01:50 9856 2.9 5.1
Conclusion: The ‘who’ command in Linux is a handy utility for checking the currently logged-in users and their session details. It provides insights into user activity, process information, and idle times. By using different options, you can customize the output according to your specific requirements.