How to use the command 'users' (with examples)
The ‘users’ command is a utility in Linux and Unix-like operating systems that is used to display a list of currently logged in users. It is a simple and straightforward command that can be handy for system administrators and users who want to know who is currently using the system. The ‘users’ command can also be useful in situations where you want to check the activity on a server or monitor the number of active users.
Use case 1: Print logged in usernames
Code:
users
Motivation: Printing the logged in usernames provides a quick overview of the users who are currently logged into the system. This can be useful for monitoring purposes or for checking if a specific user is currently online.
Explanation: The command ‘users’ is used without any arguments. It simply displays a list of all the currently logged in usernames, separated by spaces.
Example output:
john doe jane
In this example, three users, “john,” “doe,” and “jane,” are currently logged in.
Use case 2: Print logged in usernames according to a given file
Code:
users /var/log/wtmp
Motivation: The ‘users’ command can accept a file as an argument, which allows you to display the logged in usernames based on the contents of that file. This can be helpful in situations where you want to analyze past login activities or if you have a specific log file you want to inspect.
Explanation: In this use case, the ‘users’ command takes the path of the file ‘/var/log/wtmp’ as an argument. The ‘/var/log/wtmp’ file is a system log file that keeps a record of all user login and logout activities.
Example output:
john jane
In this example, the ‘users’ command reads the ‘/var/log/wtmp’ file and displays the logged in usernames, which are “john” and “jane.”
Conclusion:
The ‘users’ command is a handy tool for quickly checking the list of currently logged in users. By providing a file as an argument, it can also be used to analyze past login activities. Whether you need to monitor server activity or investigate user login history, the ‘users’ command has got you covered.