There are different users in Linux. Some of them are default and some of them are created later. So, how can we list all these Linux users? What are the commands used to list users in Linux? In this lesson, we will learn different ways of Linux user listing.
You can check also Linux file listing lesson. (Linux ls options)
Listing users with /etc/passwd
All users in Linux system are stored in /etc/passwd file. You can list users by opening this file with cat, less, tail commands.
cat /etc/passwd
less /etc/passwd
tail /etc/passwd
root@kali:/home/kali# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
…
You can also use an option to list only some part of these file. For example, to list the last 10 users, you can use “tail -10 /etc/passwd” command.
root@kali:/home/kali# tail -10 /etc/passwd
avahi:x:127:135:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin
saned:x:128:136::/var/lib/saned:/usr/sbin/nologin
inetsim:x:129:138::/var/lib/inetsim:/usr/sbin/nologin
colord:x:130:139:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin
geoclue:x:131:140::/var/lib/geoclue:/usr/sbin/nologin
lightdm:x:132:141:Light Display Manager:/var/lib/lightdm:/bin/false
king-phisher:x:133:142::/var/lib/king-phisher:/usr/sbin/nologin
kali:x:1000:1000:kali,,,:/home/kali:/bin/bash
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
gokhan:x:1001:1001::/home/gokhan:/bin/sh
If you would like to list only the names of the users recorded in /etc/passwd, you can use the below command:
“awk -F’:’ ‘{ print $1}’ /etc/passwd”
root@kali:/home/kali# awk -F':' '{ print $1}' /etc/passwd
root
daemon
bin
sys
sync
games
man
lp
mail
news
..
You can check Linux Cheat Sheet For Useful Common Linux Commands.
Listing Users with getent Command
We can list Linux users with also “getent” command. If we use this command without any filter comman, then it will give all the user information like /etc/passwd.
root@kali:/home/kali# getent passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
…
You can use grep command to get only one user information.
root@kali:/home/kali# getent passwd | grep gokhan
gokhan:x:1001:1001::/home/gokhan:/bin/sh
To list only usernames with getent command, we can use the below command:
“getent passwd | cut -d: -f1”
root@kali:/home/kali# getent passwd | cut -d: -f1
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
….
Linux User Listing with compgen Command
We can use compgen command for linux user listing. For example, we can use -u option with compgen command and list the names of the users in the linux system.
compgen -u
root@kali:/home/kali# compgen -u
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
…
How to Count Users in Linux?
We can count the users in our Linux system. To do this, we can use getent command or compgen command by adding wc -l options.
Firstly, let’s use getent command with wc -l option to list our user.
“getent passwd | wc -l”
root@kali:/home/kali# getent passwd | wc -l
55
Now, let’s use compgen -u command with wc -l option.
“compgen -u | wc -l”
root@kali:/home/kali# compgen -u | wc -l
55
Leave a Reply