w Command in Linux with Examples {+Options & Syntax} (2024)

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.

w Command in Linux with Examples {+Options & Syntax} (1)

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:

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
w Command in Linux with Examples {+Options & Syntax} (3)

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
w Command in Linux with Examples {+Options & Syntax} (4)

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 in Linux with Examples {+Options & Syntax} (5)

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
w Command in Linux with Examples {+Options & Syntax} (6)

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
w Command in Linux with Examples {+Options & Syntax} (7)

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
w Command in Linux with Examples {+Options & Syntax} (8)

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
w Command in Linux with Examples {+Options & Syntax} (9)

Check for Other w Command Options

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

w --help
w Command in Linux with Examples {+Options & Syntax} (10)

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.

If you want to learn how to create a communication line between two logged-in users, read our tutorial on write command in Linux. And for a more comprehensive overview of Linux commands, check out our Linux command cheat sheet.

w Command in Linux with Examples {+Options & Syntax} (2024)
Top Articles
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 6093

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.