In order to configure and manage permission system admin needs list of all users. There are commands to create user, delete user but it's critical task to list all users in Linux system. Also It is quite necessary for security purposes to make sure that you have the correct amount of users and that you didn't forget to delete some.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux System queries.
In this context, we shall look into how to list users in Linux systems.
User information is stored in /etc/passwd file. It contains one line with username for each user account on system. You can use less or cat command to see file contents:
$ cat /etc/passwd
In the output, you will see that each line has seven fields delimited by colons that contain the following information:
If you want to list only username then you can use awk or cut commands to print only the username:
awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd
Output will be as following:
Output
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
...
...
geoclue
gdm
gnome-initial-setup
sshd
How to List all users using getent command ?
Getent command will read entries from databases. It will list users from both LDAP database and /etc/passwd file.
To get a list of all Linux users type the following command:
$ getent passwd
Output will be same as contents of /etc/passwd file. You can also use awk or cut to print only the username:
getent passwd | awk -F: '{ print $1}'
getent passwd | cut -d: -f1
How to Check If a user exists in the Linux system ?
Now we know how to get list of users. But what if you want to check if a user is exists on current system or not. You can check using grep command and without using grep command.
For example, we want to check that linuxapt user is exists or not then you should issue command as below:
$ getent passwd | grep linuxapt
$ getent passwd linuxapt
Output
linuxapt:x:1001:1001:,,,:/home/linuxapt:/bin/bash
If user will exists in system then it will print details of that user otherwise it will not print anything.
If you wants total number of users exists on your system then you can get it by :
$ getent passwd | wc -l
It will return number of users as following output:
Output
47
How to Differentiate System and Normal Users ?
System users are created when you are installing OS or updating new packages. There is no more difference between system users and normal users.
Normal users are the users created by the root or another user with sudo privileges. Usually, a normal user has a real login shell and a home directory.
Each user has a UID (User ID) either he is system user or normal user. When normal user adds user by using adduser command. User will be added and UID will be assigned automatically with reference of UID_MIN and UID_MAX from file /etc/login.defs.
You can check UID_MIN and UID_MAX by below command:
$ grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
It will show output as below:
Output
UID_MIN 1000
UID_MAX 60000
From the above output, we can see that all the normal users should have unique ID between 1000 and 60000.
Now we will issue command to get list of normal users whose unique ID between 1000 and 60000.
$ getent passwd {1000..60000}
Output
linuxapt:x:1000:1000:LinuxAPT,,,:/home/linuxapt:/bin/bash
demouser:x:1001:1001:,,,:/home/demouser:/bin/bash
To print only usernames type following command:
$ eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1
Output
linuxapt
demouser
How to Get Groups List in Linux ?
You can list all groups in Linux by using the following command:
$ getent group
You can get list all groups with specific user using following command:
$ getent group | grep linuxapt
This article covers how to list users in Linux system and also differentiate difference between normal user and system user. Linux OS is unique because of its multi-user characteristic allowing multiple users on one system, at the same time. However, tracking all users is essential. The /etc/passwd file contains one line for each Linux user account, with seven fields delimited by colons. This is a text file. You can easily list users under Linux using the cat command or other commands such as grep command / egrep command and more. With this same approach, you can use the Linux commands to list all users on all Linux operating system, including Ubuntu, Debian, RHEL, Arch, Fedora, CentOS, and other distros.
To list all users on Linux, use the cat command as follows:
$ cat /etc/passwd