who - Linux


Overview

The who command in Linux displays information about users currently logged into the system. It can show each user’s login name, terminal line, login time, and remote hostname or X display. It is useful for system administrators when monitoring or managing user sessions.

Syntax

The basic syntax of the who command is as follows:

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

The FILE parameter represents the name of the file that contains the login history, usually /var/run/utmp. The ARG1 ARG2 is a placeholder for arguments like am i (or mom likes) which is traditionally used to display information about the current terminal.

Options/Flags

  • -a, --all : Display all information in the output.
  • -d, --dead : Show dead processes.
  • -H, --heading : Print a header line with column names.
  • -l, --login : Display the system login processes.
  • -m : Only print information about the current terminal.
  • -p, --process : Display active processes spawned by init.
  • -q, --count : Display all login names and number of users logged on.
  • -r, --runlevel : Display current runlevel.
  • -t, --time : Display last system clock change.
  • -u, --users : List users logged in, showing their name, terminal line, and login time.

Examples

  1. Basic Usage:

    who
    

    This command lists all users currently logged in, showing their name, terminal, login time, and host.

  2. Show Current User Info:

    who am i
    

    Displays information about your current login session.

  3. List Users and Total Count:

    who -q
    

    Outputs all logged-in user names and the total number of users.

  4. Display Headers and User Info:

    who -H
    

    Prints column headings before the user information details.

Common Issues

  • Incomplete Outputs: If the utmp file is corrupted or not properly updated, who might show inaccurate or incomplete information. Regular maintenance and checks can help mitigate this issue.
  • Permission Denied: Running who with certain options might require root privileges, especially when accessing specific system files.

Integration

The who command can be combined with other commands for enhanced monitoring or automation. For example:

who | grep username | wc -l

This can be used in a script to check how many sessions a particular user has opened.

  • w: Shows who is logged on and what they are doing. More detailed than who.
  • users: Displays the usernames of users currently logged in to the current host.
  • uptime: Tells how long the system has been running, along with the number of users and the system load averages.

For more detailed information or updates on the who command, you can visit the manual pages by typing man who in the terminal or checking the online documentation.