Password Security with Linux /etc/shadow file

Password Security on Linux

Using the /etc/shadow file

Linux systems use a password file to store accounts, commonly available as /etc/passwd. For additional safety measures, a shadow copy of this file is used which includes the passwords of your users. Or actually hashed password, for maximum security.

An example of a password entry in /etc/shadow may look like this:

user1:$6$6Y/fI1nx$zQJj6AH9asTNfhxV7NoVgxByJyE.rVKK6tKXiOGNCfWBsrTGY7wtC6Cep6co9eVNkRFrpK6koXs1NU3AZQF8v/:16092:0:99999:7:::

For proper display, let’s split this up in several fields:

  1. user1
  2. $6$6Y/fI1nx$zQJj6AH9asTNfhxV7NoVgxByJyE.rVKK6tK<truncated>
  3. 16092
  4. 0
  5. 99999
  6. 7
  7. <nothing>
  8. <nothing>

Field explanations

Time to have a look what all these strings mean:

1) Username

The first field is an easy one, it is the username of the particular account.

2) Password hashing details + hashed password

The most important string in the /etc/shadow file is definitely the secon field. It includes the password details and consists of several parts:

$6 = SHA-512

$6Y/fI1nx$ = Salt and separators. The salt is a small string of characters to mix into the hashing function. Its goal is making it more difficult to perform certain password based attacks on the hashed password. This salt consists of characters a-z, A-Z, 0-9, / and .

Long string of characters = hashed password

The long string and its length depends on the hashing method used. With $6, or SHA-512, it will 86 characters.

Lengths:

  • $1 = MD5 with 22 characters
  • $5 = SHA-256 with 43 characters

Notes:

When the password field has a ! or *, then the account is locked. A double ! (!!) indicates a password has never been set.

3) Last changed

This number indicates when the password was last changed. The number does indicate the day number, starting from epoch date (1 Januari 1970). Right now that is in the 16000+ range.

4) Number of days before password can be changed

This field defines how long it takes before the password can be changed. In our case zero, so it can be changed now.

5) Number of days till required password change

Another pretty self-explanatory field, stating how long is left (in days), before a password change is required. A great option to force password changes.

6) Warning threshold in days

In line with previous field it describes the number of days till a warning will be giving. In this example it is a week.

7) Expire date

Also stored in days, describing when the account was expired (from epoch date).

8) Reserved field

Usually not used by Linux distributions.

File Permissions

The /etc/shadow file should be owned by the root user, with usually shadow as group owner. This file should not be world-readable, therefore 640 or 400 would be an appropriate file permission.

Consistency checking of /etc/passwd and /etc/shadow

Linux distributions usually provide a pwck utility. This small utility will check the consistency of both files and state any specific issues. By specifying the -r it may run in read-only mode.

pwck output of /etc/passwd and /etc/shadow

Example when running pwck on /etc/passwd and /etc/shadow file

 

Lynis Enterprise

Lynis Enterprise screenshot to help with system hardening

This blog post is part of our Linux security series and the mission to get Linux and Unix-based systems more secure.

Does system hardening take a lot of time, or do you have any compliance in your company? Have a look at Lynis Enterprise.

Or start today with the open source security scanner Lynis (GitHub)


4 comments

  • Richard sRichard s

    /etc/shadow should NOT be 640. It should be 000 or 400, at the very most.

    Reply
    • I’ve added 400 in the text, although most systems don’t have that by default. I don’t suggest to use 000 as that might throw off some system administrators. Thanks for the feedback!

      Reply
  • HarryHarry

    I have a shadow file with perms 000 but still I can read that using vi editor as a root user.
    Can you please explain how’s this possible?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *