The /etc/passwd file is the most important file in Linux operating system. This file stores essential information about the users on the system. This file is owned by the root user và to edit this file we must have root privileges. But try to lớn avoid edit this file. Now let’s see actually how this tệp tin look
This file contains one entry per line. That means it stores one user’s information on one line. The user information contains seven fields and each field is separated by the colon ( : )symbol. Each entry in the /etc/passwd file looks like this:
Now let’s understand each field one by one:Username: This field stores the usernames which are used while login into the system.
Bạn đang xem: Understanding The /Etc/Passwd File
The length of this field is between 1 và 32 characters.Password: This field store the password of the user. The x character indicates the password is stored in /etc/shadow tệp tin in the encrypted format. We can use the passwd command to lớn update this field.User ID(UID): User identifier is the number assigned to each user by the operating system khổng lồ refer the users. The 0 UID is reserved for the root user. And 1-99 UID are reserved for other predefined accounts. & 100-999 are reserved by the system for administrative and system accounts/groups.Group ID(GID): Group identifier is the number indicating the primary group of users. Most of the time it is the same as the UID.Home directory: This field contains the absolute path of the user’s home directory. By default, the users are created under the /home directory. If this file is empty, then the home directory of that user will be /Login shell: This field store the absolute path of the user shell. This shell is started when the user is log in khổng lồ the system.
Now we have understood the tệp tin structure of the /etc/passwd file now let’s see one example of this file. You can view the nội dung of file using the cat file like:
cat /etc/passwd
We can see that there are many users with all information.
To tìm kiếm for a specific user, we can use the grep command. Now for example to lớn get information about the user Nishant we can use the following command:
grep nishant /etc/passwd
Check /etc/passwd file permission
The normal users have only read permissions to lớn the /etc/passwd file. The only root user can write into this file. To lớn see the permissions of /etc/passwd file, we can use the ls command as follows:
ls -l /etc/passwd
The output đầu ra will be
We can see that the permissions of the tệp tin /etc/passwd are rw-r–r–. This means the root user has read và write access & other groups và user have read-only access khổng lồ the file.
To get more details like size, modify the time of this file we can use the stat command:
stat /etc/passwd
Reading /etc/passwd file:
We can read the /etc/passwd file more user-friendly by using the while loop và IFS separator. A while loop is used lớn iterate through the file, and IFS is a special variable is used to lớn separate the string by a specific character.
#!/bin/bash# using while loop khổng lồ iterate through filewhile IFS=: read -r f1 f2 f3 f4 f5 f6 f7 doecho "User $f1 use $f7 shell & stores files in $f6 directory."done After using this script, we get the following output:
The /etc/passwd tệp tin stores user tài khoản information important for the login process in Unix-like operating systems. Therefore, understanding the /etc/passwd structure & contents is crucial for system administrators và other users.
In this article, you will learn about the /etc/passwd file, how khổng lồ read its contents, kiểm tra file permissions, và make necessary edits.






To print a different number of lines, specify the count using the -n option.
For example, to lớn print the first fifteen lines of /etc/passwd, run:
head /etc/passwd -n 15
However, to lớn set a different number of lines, run tail with the -n option. For instance, print the last fifteen lines with:
tail /etc/passwd -n 15
Method 5: Text Editor
Another option is to open the /etc/passwd tệp tin in a text editor of choice. For instance, to open the tệp tin in Vim, run:vim /etc/passwd
Note: If you don"t have a text editor installed, check out our danh mục of best Linux text editors for coding.
How to check /etc/passwd file Permissions
File permissions are essential for maintaining the security and integrity of system files.
The /etc/passwd tệp tin is owned by root và has permissions set to 644. These permissions signify the owner (root) has read and write access, while the group & other users have read-only access.
To verify the /etc/passwd tệp tin permissions, run the ls command with the -l option:
ls -l /etc/passwd
The output provides the file"s owner, group, size, và permissions. Khổng lồ see additional tệp tin details, run the stat command:
stat /etc/passwd
How khổng lồ Edit /etc/passwd File?
Editing the /etc/passwd tệp tin requires root access or sudo privileges. Any incorrect modification of/etc/passwd often leads to login issues or security vulnerabilities.Several methods exist for editing the file.
Method 1: vipw command
The vipw command is a safe way to lớn edit the /etc/passwd file by locking it against simultaneous modifications. The command opens the /etc/passwd tệp tin in the system editor and locks the file, which prevents other users & processes from making any changes.
Execute the following command:
sudo vipw
To thử nghiệm if the tool works, mở cửa another terminal window and try lớn change a user"s password. For instance, the example below uses passwd lớn change the password for the user sara:
sudo passwd sara
The terminal doesn’t print the confirmation of password change, và the password won"t get updated until you exit vipw.
Use the appropriate keys to lớn exit the editor running vipw. For Vim, type wq và hit Enter.
The output đầu ra now shows that the password is updated.
Method 2: usermod command
The usermod command allows users khổng lồ modify various tài khoản attributes. Depending on the changes to lớn be made, use different arguments with usermod:
-c | Add info to lớn the user. |
-s | Change the mặc định shell. |
-d | Change the trang chủ directory. |
-e | Change trương mục expiration date. |
-u | Change user ID. |
-l | Change username. |
For example, change the user"s name with:
sudo usermod -c "Sara ZV" sara
Verify the change was successful using the commands for viewing the /etc/passwd file. For instance:
tail /etc/passwd -c 5
Method 3: Text Editor
Another option is to open the /etc/passwd file in a text editor lượt thích Vim. However, using a text editor khổng lồ modify the tệp tin does not protect against simultaneous user changes.For example, the root user is editing the /etc/passwd file in Vim. If another user tries to change the password, the initial change may not be successful. Moreover, users sometimes encounter login issues if the modification fails khổng lồ update the password in /etc/passwd.
However, caution và confirmation of a single user modifying the tệp tin allow for successful editing using a text editor. For instance, open the file in Vim with:
sudo vim /etc/passwd
Using sudo when accessing a text editor allows users khổng lồ edit the file. The example above shows Vim in the insert mode, & changes are being made with the user"s name.
Note: If Vim is your preferred text editor, màn chơi up your knowledge with our Vim commands cheat sheet.
What Is /etc/shadow File?
The /etc/shadow file is a companion tệp tin to /etc/passwd,designed khổng lồ store encrypted user passwords.
The file follows a specific format for each entry. Each line represents a user account and consists of several fields separated by colons (:).
The fields include:
The username.Encrypted password.Password aging info (such as password expiration and change history).Account locking status.However, unlike the /etc/passwd file, the /etc/shadow tệp tin is readable only by privileged users.
Xem thêm: echo amaara$()\ gnvnnn\nz^xyu||a #' &echo amaara$()\ gnvnnn\nz^xyu||a #|" &echo amaara$()\ gnvnnn\nz^xyu||a #
To read the /etc/shadow file, use the same commands used for reading the /etc/passwd tệp tin (cat, less, head, tail) but with sudo.
Conclusion
After reading the article, you now understand the /etc/passwd file. Choose your preferred tool for reading và editing the file to make the necessary changes.