w command in Linux
Introduction
Before we discuss about w command in linux, let's understand-What is w command ?
w is a command-line application that provides information about presently logged-in users and their activities. It also tells you how long the system has been running, the current time, and the average system load.
In this tutorial, we will discuss the w command.
How to Use the w Command
Below is the syntax for w command:
w [OPTIONS] [USER]
The output of w when run without any options or arguments looks like this:
Output
21:41:07 up 12 days, 10:08, 2 users, load average: 0.28, 0.20, 0.10
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.10.0.2 20:59 1.00s 0.02s 0.00s w
vegastack pts/1 10.10.0.8 21:41 7.00s 0.00s 0.00s bash
The first line is identical to the uptime command in terms of information. The following columns are included:
21:41:07- The current time on the system.up 12 days, 10:08- The amount of time the system has been operational.2 users- The total number of users who have logged in.load average: 0.28, 0.20, 0.10- The average system load during the last 1, 5, and 15 minutes. The system load average is a figure that indicates how many jobs are currently executing or waiting for disk I/O. It essentially informs you how busy your system has been throughout the specified time period.
The fields on the second line are as follows:
USER- The logged-in user's name.TTY- The user's preferred terminal's name.FROM- The host name or IP address of the user's login computer.LOGIN@- The time the user first logged in.IDLE- The amount of time since the user last used the terminal. Idle time.JCPU- The total amount of time spent by all processes connected to the tty.PCPU- The amount of time spent on the current task by the user. The one that appears in the WHAT field.WHAT- The current process and options/arguments of the user.
The command then displays a list of all presently logged in users and their associated information.
If you supply the w command one or more user names as arguments, the output is limited to those users:
w vegastack
Output
22:08:55 up 12 days, 10:35, 2 users, load average: 0.00, 0.06, 0.12
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
vegastack pts/1 10.10.0.8 21:41 27:55 0.00s 0.00s bash
w reads the /var/run/utmp file for information about logged-in users.
w Command Options
w allows a number of infrequently used options.
w will not print the header if you provide the -h, --no-header option:
w -h
Only the following data about logged-in users is printed:
Output
root pts/0 10.10.0.2 20:59 1.00s 0.02s 0.00s w -h
vegastack pts/1 10.10.0.8 21:41 7.00s 0.00s 0.00s bash
The FROM field is toggled with the -f, --from option. The default visibility of this field depends on the distribution you're using.
w -f
Output
22:48:39 up 12 days, 11:15, 2 users, load average: 0.03, 0.02, 0.00
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 20:59 5.00s 0.03s 0.01s bash
vegastack pts/1 21:41 1.00s 0.02s 0.00s w -f
The --old-style option instructs w to use the old output style. When the IDLE, JCPU, and PCPU periods are less than one minute, the command prints blank space.
w -o
Output
22:50:33 up 12 days, 11:17, 2 users, load average: 0.14, 0.04, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.10.0.2 20:59 1:59m bash
vegastack pts/1 10.10.0.8 21:41 w -o
The -s, --short option instructs w to output in the short style. The LOGIN@, JCPU, and PCPU fields are not printed when this option is selected.
w -s
Output
22:51:48 up 12 days, 11:18, 2 users, load average: 0.04, 0.03, 0.00
USER TTY FROM IDLE WHAT
root pts/0 10.10.0.2 3:14 bash
vegastack pts/1 10.10.0.8 2.00s w -s
The -i, --ip-addr option tells w to always show the IP address in the FROM column instead of the hostname.
w -i
FAQs on w command in Linux
What information does the w command display?
The w command displays information about the currently logged-in users, including their username, terminal, login time, idle time, JCPU (CPU time used by all processes), PCPU (CPU time used by the current process), and the command they are executing.
Can I use the w command to see who is logged in to a remote machine?
Yes, the w command can display information about users logged in to both local and remote machines if you have proper access to the remote system.
How does the w command calculate idle time?
The idle time displayed by the w command is calculated by subtracting the user's login time from the current time. If a user is active, their idle time will be shown as "tty".
What does the JCPU column represent in the w command output?
The JCPU column indicates the amount of CPU time used by all processes associated with a specific user.
What does the PCPU column represent in the w command output?
The PCPU column represents the percentage of CPU time used by the currently active process.
How can I sort the output of the w command based on specific columns?
To sort the output based on specific columns, you can use the --sort option followed by the column name. For example, w --sort=user will sort the output based on the username.
Can the w command display system load average information?
Yes, the w command displays the system load average at the top of the output. The load average provides information about the average number of processes in the ready-to-run or waiting state over a certain period.
Conclusion
The w command prints system activity and logged-in users information. In your terminal, type man w for more details.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.