Understanding the /etc/passwd File in Linux

In Linux, the “/etc/passwd” file holds the necessary information about the system as well as normal users. This file contains one record per line, each referring to the user accounts in the system. It is modified only by the administrator or root user via the “sudo” utility.

This guide will provide a detailed understanding of the “/etc/passwd” file in Linux with the following outcomes:

Let’s start with the basic understanding of the “/etc/passwd” file in Linux.

Understand the /etc/passwd File in Linux

In Linux, the “passwd” file contains seven fields separated by a colon. To display the “passwd” file, the “cat” command is utilized by accessing the “etc” directory as below:

$ cat /etc/passwd

The output shows the information of system users in record format.

Let’s discuss the seven fields of the first record:

root:x:0:0:root:/root:/bin/bash

From left to right, the description of the above script is given below:

  • root: The first field represents the username.
  • x: The second field exhibits the password status, and the x symbol shows that the password is stored in the /etc/shadow file in encrypted form.
  • 0: The third field displays the user ID.
  • 0: The fourth field indicates the group ID.
  • root: It keeps the user information, including full name, phone number, and email.
  • /root: It contains the path to the home directory.
  • /bin/bash: It displays the shell for the user.

Permissions of /etc/passwd File

The “ls” command with the “-l” utility is used to display the permission of file:

$ ls -l /etc/passw

The output shows that only “root” users have permission to read and write files.

Detailed View of /etc/passwd File

The “stat” command helps us to display the detailed information of the “/etc/passwd” file. This information contains the file size, path, blocks, and date and time of “Access”, “Modify”, “Change” and “Birth” permission:

$ stat /etc/passwd

The output shows the details of the “passwd” file that is present in the “etc” directory.

Display the Information of the Specific User From /etc/passwd File

To display the information of logged-in users from the “/etc/passwd” file, a script is written below that fetches the record after matching the pattern:

$ user="itslinuxfoss";grep $user /etc/passwd | awk -F: '{print "Username:"$1,"\nPassword:"$2,"\nUID:"$3,"\nGID:"$4,"\nGECOS:"$5,"\nHome directory:"$6,"\nLogin shell:"$7"\n-"}'

In the above script, the “grep” command search out the file “passwd” of “itslinuxfoss” user. The “awk” split the input and display into fields after matching the pattern “Username”, “Password”, “UID”, “GID”, “GECOS”, “Home directory” and “Login shell”.

The output shows the information of “Username”, “Password”, “UID”, “GID”, “GECOS”, “Home directory”, and “Login shell”.

Edit the /etc/passwd File

To modify the “/etc/passwd” file, the “nano” utility is used that enables the “root” user to add, change and remove users which are located on the most left side:

$ sudo nano /etc/passwd

It navigates to the text editor on which the root user can perform any modifications.

This is all about the “/etc/passwd” file.

Conclusion

In Linux, the “/etc/passwd” file holds the information of the system and normal users in a record format. It displays the information of “Username”, “Password”, “UID”, “GID”, “GECOS”, “Homedirectory”, and “Loginshell”. Additionally, users can check permission, detailed view, and visualize the specific user information from the /etc/passwd file in Linux.