A Linux system can have many users that can log in to the server. The user accounts are stored in the /etc/passwd
file. You can view this file to see what users are able to log in. The easiest way to retrieve the list of users is by viewing the contents of /etc/passwd
text file.
Table of Contents
- View the contents of /etc/passwd file
- Use the ‘awk’ command to display all user accounts in ubuntu
- Create a New User
- Get info about a specific user
- Conclusions
View the contents of /etc/passwd file
If you wish to view the contents of /etc/passwd just type cat
into Terminal. The cat command outputs the full-text contents of the file.
cat /etc/passwd
Each row in the result set corresponds to a user. You can see some useful information about each user. The seven columns are:
- Username
- Encrypted Password
- User ID (UID)
- Group ID (GUID)
- User ID Info (GECOS)
- Home Directory Path
- Command Shell (bash, sh, nologin, etc.)
The first item of each line before the “:” separator is the username.
Use the ‘awk’ command to display all user accounts in ubuntu
Using the ‘awk’ command, we can extract only the first field from the /etc/passwd file. Awk is a powerful text manipulation tool in Linux.
awk -F: '{print $1}' /etc/passwd
This command lists each user’s full name on the system.
Example output of running the awk command above on an Ubuntu server.
$ awk -F: '{print $1}' /etc/passwd root daemon bin sys sync games man lp mail news uucp proxy www-data backup [...]
Create a New User
Apart from listing all users in Ubuntu, it is important to know how to add a new user. To create a new user in Linux, use this command:
To add a new user, type
sudo useradd newusername
This will create a new user record in the /etc/passwd file.
Get info about a specific user
Use the grep
command to search through the /etc/passwd
file to see whether any specific username exists. This is helpful if there are many users in the /etc/passwd
file. This can help check for the existence of a particular user.
grep -i username /etc/passwd
We can obtain information about a particular user called “username” by reading their /etc/passwd file.
Conclusions
This tutorial shows us how to list all the users in a Linux system along with how to add a new user. Mainly how to access the /etc/passwd file and add a new user.