Beginner’s Guide to Using the ‘users’ Command in Linux (with Examples)

As a Linux user, you might sometimes need to find out who is currently logged in to your system. Fortunately, Linux provides a built-in command-line utility called users that makes this task easy. In this guide, we’ll explore the basics of the users command using simple examples. Note that all examples have been tested on Ubuntu 24.04.

Understanding the users Command

The users command outputs the names of users who are currently logged in. Here’s the syntax:

users [FILE]

According to the manual:

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.

For a more 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.

Let’s dive into some practical examples of using the users command.

Example Usage

Q1. How to use the users command?

It’s straightforward—simply run:

users

For instance, this command produced the following output on my system:

himanshu himanshu himanshu

This indicates that the user ‘himanshu’ is logged in three times. Your output might differ based on your logged-in users.

Q2. How to list the number of logged-in users?

If you’re only interested in the count of logged-in users, you can use:

users | wc -w

This command returned ‘3’ on my system, consistent with the previous output.

Q3. How to make users extract info from a specific file?

By default, the users command retrieves information from files like:

`/var/run/utmp' or `/etc/utmp'

To specify a different file, provide the file’s path as an argument, for example:

users /var/log/wtmp

Q4. How to get more info about the users command?

While users doesn’t offer unique command-line options, you can still use the following standard options for additional information:

users --help
users --version

Conclusion

The users command is a straightforward tool that provides the simple yet useful function of listing users currently logged in to a system. Below is a link to the tool’s man page for further reading.

Frequently Asked Questions (FAQ)

What is the ‘users’ command used for in Linux?

The users command is used to list the user accounts that are currently logged in to a Linux system.

Can I find out how many times a user is logged in?

Yes, running the users command will show each session a user has established, so you may see a username listed multiple times if they have multiple active sessions.

How can I specify a different file for the ‘users’ command?

You can specify a different file by passing its path as an argument to the command, e.g., users /path/to/file.

Does the ‘users’ command offer detailed information about each session?

No, users simply lists usernames. For detailed session information, consider using the w or who commands.