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.
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.[email protected]
: 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-header |
Print output without the header. |
-u, --no-current |
Ignores username when calculating current process times and load. |
-s, --short |
Print output in the short format. |
-f, --from |
Toggle printing the FROM (remote hostname) field. |
--help |
Display help text. |
-i, --ip-addr |
Replace the hostname in the FROM field with the IP address. |
-V, --version |
Display current command version. |
-o, --old-style |
Print 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
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
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
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
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
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
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
Check for Other w Command Options
Using the --help
option displays all the options available for use with the w
command:
w --help
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.
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/224447.html