linux: how to list users?…show all users or just local users

Linux doesn’t provide a straight forward command to list all users in the system. You can list users who are currently logged in, or you can find groups that the user belongs to, but really no simple way to list users or to get a comprehensive list of all users in the system or a list of users in a specific group.

However, this being Linux there are several different commands that you can use to list users, if you are willing to use the command line to stitch some commands together. First, you might be better off by understanding how and where the user names and the associated user data is stored in Linux to understand how and why the commands detailed below will work.

/etc/passwd

The /etc/passwd file holds all the necessary information about the local users such as the user id, password info, login, primary group and home directory. But this file also lists the local users to the system including pseudo users such as adm, mail, news, apache etc etc. These are pseudo users that the system uses to run some applications or services and are not real world users with login privileges.

This file however does not include users that are remote to the system but can log in to the system, such as users in external databases like NIS or LDAP.

/etc/group

The /etc/group file holds the information pertaining to the user groups in the system. There is a field in the file that lists the logins of users who belong to that particular group. This field can be useful when listing the users.

Now lets’ see how you might be able to list users in the system.

list all local linux users

First let’s see how we can list all the local users in the system. You can list all the local users by doing a simple cat of the passwd (/etc/passwd) file.

$ cat /etc/passwd

This prints out all the information on the file. If you are just interested in the login names of the users, then you can cut out just the pertinent information. The login name is the first field in each line separated by the colon (:) delimiter.

$ cut -d: -f1 /etc/passwd

You can format your output so that it prints out more than just the login names. In order to print out the full name and home directory along with the login, you can use the awk command…

$ awk -F":" '{print "Login:" $1 "\tName:" $5 "\tHome:" $6}' /etc/passwd

You can substitute the variables ($1, $5 and $6 in the above example command) to print out more (or less) information as needed. You can add more fields to the list as well.

Another command in Linux that essentially does the same is the passwd command. You can get essentially the same information as detailed above by using the passwd command as in the example below.

$ passwd -S -a | cut -d" " -f1

To print out some of other user information such as password change date, minimum age, max age and inactivity period etc, you will need to use the passwd command instead of the passwd file directly.

$ passwd -S -a | awk '{print "Login:"$1 "\tLast Password Change:"$3 "\tInactivity(in days):"$7}'

list users in linux

list only “real” linux users

The above example lists users irrespective of their status or privileges which means it will also contain the pseudo users as described above. If you like to list only “real world” users with login privileges and a home directory which is probably what you meant by “list users“, then you will need to filter the above command output to weed out these pseudo users.

Let’s assume that the real users on the system have a home directory at /home.

$ cat /etc/passwd | grep '/home' | cut -d: -f1

If you happen to have users on multiple partitions such as /h1, /h2 and /h3 ….then you can modify the grep command to filter all of them out using some regular expressions such as in the example below.

$ cat /etc/passwd | grep -E ':/h1|:/h2|:/h3' | cut -d: -f1

Remember that you will need to filter the users using grep before using cut to print only the login names. The above examples will list users that have a home directory, under the assumption that all “real” users will have a home directory.

list all users in linux

The above lists only the local users. If you like to get a list all the users that have access to the system across many database such as NIS, LDAP etc, then the command you can use is getent. You can use the cut, grep and awk commands to modify and format the output as described the previous commands.

$ getent passwd | cut -d: -f1

If you would like to list only logged in users, then you will need to use other linux commands such as who, w or finger.

list all linux users in a group

Again there is no single and simple command to do this. The /etc/group file contains all the group names and the users who are in that group, but may or may not have the users who have the particular group as their primary group. That information is stored in the /etc/passwd file and it uses the group id rather than the group name.

$ grep ^usb /etc/groups | cut -d: -f4

The above command will list users who are in the user group usb, but may not (sometimes it does though, depending on your linux distro or configuration!) include the users who happens to have usb as their primary group. We can find all the users whose primary group is usb by looking at the /etc/passwd file.

We need to get the group id first, from the group file for the group usb, for that we will have to grep the /etc/group and get the third field which is the group id.

$ grep ^usb /etc/group | cut -d: -f3

Now, we can plug this into another grep command that will search the /etc/passwd file. This will print out the users that are in the passwd file with the primary group usb.

$ grep :`grep ^usb /etc/group | cut -d: -f3`: /etc/passwd

Now, merging all of the above commands into a single line command….

$ grep ^usb /etc/group |cut -d: -f4 && grep :`grep ^usb /etc/group | cut -d: -f3`: /etc/passwd | cut -d: -f1

Ok, that is definitely a long command to type out if you do this often. You do have the option of converting this into an alias or a shell script so that you can use it easily and repeatedly without having to retype.

This still might list some users twice if they exist in both the group as well as the passwd files, and the output is comma separated and in multiple lines. You might also want to convert this into a shell script that accepts the group as an argument. I will leave that as an assignment for you.