How to List Logged-in Users Using w Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The w command is a built-in Linux utility that allows you to list logged-in users on your system, including additional information about them.

This includes their username, the terminal devices (TTY) they are using, the remote host or IP address they have connected from, the time when they logged in, how long they have been idle, and their activity.

As a SysAdmin, it could be a beneficial tool for quickly monitoring logged-in users and checking the commands or processes they are running, including their load on the system.

In this article, I’ll show you how to use the w command, including its options (with practical examples).

Tutorial Details

Descriptionw
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesw
Internet RequiredNo

Usage of the W Command

The w command takes two arguments: one is the option, and the other is the username. Both arguments are optional.

$ w [OPTION] [USERNAME]

By default, if the username field is empty, it prints all logged-in user information; otherwise, you can pass a username to filter the data for a single user.

The option is used to modify the default behavior of the w command. For example, you can use the “-h” flag to suppress the header, which is often done when working with shell scripts.

When you execute this command without any options, you will be presented with an output similar to this.

w command output

The first line of the output shows the system information, identical to that of the uptime command output.

  • 13:30:05: The current system time when the w command was executed.
  • up 3:40: The system has been up and running for 3:40 minutes.
  • 2 users: There are currently two logged-in users.
  • load average: The number of processes that are either currently being executed by the CPU or are waiting for execution in the last 1 (which is 0.49), 5 (which is 0.22), and 15 (which is 0.12) minutes, respectively.

The second line is a header, after which the list of logged-in users with their information is printed, where the first user’s information is followed by:

  • USER: The name of the logged-in user (in my case, linuxtldr).
  • TTY: The name of the terminal device used by the logged-in user. For graphical sessions, it typically starts with a colon (:) followed by a number. The command line is typically represented as tty followed by a number (in my case, tty2).
  • FROM: The host name or IP address from where the user logged-in. In my case, it’s tty2 that indicates the user have logged in locally.
  • LOGIN@: The time when the user logged-in, in 24-hour format (in my case, it’s 09:52).
  • IDLE: The time since the user interacted with the terminal (in my case, it’s 3:39 minutes).
  • JCPU: The total time for all the processes used by the user terminal (in my case, it’s 0.74s).
  • PCPU: The time used by the user’s current process, which is mentioned in the “WHAT” field (in my case, it’s 0.73s).
  • WHAT: The user’s current process with options and arguments.

The following is a basic example of the w command without any additional flags:

$ w

Output:

Usage of w command

Note that all the information related to the currently logged-in users is pulled from the “/var/run/utmp” file, while process related information is stored in the “/proc” directory.

Now, let’s take a look at some w command examples with available options:

W Command Options

The w command accepts several options; when they are specified, you will get different output. So, let’s start with the first option.

1. Show Output Without Headers

If you wish to implement the w command in a shell script, the header isn’t necessary; instead, you can use the “-h” flag to hide it.

$ w -h

Output:

w command output without a header

2. Enable or Disable the FROM Field

Arch Linux or Fedora systems do not display the “FROM” field, whereas Ubuntu includes it by default in the output, so if you want to toggle the visibility of the “FROM” field in the output, you can use the “-f” flag.

$ w -f

Output:

w command output without the FROM field

3. Display the Output in the Old Style

The “-o” flag will display the output in old style (also known as legacy output), in which the IDLE, JCPU, and PCPU fields will be replaced with blank space for the users that have been idle for less than one minute.

$ w -o

Output:

w command output in old style

4. Display the Output in a Short Style

The “-s” flag will display the output in short style, in which only the USER, TTY, FROM, IDLE, and WHAT fields will be printed while the LOGIN@, JCPU, and PCPU fields will not be printed.

$ w -s

Output:

w command output in short style

5. Display the IP address in the Output

The “-i” flag will show the IP address instead of the terminal name or remote host in the “FROM” field.

$ w -i

Output:

w command output with IP address in the "FROM" field

6. Ignoring the Username

The “-u” flag will return the output without a username for the current process.

$ w -u

Output:

w command output without process username

That was the end of the last example.

Two examples, the “--help” flag (used to display the help section) and the “--version” flag (used to display the version information), are not mentioned as they are self-explanatory.

If you want to know another method of listing all logged-in users, you can check out our article on who, finger, pinky, and last.

So, let’s conclude this article here, as there is nothing left to say, although if you have questions or queries related to this topic, feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.