Today, operating systems are managed by central user management systems and local users. Here are the articles on the 2 most common centralized user management tools you'll come across in Linux:
- Configure OpenLDAP Master Slave replication Rocky Linux 8
- Install & configure FreeIPA Server & Client (RHEL/CentOS 7)
Now that we have left the central user management to these applications, let's see how to list users. In this article, we will explain how to list users on Rocky Linux 9.
Different methods to list users in Linux
Let's explain how we list users with 4 different methods.
Method-1: Using Passwd File
In Rocky Linux, as in other linux distributions, the information of local users is in different files. For example, password hash information is in /etc/shadow, group information is in /etc/group. Local users are located in the /etc/passwd file:
You can use a different screen print command (more, less, etc.) than the cat
command:
[foc@rocky9 ~]$ more /etc/passwd [foc@rocky9 ~]$ less /etc/passwd
There are 7 values belonging to the user in the passwd file. These:
- User Name
- Encrypted password (x represents the password is stored (in the shadow file))
- User ID number (UID)
- User’s group ID number (GID)
- User's Full name
- User’s home directory
- User’s login shell (default is bash shell)
When you want to get the user list from this file, it will be enough to get the first value. For a simple list of users, type the command as follows:
[foc@rocky9 ~]$ cat /etc/passwd | awk -F: '{print $1}'
root
bin
daemon
adm
lp
sync
shutdown
...
tss
sssd
sshd
chrony
systemd-oom
foc
faruk
cat command output is clubbed with awk. You can also get the same output using the cut command:
[foc@rocky9 ~]$ cat /etc/passwd | cut -d: -f1 root bin daemon ...
Now let's list the real local users:
[foc@rocky9 ~]$ cat /etc/passwd | grep "bash"| awk -F: '{print $1}' root foc faruk
2 users other than root user can login to the system. You can tell this from /bash/nologin
values.
Method-2: Using getent Command
You can also get the same output with the getent command. The getent
command is used to query a list of all users from the databases configured in the /etc/nsswitch.conf
file:
[foc@rocky9 ~]$ getent passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ...
You might say, what are all these users doing in the system? The vast majority of these users are users of the applications and services installed on the system.
On Linux systems, the ID of normal users is between 1000 and 60000. We can also use this information when listing users:
[foc@rocky9 ~]$ getent passwd {1000..60000} foc:x:1000:1000:foc:/home/foc:/bin/bash faruk:x:1001:1001::/home/faruk:/bin/bash
Method-3: Using compgen Command
Another command we will use to list users is compgen
:
[foc@rocky9 ~]$ compgen -u root bin daemon adm lp foc faruk ...
To get the count of all the available users we can combine compgen with wc as shown below:
# compgen -u | wc -l 50
So currently I have 50 users on my server.
Method-4: List Active Users
So which users are active now? Now let's list users who have an active session in the system:
[foc@rocky9 ~]$ who faruk pts/0 2022-10-15 20:25 (192.168.122.1) foc pts/1 2022-10-14 10:20 (192.168.122.1)
You can also print the usernames of the users logged into the system with the users command:
[foc@rocky9 ~]$ users faruk foc
Summary
When you finished the article, you saw that we have listed users on Linux with more than one method.
You can look at the manual pages of each command we used above, as in the command example below:
[foc@rocky9 ~]$ man users NAME users - print the user names of users currently logged in to the cur‐ rent host
References
unix.stackexchange.com - View list of users