How to Use the w Command in Linux with Examples

August 17, 2021

Introduction

Every Linux SysAdmin needs to monitor and manage users, hopefully with ease. This is where the Linux w command can help.

The w command is a built-in tool that allows administrators to view information about users that are currently logged in. This includes their username, where they are logged in from, and what they are currently doing.

In this tutorial, we will go over the w command syntax, break down the details of its output, and give you examples of how you can use it.

How to use the w command in Linux

Prerequisites

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window or command line

w Command in Linux Syntax

The Linux w command is a system utility that displays information about currently logged-in users. It uses the following syntax:

w [options] [username]

Where:

  • [options]: Options that change the way the command behaves.
  • [username]: Entering the name of a specific user only shows information about that particular user in the output.

Using the w command without any additional options produces an output similar to this:

Elements of the w command output

The first line of the output shows system information:

  • System time: The current system time.
  • Up time: How long the system has logged in.
  • Number of users: The number of users currently logged in.
  • Average system load: The average number of jobs running on the system in the last 1, 5, and 15 minutes, respectively.

The second line shows user and process information:

  • USER: The names of currently logged in users.
  • TTY: The name of the terminal the user is logging in from.
  • FROM: The name or IP address of the terminal or host the user is logging in from.
  • LOGIN@: The time the user logged in, in a 24-hour format.
  • IDLE: The time since the user last used the terminal; displays ?xdm? if the user is currently active.
  • JCPU: The total run time of all system processes attached to the user's terminal.
  • PCPU: Elapsed time for the user's current process.
  • WHAT: The name of the user's current process.

Note: You can change the length of the USER (default of 8 characters) and FROM (default 16 characters) fields by setting the PROCPS_USERLEN and PROCPS_FROMLEN environment variables, respectively. To learn more, have a look at our guide on setting environment variables in Linux.

The w command uses the following options:

-h, --no-headerPrint output without the header.
-u, --no-currentIgnores username when calculating current process times and load.
-s, --shortPrint output in the short format.
-f, --fromToggle printing the FROM (remote hostname) field.
--helpDisplay help text.
-i, --ip-addrReplace the hostname in the FROM field with the IP address.
-V, --versionDisplay current command version.
-o, --old-stylePrint old-style output (blank space for idle times shorter than 1 minute).

Note: You can also find information about users and processes in system files. Information on currently logged in users is stored in /var/run/utmp, while /proc contains process information.

w Command in Linux Examples

Combining options with the w command results in different outputs. Here are some of the things you can do with this command:

Display the Short Format

The short output format only displays the USER, TTY, FROM, IDLE, and WHAT fields. To display the short format, use the w command with the -s option:

w -s
Displaying the short format version of the w command output

List the w Command Output Without Printing the Header

If you want to focus on user information, the w command lets you display the output without the header containing system details and field labels. Use the -h option to do this:

w -h
Displaying the wo command output without the header

Ignore Username

Using the -u option allows the w command to ignore usernames when calculating the current process and CPU times:

w -u

Note: Several users need to be logged in to apply the ignore username w command option.

Check the Version of w Command

Check the current version of the w command with:

w -V
w command check version with -V

Display the IP Address in w Command Output

By default, the FROM field displays the name of the terminal or remote host the user is logged in from. Switch over to displaying their IP address with the -i option:

w -i
Displaying the user's IP address in the FROM field

Output Displayed in Old Style

Displaying the output in the old style leaves a blank space under the IDLE, JCPU, and PCPU fields for users that have been idle for less than one minute. Use the -o option to switch the output to the old style:

w -o
Displaying the w command output in old style

Toggle FROM Field

Some Linux distributions, such as Arch Linux, display the w command output without the FROM field by default. Others, like Ubuntu, include the FROM field in the default output.

The -f option shows or hides the FROM field, depending on the system's default output. For example, to hide the FROM field in Ubuntu, use:

w -f
Toggling the FROM field on and off in the w command output

Display User Output

Including a username as an argument with the w command displays the information for that specific user. For instance, if we want the output to show the information for the user phoenixnap:

w phoenixnap
Displaying the w command output for a specific user

Check for Other w Command Options

Using the --help option displays all the options available for use with the w command:

w --help
Using the help option for the w command

Note: For a more comprehensive overview of the w command options, use the man w command. Learn more in our guide to the man command.

Conclusion

After reading this tutorial, you should have a better understanding of the w command, its syntax, and how you can use options to change its output.

For a more comprehensive overview of Linux commands, check out our Linux command cheat sheet.

Was this article helpful?
YesNo
Aleksandar Kovačević
Aleksandar Kovacevic is an aspiring Technical Writer at phoenixNAP. With a background in both design and writing, he aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Use the usermod Command in Linux
March 4, 2021

The usermod command modifies user account details: username, password, home directory location, shell, and more. This tutorial explains and provides examples for the use of the command, along with its options.
Read more
How to Use the who Command in Linux with Examples
February 25, 2021

The who command belongs to the GNU coreutils package in Linux. Its main purpose is to provide system administrators with information about the users who are currently logged into the system.
Read more
How to Use the wall Command in Linux
February 18, 2021

The wall command allows sending terminal messages to all logged-in users at once. This feature is particularly useful in scenarios when system administrators perform system maintenance tasks...
Read more
How to Use Linux dig Command (DNS Lookup)
September 1, 2020

dig (Domain Information Groper) command is a tool for querying DNS name servers. It is a helpful command for diagnosing DNS problems but is also used to display DNS information.
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.