As a Linux system administrator, you may come across situations when you want to know what other users are there.
You may want to know the exact username to give them sudo access or reset their password.
You may simply want to see if there are other users that might not be needed anymore. To remove users, you need to know the usernames.
Reasons could be plenty, and so do the methods for listing users in Linux. My favorite is this one:
compgen -u
It only gives you the list of usernames. If you are looking for 'human users', look at the bottom of the list.
But let's see more ways of listing users in a Linux based system.
Use the content of /etc/passwd file
The /etc/passswd file is crucial for user management. It defines what groups a user belongs to, whether a password is set for it and more.
Use the cat command to display the user details from this file:
cat /etc/passwd
You'll see a huge output listing all the users:
Each line represents a Linux user (system user or human user) in the following format:
abhishek:x:1000:1000:abhishek,,,:/home/abhishek:/bin/bash
The colon separated fields represent the following in the specific order:
- username
- Password (x means password is set for the user)
- User ID (UID)
- Group ID (GID)
- Full name, room number, phone number etc (optional things that were used once upon a time with the useradd command)
- Home directory of the user
- Default login shell
Which ones are the normal users?
Among the so many users, the real, human users are usually at the bottom with a user ID number greater than 500 (Red Hat systems) or 1000 (Debian and Ubuntu).
The system users are created for different purpose and they usually have a nologin or false in their default login shell field.
If you just want to list normal users (human ones), you could try filtering the output with the help of the grep and cut commands:
grep -vE "nologin|false" /etc/passwd | cut -d: -f1
And then you should see less output that will help you identify the
abhishek@itsfoss:~$ grep -vE "nologin|false" /etc/passwd | cut -d: -f1
root
sync
abhishek
prakash
You may also use awk command for this filtering purpose:
cat /etc/passwd | awk -F: '{print $1}'
It may still have some system users but it reduces the noise considerably to list regular user accounts.
Use getent command to list Linux users
The getent command displays entries from Name Service Switch libraries supported databases. The passwd database is one of them.
getent passwd
Basically, this command uses the same /etc/passwd file that you saw in the previous section. That's the reason why the output is pretty much the same as the content of the /etc/passwd database file.
And as previously, you can combine it with grep and cut command :
getent passwd | grep -vE "nologin|false" | cut -d: -f1
You can use the getent
command to display all the users with sudo access. The group
option displays the users from a specific group.
getent group sudo
Use compgen command to list only the usernames
A shell-builtin, it displays possible completions depending on the options.
To display only usernames, use the option `-u`:
compgen -u
It will give you only the usernames of both system and human users:
It is up to you to identify the normal user which could be difficult without the user and group ID. They are usually at the bottom of the list.
Check if a user exists on the system
You may not always want to list all the users. Sometimes you just want to know if a specific user exists on the system or not.
There is no specific command for that. Just use the grep command to filter the content of the /etc/passwd file for the particular user like this:
grep -i user_name /etc/passwd
Bonus: List currently logged in users in Linux
If you want to list all the connected users at present, you can use the users command or who command.
users
The who command gives you details
who
Conclusion
Here, you saw various ways of displaying users on Linux. Since you are using the command line, the methods will work on any Linux distribution. A handy trick for the system administrators.
Desktop Linux users can go to System settings and then to User settings. All the regular users are displayed there.
Please let me know if you have any further questions.