Linux List All Users In The System
|Root privileges required||No|
|Estimated completion time||2m|
|Table of contents |
Linux list all users account using the /etc/passwd file
Type any one of the following command:
$ cat /etc/passwd
Each line in the file has seven fields as follows. For example, consider the following line:
vnstat:x:131:137:vnstat daemon. /var/lib/vnstat:/usr/sbin/nologin
- vnstat – The user name or login name.
- x – Encrypted password is stored in the /etc/shadow file.
- 131 – UID (user ID number)
- 137 – Primary GID (group ID number)
- vnstat daemon – GECOS. It may includes user’s full name (or application name, if the account is for a program), building and room number or contact person, office telephone number, home telephone number and any other contact information.
- /var/lib/vnstat – Home directory for the user.
- /usr/sbin/nologin – Login shell for the user. Pathnames of valid login shells comes from the /etc/shells file.
How to list users in Linux using pagers
Of course we can use pagers such as more/less commands as follows to view the /etc/passwd file:
$ more /etc/passwd
$ less /etc/passwd
Fig.01: List users using /etc/passwd
Linux list user names only
To list only usernames type the following awk command:
$ awk -F’:’ ‘< print $1>‘ /etc/passwd
Another option is to use the cut command:
$ cut -d: -f1 /etc/passwd
Get a list of all users using the getent command
To get a list of all Linux users you can type the following getent command:
$ getent passwd
$ getent passwd | grep tom
## get a list all users ##
$ getent passwd | cut -d: -f1
## count all user accounts using the wc ##
$ getent passwd | wc -l
One can use the compgen command on Linux to list users and other resources too:
$ compgen -u
Find out whether a user account exists in the Linux server
We can use above commands to see whether a user exists in the Linux machine as follows using the grep command:
compgen -u | grep vivek getent passwd | grep -q sai && echo “User sai found” || echo “User sai not found” compgen -u | grep -q ram && echo “User ram found” || echo “User ram not found”
A simplified command would be:
How to count user accounts in the Linux server
Want to get user accounts count on your system? Try the wc command as follows:
$ compgen -u | wc -l
$ getent passwd | wc -l
A Note About System and General Users
Each user has numerical user ID called UID. It is defined in /etc/passwd file. The UID for each user is automatically selected using /etc/login.defs file when you use useradd command. To see current value, enter:
$ grep “^UID_MIN” /etc/login.defs
$ grep UID_MIN /etc/login.defs
1000 is minimum values for automatic uid selection in useradd command. In other words all normal system users must have UID >= 1000 and only those users are allowed to login into system if shell is bash/csh/tcsh/ksh etc as defined /etc/shells file. Type the following command to list all login users:
How to List Users in Linux
Updated Apr 12, 2020
Have you ever wanted to list all users in your Linux system or to count the number of users in the system? There are commands to create a user, delete a user, list logged in users, but what is the command to list all users in Linux?
This tutorial will show you how to list users in Linux systems.
Get a List of All Users using the /etc/passwd File #
Local user information is stored in the /etc/passwd file. Each line in this file represents login information for one user. To open the file you can either use cat or less :
Each line in the file has seven fields delimited by colons that contain the following information:
- User name.
- Encrypted password ( x means that the password is stored in the /etc/shadow file).
- User ID number (UID).
- User’s group ID number (GID).
- Full name of the user (GECOS).
- User home directory.
- Login shell (defaults to /bin/bash ).
If you want to display only the username you can use either awk or cut commands to print only the first field containing the username:
Get a List of all Users using the getent Command #
The getent command displays entries from databases configured in /etc/nsswitch.conf file, including the passwd database, which can be used to query a list of all users.
To get a list of all Linux userr, enter the following command:
As you can see, the output is the same as when displaying the content of the /etc/passwd file. If you are using LDAP for user authentication, the getent will display all Linux users from both /etc/passwd file and LDAP database.
You can also use awk or cut to print only the first field containing the username:
Check whether a user exists in the Linux system #
Now that we know how to list all users, to check whether a user exists in our Linux box we, can simply filter the users’ list by piping the list to the grep command.
For example, to find out if a user with name jack exists in our Linux system we can use the following command:
If the user exists, the command above will print the user’s login information. No output that means the user doesn’t exist.
We can also check whether a user exists without using the grep command as shown below:
Same as before, if the user exists, the command will display the user’s login information.
If you want to find out how many users accounts you have on your system, pipe the getent passwd output to the wc command:
As you can see from the output above, my Linux system has 33 user accounts.
System and Normal Users #
There is no real technical difference between the system and regular (normal) users. Typically system users are created when installing the OS and new packages. In some cases, you can create a system user that will be used by some applications.
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 numeric user ID called UID. If not specified when creating a new user with the useradd command, the UID will be automatically selected from the /etc/login.defs file depending on the UID_MIN and UID_MIN values.
To check the UID_MIN and UID_MIN values on your system, you can use the following command:
From the output above, we can see that all normal users should have a UID between 1000 and 60000. Knowing the minimal and maximal value allow us to query a list of all normal users in our system.
The command below will list all normal users in our Linux system:
Your system UID_MIN and UID_MIN values may be different so the more generic version of the command above would be:
If you want to print only the usernames just pipe the output to the cut command:
In this tutorial, you learned how to list and filter users in your Linux system and what are the main differences between system and normal Linux users.
The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint.
Feel free to leave a comment if you have any questions.
A command to list all users? And how to add, delete, modify users?
I need a command to list all users in terminal. And how to add, delete, modify users from terminal.
That could help in administrating your accounts easily by terminal.
9 Answers 9
To list all local users you can use:
To list all users capable of authenticating (in some way), including non-local, see this reply.
Some more useful user-management commands (also limited to local users):
To add a new user you can use:
To remove/delete a user, first you can use:
Then you may want to delete the home directory for the deleted user account :
Please use with caution the above command!
To modify the username of a user:
To change the password for a user:
To change the shell for a user:
To change the details for a user (for example real name):
To add a user to the sudo group:
And, of course, see also: man adduser , man useradd , man userdel . and so on.
Just press Ctrl + Alt + T on your keyboard to open Terminal. When it opens, run the command(s) below:
You can also use awk: awk
The easiest way to get this kind of information is getent – see manpage for the getent command . While that command gives the same output as cat /etc/passwd it is useful to remember because it will give you lists of several elements in the OS.
To get a list of all users you type (as users are listed in /etc/passwd )
To add a user newuser to the system you would type
to create a user that has all default settings applied.
Bonus: To add any user (for instance anyuser) to a group (for instance cdrom) type
You delete a user (for instance obsolete) with
If you want to delete his home directory/mails as well you type
will remove the user and all files owned by this user on the whole system.
You can use compgen GNU bash built-in too:
Will lists all users.
This should get, under most normal situations, all normal (non-system, not weird, etc) users:
- reading in from /etc/passwd
- using : as a delimiter
- if the third field (the User ID number) is larger than 1000 and not 65534, the first field (the username of the user) is printed.
This is because on many linux systems, usernames above 1000 are reserved for unprivileged (you could say normal) users. Some info on this here:
A user ID (UID) is a unique positive integer assigned by a Unix-like operating system to each user. Each user is identified to the system by its UID, and user names are generally used only as an interface for humans.
UIDs are stored, along with their corresponding user names and other user-specific information, in the /etc/passwd file.
The third field contains the UID, and the fourth field contains the group ID (GID), which by default is equal to the UID for all ordinary users.
In the Linux kernels 2.4 and above, UIDs are unsigned 32-bit integers that can represent values from zero to 4,294,967,296. However, it is advisable to use values only up to 65,534 in order to maintain compatibility with systems using older kernels or filesystems that can only accommodate 16-bit UIDs.
The UID of 0 has a special role: it is always the root account (i.e., the omnipotent administrative user). Although the user name can be changed on this account and additional accounts can be created with the same UID, neither action is wise from a security point of view.
The UID 65534 is commonly reserved for nobody, a user with no system privileges, as opposed to an ordinary (i.e., non-privileged) user. This UID is often used for individuals accessing the system remotely via FTP (file transfer protocol) or HTTP (hypertext transfer protocol).
UIDs 1 through 99 are traditionally reserved for special system users (sometimes called pseudo-users), such as wheel, daemon, lp, operator, news, mail, etc. These users are administrators who do not need total root powers, but who perform some administrative tasks and thus need more privileges than those given to ordinary users.
Some Linux distributions (i.e., versions) begin UIDs for non-privileged users at 100. Others, such as Red Hat, begin them at 500, and still others, such Debian, start them at 1000. Because of the differences among distributions, manual intervention can be necessary if multiple distributions are used in a network in an organization.
Also, it can be convenient to reserve a block of UIDs for local users, such as 1000 through 9999, and another block for remote users (i.e., users elsewhere on the network), such as 10000 to 65534. The important thing is to decide on a scheme and adhere to it.
Among the advantages of this practice of reserving blocks of numbers for particular types of users is that it makes it more convenient to search through system logs for suspicious user activity.
Contrary to popular belief, it is not necessary that each entry in the UID field be unique. However, non-unique UIDs can cause security problems, and thus UIDs should be kept unique across the entire organization. Likewise, recycling of UIDs from former users should be avoided for as long as possible.