Objective: Get the list of usernames defined in the /etc/passwd
file.
The /etc/passwd
file stores the account information (except the password field) of all local users on a Unix / Linux system. The password is normally stored in the /etc/shadow
file.
The /etc/passwd
file is world-readable. In other words, the file permissions allow anyone to read the file. A sample entry on the /etc/passwd
file looks like this.
1 |
ibrahim:x:1000:50:Mohamed Ibrahim:/home/ibrahim:/bin/bash |
Each account occupies one line and the field entries for every user are separated by a colon (“:”). There are 7 fields per line (or per user) and first field contains the username.
To extract the list of usernames, we can use one of the following commands.
1 |
$ awk -F':' '{print $1}' < /etc/passwd |
1 |
$ sed 's/:.*//' < /etc/passwd |
1 |
$ cut -d":" -f1 < /etc/passwd |
If you need to use grep
, and you have the GNU grep
installed, you can use the following syntax.
1 |
$ grep -oE '^[^:]+' /etc/passwd |