users

user managementLinux/Unix
The users command is one of the most frequently used commands in Linux/Unix-like operating systems. users Print the usernames of users currently logged in

Quick Reference

Command Name:

users

Category:

user management

Platform:

Linux/Unix

Basic Usage:

users [options] [arguments]

Common Use Cases

    Syntax

    users [OPTION]... [FILE]

    Options

    Option Description
    --help Display help message and exit
    --version Output version information and exit

    If FILE is not specified, use /var/run/utmp. /var/log/wtmp as FILE is common.

    Examples

    How to Use These Examples

    The examples below show common ways to use the users command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Display all users currently logged in users
    # Display users from a specific utmp file users /var/run/utmp
    # Advanced Examples Advanced
    # Count the number of logged-in users users | wc -w
    # Count unique users (removing duplicates) users | tr ' ' '\n' | sort | uniq | wc -l
    # Check if a specific user is logged in if users | grep -q "johndoe"; then echo "User johndoe is logged in" else echo "User johndoe is not logged in" fi # List all unique users with the number of logins users | tr ' ' '\n' | sort | uniq -c | sort -nr # Monitor user logins every 5 seconds watch -n 5 users # Save the list of logged-in users to a file with timestamp echo "$(date): $(users)" >> logged_in_users.log # Compare with other user information commands echo "users: $(users)" echo "who: $(who | cut -d' ' -f1 | tr '\n' ' ')" echo "w: $(w -h | cut -d' ' -f1 | tr '\n' ' ')" # Use in a backup script to check for active users if [ $(users | wc -w) -gt 0 ]; then echo "Warning: Users are logged in during backup" fi # Create a simple login notification system previous_users=$(users) while true; do current_users=$(users) if [ "$previous_users" != "$current_users" ]; then echo "User login status changed at $(date)" echo "Before: $previous_users" echo "After: $current_users" previous_users=$current_users fi sleep 60 done

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `users` command is a simple yet useful utility in Linux and Unix-like operating systems that displays a compact list of usernames for users currently logged into the system. Unlike more detailed commands like `who` or `w`, `users` provides just the usernames in a space-separated list, making it ideal for quick checks or use in scripts where only the usernames are needed. This command reads the system's utmp file (typically `/var/run/utmp`), which records current login sessions. Each username in the output represents an active login session, so a single user might appear multiple times if they're logged in through multiple terminals or from different locations. **Key Characteristics:** 1. **Simplicity**: The `users` command is extremely simple to use, with minimal options. This makes it quick and convenient for basic user presence checks. 2. **Concise Output**: Unlike `who` or `w`, which provide detailed information about each login session, `users` returns only usernames, making it ideal for scripts or situations where additional information isn't needed. 3. **Multiple Entries**: If a user has multiple login sessions, their username will appear multiple times in the output. This behavior differs from commands like `who | cut -d' ' -f1 | sort | uniq`, which would show each username only once. 4. **Utmp File**: By default, `users` reads the `/var/run/utmp` file, but you can specify an alternative file as an argument. This can be useful for examining historical login data from backup utmp files. **Common Use Cases:** 1. **Quick System Check**: System administrators often use `users` to quickly see who's currently on the system without the verbose output of other commands. 2. **Script Integration**: The simple output format of `users` makes it easy to parse and use in shell scripts for tasks like: - Counting the number of active users - Checking if a specific user is logged in - Monitoring login activity - Creating conditional logic based on user presence 3. **Resource Management**: Scripts that manage system resources might check the current user load to determine appropriate actions. 4. **Security Monitoring**: Security scripts might monitor for unexpected logins or track login patterns over time. **Comparison with Related Commands:** - **`who`**: Provides more detailed information, including terminal, login time, and remote host for each user session. - **`w`**: Even more detailed than `who`, adding information about what each user is doing, including their current process and system load information. - **`last`**: Shows a history of logins rather than just current sessions. - **`whoami`**: Shows only the current user's username, not all logged-in users. The `users` command is part of the GNU coreutils package, which is installed by default on most Linux distributions. While it may seem limited compared to more feature-rich commands, its simplicity and focused functionality make it a valuable tool in the system administrator's toolkit, particularly for quick checks and automated scripts where parsing more complex output would be unnecessary.

    Related Commands

    These commands are frequently used alongside users or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the users command works in different scenarios.

    $ users
    View All Commands