TranslateProject/sources/tech/20230102.1 ⭐️ who Command in Linux Explanation with Examples.md

4.3 KiB
Raw Blame History

who Command in Linux: Explanation with Examples

Heres a beginners guide on understanding who command in Linux with several examples.

This article is part of the Linux command learning series.

who command

The “who” command in Linux is used to display information about users who are currently logged in to the system. It shows the users login name, the terminal from which the user is logged in, the time at which the user logged in, and the remote hostname (if applicable).

Syntax

Here is the basic syntax for the “who” command:

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

Example of various who command and switches

By default, “who” reads the file /var/run/utmp, which contains information about users who are currently logged in. If no options are specified, it displays each users login name, terminal, and time of login.

who

It gives the following output. As you can see it shows the login name=debugpoint, terminal id tty2 and the date and time of the login.

debugpoint tty2 2023-01-01 11:22 (tty2)

who command - default example

However, if you run the above command in a guest virtual machine, you should see the same but the terminal id would be the x11 server display name i.e. :0.

 whodebugpoint :0 2023-01-01 23:36 (:0)

To show the username of the current user and information, use below:

whoami

View the last system boot time using the -b option:

 who -bsystem boot 2023-01-01 23:36

Display the count of users logged in the current system:

 who -qdebugpointusers=1

All the above command when paired with -H option, you get a better info with a header line, like below:

who -H

NAME LINE TIME COMMENT
debugpoint tty2 2023-01-01 11:22 (tty2)

If you want to display all the information related to who command in Linux, use the option -a:

who -aH

NAME LINE TIME IDLE PID COMMENT EXIT
system boot 2023-01-01 11:19
run-level 5 2023-01-01 11:19
debugpoint + tty2 2023-01-01 11:22 13:26 2042 (tty2)

As always, you can save the output of the who command to any file using the redirect as below:

who > user_details.txt

Example summary of who command options

Here are some who command examples and their explanation:

Here are some options that can be used with the “who” command:

  • -a: Display the hostname, time of login, and processes for each user
  • -b: Display the time of the last system boot
  • -d: Display dead processes (processes that have terminated but have not been removed from the utmp file)
  • -H: Display a header line
  • -l: Display login processes in long format
  • -m: Display only the name and line of the user who is logged in on the terminal specified by ARG1 ARG2
  • -q: Display a count of logged in users
  • -u: Display information about users who have processes that are not detached
  • -w: Display information about users who are logged in, in the same format as the utmp file

Closing Notes

I hope this article helps you to understand who command and its basics. You can also read the who man pages to learn more. Let me know if you have any questions.

This article is part of the Linux command learning series.

Next:How to Force Auto Dark Mode in Chrome and Chromium

Using Mastodon? Follow us at floss.social/@debugpoint


via: https://www.debugpoint.com/who-command-linux/

作者:Arindam 选题:lkxed 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出