Linux How To's | Bash Scripting | Python

Saturday, 1 August 2015

/etc/passwd file is one of the most important files as it possess all the necessary details about every account in the Linux system. In this article, we will learn about /etc/passwd file in more depth.

File permissions on /etc/passwd file

In order to view file permissions on /etc/passwd file, you can execute ll /etc/passwd as follows:

You can clearly observe that, this file is open to be read by all, but is only writable by root or superuser.

/etc/passwd file contents

As quoted, /etc/passwd maintains the information about each and every user that can use the system. Every time a new user account is created, the user account details are stored in the same file. Whenever some user attempts for a login to the system, the getty process validates the user, whether the user is legitimate or not, using /etc/passwd (and /etc/shadow for the password validation) file.

Normally, the /etc/passwd is a text file that contains one separate line entry, delimited by a colon (:), for each user account configured in the system, as shown below:

As I said, there is a separate entry on a separate line in the /etc/password file, for each user in the system, and each entry being delimited with a colon (:). For better understanding, I've created the fields across each colon and numbered each of the fields, in this way, there are total 7 fields generated. Let us study each field one by one:

Username field: This field denotes the User (or User Account) Name. According to the man page of useradd command, "Usernames may only be up to 32 characters long". This username must be used at the time of logging in to the system.

Password field: Second field is the Password field, not denoting the actual password though. A 'x' in this field denotes the password is encrypted and saved in the /etc/shadow file.

UID field: Whenever a new user account is created, it is assigned with a user id or UID (UID for the user 'mandar' is 500, in this case) and this field specifies the same.

GID field: Similar to the UID field, this field specifies which group the user belongs to, the group details being present in /etc/group file.

Comment/Description/User Info field: This field is the short comment/description/information of the user account (For this example, user account 'mandar' belongs to the user Mandar Shinde, hence this comment).

User Home Directory: Whenever a user logs in to the system, he is taken to his Home directory, where all his personal files reside. This field provides the absolute path to the user's home directory (/home/mandar in this case).

Shell: This field denotes, the user has access to the shell mentioned in this field (user 'mandar' has been given access to /bin/bash or simply bash shell).

To get the user account information, you can simple write a script, that fetches the details from /etc/passwd file, as below: