Linux 'users' Command Tutorial for Beginners (with Examples)
Sometimes, while working on the Linux command line, you might want to quickly check which all users are currently logged in to the system. Well, there's a built-in Linux command line utility that lets you do this easily. The tool in question is 'users', and in this tutorial, we will discuss the basics of it using some easy to understand examples.
But before we do that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 16.04 machine.
Linux users command
As already mentioned above, the users command prints in output the names of users who are currently logged in. Following is its syntax:
users [FILE]
And here's how the tool's man page explains it:
Output who is currently logged in according to FILE. If FILE is not specified, use /var/run/utmp. /var/log/wtmp as FILE is common.
The info page for 'users' contains an even detailed explanation:
`users' prints on a single line a blank-separated list of user names of
users currently logged in to the current host. Each user name
corresponds to a login session, so if a user has more than one login
session, that user's name will appear the same number of times in the
output. Synopsis:
users [FILE]
With no FILE argument, `users' extracts its information from a
system-maintained file (often `/var/run/utmp' or `/etc/utmp'). If a
file argument is given, `users' uses that file instead. A common
choice is `/var/log/wtmp'.
An exit status of zero indicates success, and a nonzero value
indicates failure.
Following are some Q&A-styled examples that should give you a good idea on how the users command works.
Q1. How to use users command?
It's very simple - just execute 'users'. Here's an example:
users
On my system, the command produced the following output:
himanshu himanshu himanshu
So that means the user 'himanshu' has logged in three times. Of course, depending upon your case, these names (and their number) could be different.
Q2. How to list number of logged in users?
Just in case you are only interested in the number of users currently logged in, you can use the 'users' command in the following way:
users | wc -w
On my system, the above command produced '3' as output, which is inline with the output shown in the previous section.
Q3. How to make users extract info from a specific file?
BY default, the users command fetches information from files like:
`/var/run/utmp' or `/etc/utmp'
However, if you want, you can make the tool extract information from some other file. In that case, you'll have to pass the file name and path as input to the 'users' command. For example:
users /var/log/wtmp
Q4. How to get more info about 'users' command?
While the users command doesn't offer any exclusive command line option, the standard --help and --version options do exist so that you can get more info about the tool itself.
users -- help
users --version
Conclusion
As you'd have observed, the users command doesn't offer many features - it just sticks to its basic task of providing names of users currently logged in to the system. So in that sense, it's a handy tool. Just in case you need, here's the tool's man page.