How to list users in linux?

How to list users in linux?

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

However, 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 user data is stored in Linux to understand how and why the commands detailed below 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 admmailnewsapache 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.

List All Local 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 awkcommand…

$ 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}'

Leave comment

Your email address will not be published. Required fields are marked with *.