1) From "/etc/passwd - Using Cut & Awk Command

A file called "/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 list without un-necessary information by using,

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

The cut is a command for formatting each line of files and display only required output on the console. Basically, the cut command cuts a line and extracts the text. 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
ucartz
...

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, Awk is a utility that enables a system administrator to build commands that define text patterns that need to be searched in a file. Awk is mostly used for post-processing & Pattern Matching.Now let us try some formatting the output, the below prints more than just the login names. awk command print out the 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 well formatted and decorated output as compared to nay other command,

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
...


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
ucartz: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

 

Was this answer helpful? 1 Users Found This Useful (1 Votes)