6 July 2018
3 mins read

How to List Users using Linux Command Line

In Linux or Unix-like operating system, the ‘/etc/passwd’ file stores all user information. This file contains the normal (regular) and system users.

In this tutorial, I will show you how to list users in Linux using the command line.

The commands that I have listed below would work on all Linux distros like Ubuntu/Centos and Arch Linux.

1) From “/etc/passwd – Using Cut & Awk Command

As mentioned above the file /etc/passwd stores all users list regardless of created account for a human, service associated account or system functional user. Each line of  /etc/passwd is a distinct user. These commands should work on Centos/Ubuntu/Arch and other Linux distros as well.

Use below command to list all user

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:998:User for polkitd:/:/sbin/nologin hacluster:x:189:189:cluster user:/home/hacluster:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin ...

you get the list without un-necessary information by using,

$ cut -d : -f 1 /etc/passwd

Here cut command is used to extract 1st field from the file named ‘/etc/passwd’ using the delimiter (;) and display required output on the console

Remember, cut command needs option otherwise it gives error.

root daemon bin sys sync games man lp mail news uucp proxy www-data backup ....

Let’s try something more,

$ cat /etc/passwd | grep "/home" |cut -d: -f1
syslog administrator linoxide ...

Now what we have done is, we have piped the previous command output to another variable “cut”

$ cut -d: -f1

-d defines delimiter “:”
-f1 display first field of line i.e. username.

Let us try some more formatting using Awk utility. This tool enables a system administrator to build commands that define text patterns that need to be searched in a file.

The following awk command will list all users with full name and home directory along with the login,

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

look at the output, you will see a well-formatted and decorated output.

Login:root Name:root Home:/root Login:daemon Name:daemon Home:/usr/sbin Login:bin Name:bin Home:/bin Login:sys Name:sys Home:/dev Login:sync Name:sync Home:/bin Login:games Name:games Home:/usr/games Login:man Name:man Home:/var/cache/man Login:lp Name:lp Home:/var/spool/lpd Login:mail Name:mail Home:/var/mail Login:gnats Name:Gnats Bug-Reporting System (admin) Home:/var/lib/gnats ...

The following command will list all users including system and regular (normal) users:

$ awk -F: '{print $1}' /etc/passwd
Sample Output $ awk -F: '{print $1}' /etc/passwd root daemon bin sys sync games man lp mail www-data backup list irc gnats testuser

To list all regular user (only)

Linux systems keep ‘system’ accounts below a specific user id, so use awk search for UIDs higher than certian limit.

$ awk -F: '$3 >= 500 {print}' /etc/passwd

2) Getent command

The getent command do the same like cut command we have seen before, getent command fetches entries from databases supported by the Name Service library. If one or more options are provided to command, then only the entries that match the option will be displayed. Otherwise, all entries will be displayed. the syntax for getting end command,

getent [option] [database]

Display all Users

This is same as that of listing users using cat /etc/passwd, if no other options are provided the getent command uses passwd as database reference and lists all users.

$ getent passed
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin saslauth:x:996:76:"Saslauthd user":/run/saslauthd:/sbin/nologin libstoragemgmt:x:995:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin vivek:x:1000:1000:vivek:/home/vivek:/bin/bash vboxadd:x:991:1::/var/run/vboxadd:/bin/false linoxide:x:9999:9999:official:/home/lino_dir:/bin/bash ...

Find all Groups

The below commands list all group without any condition or matches if no other options are provided the getent command uses group as database reference and lists all groups.

$ getent group
root:x:0: bin:x:1: daemon:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: mail:x:12:postfix man:x:15: dialout:x:18: floppy:x:19: haclient:x:189: unbound:x:997: colord:x:996: dip:x:40: usbmuxd:x:113: ...

Search All groups with specific user

$ getent group | grep username

The above command fetches all group, passing username to pipe lists only matching row. The output has a group and username belonging to that group.

The below command is for advanced formatting using awk, cut & sed command together to list all users and users belongs to specific group.

cat /etc/group | awk -F: '{print $1, $3, $4}' | while read group gid members; do members=$members,$(awk -F: "$4 == $gid {print "," $1}" /etc/passwd); echo "$group: $members" | sed 's/,,*/ /g';done
Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

How to Install and Configure DHCP on Ubuntu 18.04

Next Story

How to Use Netcat Commands with Examples

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop