Skip to main content

User Identification

w Command Examples

Probably the shortest command in Linux ecosystem, w gives you quick list of logged-in users.

The w command in Linux is used to list logged-in users of your system and also gives you additional information.

This includes their usernames, the terminal devices (TTY) they are using, the remote hosts or IP addresses they have connected from, the times they logged in, how long they have been idle, and what commands or processes they are running.

It provides a quick overview of user activity and system load in a single line of output for each logged-in user.

So in this tutorial, I will walk you through the following to learn how to use the w command:

  • Syntax and available options with the w command
  • Practical examples of the w command

Let's start with the first one.

How to use the w command in Linux

To use the w command, it is important to know the syntax. So here's a simple syntax of the w command:

w [options]

Here,

  • [options]: it is used to modify the default behavior of the w command. For example, using the -s flag will give you a short output.

Now, let's take a look at the available options with the -w command:

Option Description
-h Suppresses the header line from the output.
-u Ignores the username that the command is run under, and shows all other users' information.
-s Uses a shorter format for the output, showing only the user's name, terminal, and the time of their activity.
-f Shows the full hostname for remote connections instead of just the hostname or IP address.
-i Shows the IP address for remote connections instead of the hostname.
-o Uses the old style of output, which does not include the idle time information.
-V Displays the version information.

Practical examples of the w command

In this section, I will walk you through some practical examples of the w command which will not only help you learn more about getting more about this utility.

1. List logged-in users with additional

To list logged-in users, all you have to do is execute the w command without any additional flags:

w
Use w command in Linux

First, let's break the first line which is:

16:52:32 up 19 min,  2 users,  load average: 1.36, 1.31, 1.02

Here,

  • 16:52:32: The current time when the w command was executed.
  • up 19 min: The system has been up and running for 19 minutes.
  • 2 users: There are currently 2 users logged in.
  • load average: You are given 3 load averages for the past 1 minute, 5 minutes, and 15 minutes, respectively. In my case, load averages were:
    • 1.36: This is the load average for the past 1 minute.
    • 1.31: This is the load average for the past 5 minutes.
    • 1.02: This is the load average for the past 15 minutes.

Now, let's address the output with different data fields.

  • USER: This column displays the username of the logged-in user. In my case, the users are kabir and team.
  • TTY: This column shows the terminal device associated with the user's session. For graphical sessions, it typically starts with a colon (:) followed by a number. In this my case, it's :1 for kabir and :2 for team.
  • FROM: This column indicates the hostname or IP address from which the user has logged in. In my case, it's :1 and :2, which indicates that both users have logged in locally.
  • LOGIN@: This column shows the time and date when the user logged in. For the user kabir, the login time is 16:33, and for the user team, it's 16:48.
  • IDLE: This column displays the amount of time the user has been idle or inactive. In my case, it shows ?xdm? for both users, which the desktop environment (GNOME Display Manager or GDM) is handling idle time reporting.
  • JCPU: This column represents the CPU time consumed by all processes attached to the user's session. In my case, both kabir and team have consumed 9:27 (9 minutes and 27 seconds) of CPU time.
  • PCPU: This column shows the CPU time consumed by the current process. For both users, it's 0.00s, which means the current process is not consuming any CPU time at the moment.
  • WHAT: This column displays the command or process that the user is currently running. In my case, both kabir and team are running the same command: /usr/libexec/gdm-x-session --run-script en, which indicates both users are in the GNOME Display Manager (GDM) session.

2. Show output without headers

If you want to implement the w command in script, having headers won't make much sense and there's a way you can remove them.

To remove the header from the w command output, you use the -h flag as shown here:

w -h
Use w command without headers

3. Use short output

If you only want to print the USER, TTY, FROM, IDLE, and WHAT fields, then you can use the -s flag with the w command to get the short output:

w -s
Print short output using the w command in linux

4. Enable/disable the FROM field

Some Linux distributions like Arch will not show the FROM field by default whereas Ubuntu will show it by default.

But the w command lets you disable/enable the FROM field using the -f flag:

w -f

When you use the -f flag, it will hide the FROM field if it was enabled and will show the FROM field if it wasn't shown by default.

Disable or enable FROM field in Ubuntu

5. Show output for a specific user

If you want to check the information of a specific logged-in user, then all you have to do is append the target username to the w command as shown here:

w <username>

For example, if I want to display data of the user kabir, then, I'll use the following command:

w kabir
Print information of a specific user only with the w command

6. Use old-style output

When you use the old style output (also known as legacy output), it will blank the IDLE, JCPU, and PCPU fields for users that have been idle for less than one minute. If users are idle for over a minute, then the JCPU and PCPU fields will be blank.

To use the old-style output, you can use the -o flag as shown here:

w -o
Use old style output  while using the w command

Here are more ways to find logged-in users

If want to find a better alternative to the w command to find logged-in users, then you can refer to our detailed guide on how to see logged-in users in Linux:

How to see Logged in Users in Linux [4 Simple Ways]
Check who is logged in your Linux system with these simple commands. You can also get additional information about logged in users such as their log in time.

Have any suggestions or queries, feel free to leave us a comment.

Satoshi Nakamoto