Linux w Command Tutorial for Beginners (5 Examples)

If you are new to Linux system administration, you should be aware of tools that help you in user management. One such utility is w, which shows info about users that are currently logged in to a system, including what they are doing. In this article, we will discuss this tool using some easy to understand examples.

But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.

Linux w command

As already mentioned above, w displays information related to users that are currently logged in. Following is its syntax:

w [options] user [...]

And here's how the tool's man page explains it:

 w displays information about the users currently on the machine, and
their processes. The header shows, in this order, the current time,
how long the system has been running, how many users are currently
logged on, and the system load averages for the past 1, 5, and 15 min?
utes.

The following entries are displayed for each user: login name, the tty
name, the remote host, login time, idle time, JCPU, PCPU, and the com?
mand line of their current process.

The JCPU time is the time used by all processes attached to the tty.
It does not include past background jobs, but does include currently
running background jobs.

The PCPU time is the time used by the current process, named in the
"what" field.

Following are some Q&A-styled examples that should give you a good idea on how the w command works.

Q1. How to use w command?

Basic usage is very easy - all you have to do is to execute 'w' sans any options.

w

Following is the output the 'w' command produced on my system:

How to use w command

For details on what the different fields in the output mean, refer to the introduction section of the tutorial above.

Q2. How to make w command strip header?

If you don't want w to display the header in output, use the -h command line option.

w -h

How to make w command strip header

So you can see the header was stripped off in the output.

Q3. How to make w display output in short format?

If you want w to display output in short format, use the -s option. In this format, the output doesn't contain the login time, JCPU or PCPU times.

w -s

How to make w display output in short format

Q4. How to make w not display the FROM field?

This can be made possible using the -f command line option.

w -f

Here's the output this command produced on my system:

How to make w not display the FROM field

So you can see the FROM field is missing from the output. Note that you can use the -i command line option in case you want w to display IP address instead of hostname for the FROM field.

Q5. How to make w print info for a specific user?

This can be achieved by passing the corresponding user name as input to the w command. For example, if you want w to only display info related to user 'himanshu', run the w command in the following way:

w himanshu

Conclusion

The w command is a useful little tool that doesn't have a steep learning curve. In fact, the utility is easy to understand and use. We've discussed almost all major options here. For more information on the tool, head to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment