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)
Table of Contents
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.
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.
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.
Leave a Reply