who Command in Linux: Explanation with Practical Examples

The ‘who’ command is a fundamental part of the GNU coreutils package in the Linux operating system. This command-line utility provides a wealth of information about the current state of your system, including the list of users currently logged in, the current run level, and the time of the last system boot. Mastering the ‘who’ command can significantly enhance your Linux system administration skills.

Understanding the Basic Syntax of the ‘who’ Command

The ‘who’ command follows a simple syntax:

who [OPTION]... [ FILE | ARG1 ARG2 ]

When run without any options or arguments, the ‘who’ command generates a formatted list of all users currently logged into the system. The output contains four fields:

  • The username of the logged-in user.
  • The terminal session associated with the user.
  • The time the user logged in.
  • The hostname or IP address from where the user is logged in.

To display IP addresses instead of hostnames, use the --ips option.

Enhancing Output Readability with Column Headings

For a more organized view, add column headings using the -H (or --heading) option:

who -H

The ‘who’ command pulls information from the /var/run/utmp file. If you want to use a different file, specify the file path as an argument to the command.

Leveraging Non-Option Arguments

The ‘who’ command can accept two non-option arguments. When used with two arguments, the command prints information only about the terminal associated with the current user. The -m option can be used to achieve the same result.

Exploring Additional ‘who’ Command Options

The ‘who’ command supports several options that provide more detailed information. Here are some examples:

  • The -b or --boot option prints the time of the last system boot.
who -b
  • The -d or --dead option lists all the dead processes.
who -d
  • The -r or --runlevel option shows the current run level.
who -r

The -q or --count option displays only the usernames and the number of currently logged-in users.

who -q

The -a or --all option forces ‘who’ to print all information.

who -a

Practical Applications of the ‘who’ Command

The ‘who’ command is a powerful utility for system administration. You can use it to monitor your system’s status, manage resources, and enhance system security.

Counting Logged-in Users

Knowing how many users are logged into your system can be helpful in resource management and planning. The -q or --count option provides this information in a concise manner.

who -q

The output shows the usernames of the logged-in users and the total number of users.

Checking the Last System Boot Time

The time of the last system boot can be crucial for system maintenance and troubleshooting. The -b or --boot option provides this information:

who -b

The output shows the date and time of the last system boot.

Listing Dead Processes

Dead processes can consume system resources and affect performance. The -d or --dead option lists all dead processes, helping you optimize system performance:

who -d

Finding the Current Run Level

The current run level of your system is important for system administration tasks. The -r or --runlevel option provides this information:

who -r

The output shows the current run level and the date and time when the run level was last changed.

Advanced Usage of the ‘who’ Command

The ‘who’ command can be combined with other Linux commands to perform more complex tasks. Here are a few examples:

Using ‘who’ with ‘grep’

The ‘grep’ command can be used with ‘who’ to filter the output based on specific criteria. For example, to find out if a specific user, say ‘admin’, is logged in, you can use:

who | grep 'admin'

This command will return the line from the ‘who’ command output that contains the word ‘admin’.

Using ‘who’ with ‘awk’

The ‘awk’ command is a powerful tool for text processing. When used with ‘who’, it can format the output in a more readable way. For example, to print only the usernames and login times of the logged-in users, you can use:

who | awk '{print $1, $3, $4}'

Using ‘who’ with ‘sort’

The ‘sort’ command can sort the output of the ‘who’ command. For example, to sort the logged-in users by their login time, you can use:

who | sort -k 3

Conclusion

In conclusion, the ‘who’ command is a powerful and versatile tool in the Linux operating system. It provides valuable insights into the current state of your system. Mastering the ‘who’ command ensures your system is secure, optimized, and well-maintained. Remember, the power of Linux lies in its command-line utilities, and the ‘who’ command is a shining example of that.