Table of Contents
Introduction
In Linux, user management is a fundamental part of system administration, especially when it comes to monitoring active user sessions. The users
command is one of the simplest yet effective tools for this purpose. With just a single command, you can instantly view who is logged into your system, making it a valuable utility for Linux administrators.
In this article, we will delve deep into the users
command -its basic syntax, practical use cases, and advanced examples that show how you can automate user session monitoring. Whether you’re a beginner or an experienced administrator, you’ll find useful insights into how this command fits into everyday Linux administration.
What is the users Command in Linux?
The users
command is a Linux utility that displays the usernames of all users currently logged into the system. It fetches this data from the /var/run/utmp
file, which keeps track of all active sessions. This simple tool is invaluable for monitoring who is using the system at any given moment, especially when maintaining or troubleshooting multi-user environments.
Why Use the users Command?
The users
command is useful for:
- Quickly checking which users are currently logged in.
- Monitoring user sessions during system maintenance.
- Tracking user activities for security purposes.
While it seems straightforward, the users
command can be enhanced and combined with other Linux commands to create powerful user management tools.
Basic Syntax of the users Command
The basic syntax for the users
command is extremely simple:
users
When you run this command, it will display a space-separated list of usernames currently logged into the system. No options or additional parameters are required.
Example
$ users
root admin user1
This example shows that three users – root
, admin
, and user1
– are currently logged in.
Practical Use Cases of the users Command
Now, let’s explore some practical use cases where the users
command comes in handy.
Example 1: Basic Usage
To see all currently logged-in users, simply run:
$ users
john mary admin
This provides a quick overview of who is using the system.
Example 2: Combine with the who Command for Detailed Info
If you need more information, like the terminal or login time, you can use the who
command. The who
command provides detailed user session data such as the terminal (pts/
), login time, and remote host IP:
$ who
john pts/0 2023-10-05 09:15 (192.168.1.100)
mary pts/1 2023-10-05 09:20 (192.168.1.101)
admin pts/2 2023-10-05 09:25 (192.168.1.102)
This shows the login times and other session details, which can be useful for system monitoring.
Count the number of logged-in users:
users | wc -w
By piping the output of the users
command to wc -w
, we can count the number of words in the output, which corresponds to the number of logged-in users.
Check if a specific user is logged in:
users | grep <username>
Check if a user is logged in and display a custom message:
if users | grep -q <username>; then echo "<username> is logged in"; else echo "<username> is not logged in"; fi
Advanced Examples with the users Command
The true power of Linux commands comes from combining simple commands with more advanced tools or integrating them into scripts. Let’s look at some advanced examples of using the users
command.
Example 3: Count the Number of Logged-In Users
To count how many users are currently logged into your system, combine the users
command with wc -w
(word count):
$ users | wc -w
3
This counts the total number of words (usernames) output by the users
command, showing that three users are logged in.
Example 4: Search for a Specific User
If you want to check if a specific user, like john
, is logged in, you can use grep
with the users
command:
$ users | grep john
john
If john
is logged in, his name will appear in the output. If he is not logged in, you’ll get no output.
Example 5: Monitor Users Continuously with watch
The watch
command allows you to continuously execute the users
command, refreshing every two seconds by default:
watch users
This command will update the list of logged-in users in real time, which is useful for monitoring systems under heavy use or tracking session changes during maintenance.
Automation and Scripting with the users
Command
The users
command can also be integrated into shell scripts for more complex automation tasks, such as user activity tracking or system monitoring.
Example 6: Create a Shell Script to Alert When Multiple Users Are Logged In
Below is a simple shell script that checks the number of logged-in users and sends a warning if there are more than three users:
#!/bin/bash
logged_in_users=$(users | wc -w)
if [ $logged_in_users -gt 3 ]; then
echo "Warning: More than 3 users are logged in!"
fi
This script is a basic example of automating system monitoring by counting users and triggering an alert based on specific conditions. You can customize this further to send alerts via email or log events.
Additional Commands for User Monitoring
While the users
command is excellent for a quick snapshot of logged-in users, you may need more detailed insights in some scenarios. Here are a few other commands that complement the users
command:
1. The who Command
The who
command offers more comprehensive information about logged-in users, including the terminal, login time, and remote IP:
$ who
2. The w
Command
The w
command displays not only the list of logged-in users but also what they are currently doing. It shows CPU usage, terminal details, and more:
w
Example Output:
09:23:31 up 1:12, 3 users, load average: 0.08, 0.05, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
john pts/1 192.168.1.100 09:21 1:12 0.01s 0.00s bash
mary pts/2 192.168.1.101 09:23 0.02s 0.03s 0.01s vim
admin pts/3 192.168.1.102 09:24 0.03s 0.02s 0.01s sshd
3. The last Command
The last
command displays a log of all user login and logout events. It is useful for auditing and tracking user activity over time:
$ last
john pts/0 192.168.1.100 Thu Oct 5 09:15 still logged in
Frequently Asked Questions (FAQs)
What does the users
command do?
The users
command in Linux shows a list of all users currently logged into the system by fetching data from the /var/run/utmp
file.
Can the users
command show login times or session details?
No, the users
command only displays usernames. For more details like login times, use the who
or w
command.
How do I count the number of logged-in users?
You can count the number of logged-in users by using the users
command combined with wc -w
:
users | wc -w
Can I monitor users in real-time?
Yes, you can use the watch
command with the users
command to monitor logged-in users in real-time:
watch users
Conclusion
The users
command in Linux is a versatile tool for monitoring logged-in users and tracking session activity. Though it provides only basic information, it can be combined with other commands like who
, w
, and last
for more detailed insights. Advanced users can even automate tasks by integrating the users
command into shell scripts to create custom monitoring solutions.
Whether you are just starting with Linux or managing a complex server environment, understanding how to use it efficiently will make you a better system administrator. Explore the commands and examples shared here, and enhance your ability to manage user sessions effectively.. Thank you for reading the DevopsRoles page!