The /etc/passwd tệp tin is a text tệp tin that describes user login accounts for the system.It should have read permission allowed for all users (many utilities, like ls(1) use it to bản đồ user IDs to usernames), but write access only forthe superuser.In the good old days there was no great problem with this general read permission. Everybody could read the encrypted passwords, but the hardware was tooslow to lớn crack a well-chosen password, và moreover the basic assumption used khổng lồ be that of a friendly user-community. These days many people run some versionof the shadow password suite, where /etc/passwd has an aqxaq character in the password field, và the encrypted passwords are in /etc/shadow,which is readable by the superuser only.If the encrypted password, whether in /etc/passwd or in /etc/shadow, is an empty string, login is allowed without even asking for a password.Note that this functionality may be intentionally disabled in applications, or configurable (for example using the "nullok" or "nonull" arguments topam_unix.so).If the encrypted password in /etc/passwd is "*NP*" (without the quotes), the shadow record should be obtained from an NIS+ server.Regardless of whether shadow passwords are used, many system administrators use an asterisk (*) in the encrypted password field khổng lồ make sure that this usercan not authenticate him- or herself using a password. (But see NOTES below.)If you create a new login, first put an asterisk (*) in the password field, then use passwd(1) to lớn set it.Each line of the tệp tin describes a single user, và contains seven colon-separated fields:name:password:UID:GID:GECOS:directory:shell
The field are as follows:name
This is the user"s login name. It should not contain capital letters.password
This is either the encrypted user password, an asterisk (*), or the letter aqxaq. (See pwconv
(8) for an explanation of aqxaq.)UIDThe privileged root login account (superuser) has the user ID 0.GIDThis is the numeric primary group ID for this user. (Additional groups for the user are defined in the system group file; see group(5)).GECOSThis field (sometimes called the "comment field") is optional và used only for informational purposes. Usually, it contains the full username. Someprograms (for example, finger(1)) display information from this field.GECOS stands for "General Electric Comprehensive Operating System",which was renamed lớn GCOS when GE"s large systems division was sold to lớn Honeywell. Dennis Ritchie has reported: "Sometimes we sent printer output đầu ra or batchjobs khổng lồ the GCOS machine. The gcos field in the password tệp tin was a place to stash the information for the $IDENTcard. Not elegant."directory
This is the user"s home directory: the initial directory where the user is placed after logging in. The value in this field is used lớn set the HOMEenvironment variable.shell
This is the program lớn run at login (if empty, use /bin/sh). If set khổng lồ a nonexistent executable, the user will be unable to login throughlogin(1). The value in this field is used khổng lồ set the SHELL environment variable.

Bạn đang xem: Understanding /etc/ passwd file in linux

Files

/etc/passwd

Notes

If you want khổng lồ create user groups, there must be an entry in /etc/group, or no group willexist.If the encrypted password is set khổng lồ an asterisk (*), the user will be unable lớn login using login(1), but may still login usingrlogin(1), run existing processes and initiate new ones through rsh(1), cron(8), at(1), or mailfilters, etc. Trying khổng lồ lock an trương mục by simply changing the shell field yields the same result & additionally allows the use of su(1).

See Also

login(1), passwd(1), su(1), getpwent(3),getpwnam(3), crypt(3), group(5), shadow(5)

Referenced By

authconfig(8),chage(1),chfn(1),chsh(1),darcs(1),dash(1),environ(5),environ(7),fgetpwent(3),fgetpwent_r(3),ftpd(8),ftpusers(5),getgrouplist(3),getpw(3),git-commit-tree(1),git-var(1),groff_mdoc(7),grpck(8),libbind-irs.conf(5),login.defs(5),mdoc.samples(7),mknetid(8),netgroup(5),newusers(8),nscd(8),postconf(1),pwck(8),sge_conf(5),slapd-passwd(5),slapd.backends(5),smbpasswd(5),submit(1),sudo(8),userhelper(8),userinfo(1),vipw(8),winbindd(8),ypchfn(1),yppasswdd(8)

The /etc/passwd file is the most important file in Linux operating system. This tệp tin stores essential information about the users on the system. This tệp tin is owned by the root user & to edit this tệp tin we must have root privileges. But try to avoid edit this file. Now let’s see actually how this file look

This tệp tin 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 lượt thích this:


Now let’s understand each field one by one:Username: This field stores the usernames which are used while login into the system. The length of this field is between 1 and 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 lớn each user by the operating system to 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 tệp tin is empty, then the trang chủ 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 to 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 content of tệp tin using the mèo 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 lớn get information about the user Nishant we can use the following command:


grep nishant /etc/passwd

Check /etc/passwd tệp tin 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 will be

We can see that the permissions of the file /etc/passwd are rw-r–r–. This means the root user has read và write access & other groups and user have read-only access to lớn the file.

Xem thêm:

To get more details like size, modify the time of this tệp tin 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 and IFS separator. A while loop is used khổng lồ iterate through the file, and IFS is a special variable is used khổng lồ separate the string by a specific character.

#!/bin/bash# using while loop to iterate through filewhile IFS=: read -r f1 f2 f3 f4 f5 f6 f7 doecho "User $f1 use $f7 shell and stores files in $f6 directory."done After using this script, we get the following output: