Linux How To's | Bash Scripting | Python

Sunday, 2 August 2015

In one of the recent articles we've published, we've learned the file format of /etc/passwd file, which stores one line entry for each user that can access the system. As one of the fields in each line of the /etc/passwd denotes whether the password for that user is stored in /etc/shadow file or not, it can easily be depicted that the actual passwords (of course, in the encrypted form) are stored in the /etc/shadow file. This article will help you learn more about /etc/shadow file format in more detail.

/etc/shadow File Permission

To begin with, let us observe and compare the file permissions on both /etc/passwd and /etc/shadow files:

Things are pretty clear- /etc/passwd is world readable and /etc/shadow can only be read by the root user. This is because, had the password were stored in /etc/passwd file, even in encrypted format, anyone could see, decrypt and use them pretty easily. Thus, passwords are actually stored in /etc/shadow file which can only be accessed by root or superuser and not made open to the entire world, as there is a huge risk factor involved in it.

/etc/shadow File Contents

/etc/shadow file is the text file that holds the information about User password, the hash algorithm used to create hash, the salt value used to create hash and some details related to password expiry. Each line in this file is used to store the information about one user, delimited with a colon (:), and the file looks like:

For better understanding, I've divided the line entry across each colon(:) to create 8 fields, which are explained as below:

Username field: This field denotes the username (or the user account name), that should be used while logging in to the system.

Password field: This field stores the password in encrypted format (explained in detail below).

Last Password Change: This field denotes the number of days, since UNIX time (1-Jan-1970), the last password change happened.

Minimum days between password changes: This field denotes the minimum number of days after which a user can change his password.

Password validity: This field denoted the maximum number of days for which password is valid. After that, the password will expire and the user will have to change the password.

Warning threshold: This field denotes the number of days before which the user will receive a warning notification about the password expiry.

Account inactive: This field denotes the number of days after which the account will be disabled, when the password is expired.

Time since account is disabled: This field denotes the number of days, from UNIX time, since which the account is disabled.

The Encrypted Password

The field #2 in each line entry is the encrypted password, as we just learned. But, how this password is generated, we'll learn in this portion of the article. Let's just rewrite the encrypted password here-

1. Hash Algorithm: This field denotes the hashing algorithm used to create the hashed password. The digit 6 describes that, SHA-512 algorithm is used, in this case. Some more of them are enlisted below:

2. Salt Value: Salt values are used to make the hash value stronger. These are the random type of data that is used to combine with the original password and then the hashed version of that is used as the encrypted password.3. Password: This field stores the hashed version of the combination of original password and salt value.

To verify this, we would try to generate the hash value using SHA-512 algorithm along with the salt value (5H0QpwprRiJQR19Y) and the original password (mandar) and match it with the hash value mentioned in the /etc/shadow file.

reading crypt's man page (http://perldoc.perl.org/functions/crypt.html) I think the hash algorithm is always SHA-512. In this case the salt is the full string "$6$5H0QpwprRiJQR19Y$" (that is the "$6$" is part of salt itself).Isn't so?