Website Vidya
SecurityStep-by-Step Guide

Understanding the /etc/shadow File in Linux: Password Hashes and Aging Fields

Learn etc shadow file with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

RR
Rajesh RayLead Systems Educator
Published:
12 min read
PDF Cheat Sheet
Understanding the /etc/shadow File in Linux: Password Hashes and Aging Fields
AI Executive SummaryWebsiteVidya AI

12 min read

Learn etc shadow file with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

Key Highlights
  • What chage Does and When to Use It
  • Availability and Installation Across Linux Distributions
  • Command Syntax, Arguments, and Exit Status Codes
Grounded in full engineering runbookWebsiteVidya AI Engine

Understanding the etc shadow file (/etc/shadow) is vital for Linux security hardening, PAM troubleshooting, and compliance auditing. While /etc/passwd is world-readable so user-space programs can look up UIDs and home directories, /etc/shadow isolates sensitive authentication data—specifically salted password hashes and password expiration schedules—behind strict superuser file permissions.

Every line in /etc/shadow corresponds to a local user account in /etc/passwd and is divided into nine colon-delimited fields. Instead of deciphering raw epoch day counts (days since January 1, 1970) by reading /etc/shadow directly, system administrators use the chage (change age) utility (sudo chage -l username) and passwd -S to inspect and update password rotation, warning periods, inactivity lockouts, and hard account expiration dates.

Knowing how to read the second field of the etc shadow file—including algorithm prefixes ($y$ for yescrypt, $6$ for SHA-512, $2b$ for bcrypt), salt strings, and special lock markers (!, !!, *, and empty fields)—lets you verify immediately whether an account can authenticate with a password, is locked, or is dangerously passwordless.

Advertisement
Understanding the /etc/shadow File in Linux: Password Hashes and Aging Fields — syntax, flags, and practical terminal workflow diagram
Understanding the /etc/shadow File in Linux: Password Hashes and Aging Fields — command syntax, key options, and production workflow reference.

What chage Does and When to Use It

Each record in /etc/shadow contains nine colon-separated fields: 1) Username (matching /etc/passwd), 2) Encrypted password (formatted as $id$rounds$salt$hash, or ! / * if password login is disabled), 3) Last password change (days since Jan 1, 1970; 0 forces immediate password reset at next login), 4) Minimum password age (days required before user may change password again), 5) Maximum password age (days password remains valid before expiring, e.g., 99999), 6) Warning period (days before expiry that user is warned on login, e.g., 7), 7) Inactivity period (grace days after password expires before the account is locked), 8) Account expiration date (absolute date in epoch days when the entire account—including SSH key access—is disabled), and 9) Reserved field (empty, reserved for future use).

Modern Linux distributions format Field 2 according to the Modular Crypt Format (MCF): $y$param$salt$hash (yescrypt, the default on Ubuntu 22.04+, Debian 11+, Fedora 35+, and Kali, which is memory-hard and resistant to GPU/ASIC cracking), $6$salt$hash (SHA-512 with 5,000 default rounds), or $5$salt$hash (SHA-256). If Field 2 starts with ! (such as !$y$... or !!), password authentication is locked; if Field 2 is *, the system/daemon account has never had a password and cannot log in via password.

Linux enforces discretionary access control (DAC) by evaluating the effective User ID (EUID), effective Group ID (EGID), and supplementary groups against inode permission bits. When chage executes, the Virtual File System (VFS) and active Linux Security Modules (SELinux or AppArmor) validate the caller's privileges before committing changes to disk or account databases.

Availability and Installation Across Linux Distributions

The chage utility is packaged in passwd / shadow-utils across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:

Advertisement

Before running administrative tasks or automation scripts, verify whether chage is installed and inspect which binary path and version your shell resolves:

type -a chage
chage --version 2>/dev/null || man -f chage

# Illustrative output (reviewed against documentation)
chage is /usr/bin/chage
chage (passwd / shadow-utils)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y passwd

# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y shadow-utils

# Arch Linux
sudo pacman -S --noconfirm shadow

# Alpine Linux
sudo apk add --no-cache shadow

Command Syntax, Arguments, and Exit Status Codes

sudo chage -l USERNAME | sudo getent shadow [USERNAME]

To inspect or modify /etc/shadow aging fields safely without touching a text editor, use chage [OPTIONS] USERNAME. Running sudo chage -l USERNAME translates all numeric epoch-day fields from /etc/shadow into human-readable calendar dates.

If you need to inspect the raw /etc/shadow line for a specific user, run sudo getent shadow USERNAME. Never edit /etc/shadow directly with vim or nano; use chage, passwd, usermod, or sudo vipw -s.

  • 0 — chage or getent shadow completed successfully.
  • 1 — Permission denied (caller is not root and lacks shadow group access).
  • 2 — Invalid command syntax or specified user not found in /etc/shadow.

Important Options and Flags Reference

Option / FlagLong Form / ArgumentTechnical Behavior and Purpose
-l--listDisplay a human-readable summary of all password and account aging fields from /etc/shadow.
-d--lastday LAST_DAYSet Field 3 (date of last password change in YYYY-MM-DD; pass -d 0 to force reset on next login).
-m--mindays MIN_DAYSSet Field 4 (minimum number of days between password changes; 0 allows changing anytime).
-M--maxdays MAX_DAYSSet Field 5 (maximum number of days a password is valid; 99999 disables expiration).
-W--warndays WARN_DAYSSet Field 6 (number of days of advance warning before password expiration).
-I--inactive INACTIVESet Field 7 (number of days after a password expires before the account is locked; -1 disables).
-E--expiredate EXPIRE_DATESet Field 8 (YYYY-MM-DD date when the account expires completely; -1 removes expiration).
-svipw -sOpen /etc/shadow safely under an advisory lock for emergency recovery edits.

Understanding the difference between Field 5 (-M, maximum password age) and Field 8 (-E, account expiration date) in /etc/shadow is critical on servers that use SSH keys. Field 5 (-M) only expires the user's password—an engineer authenticating with an SSH public key is not prompted to rotate an expired password unless their account itself expires or PAM enforces it. Field 8 (-E) expires the entire account in the PAM account stack (pam_unix.so), blocking both password logins and SSH public-key logins.

Advertisement

Similarly, in Field 2, understand the three non-hash states: * or ! or !! means password login is safely disabled; !<hash> means the user had a password that is currently locked via usermod -L or passwd -l; and a completely blank Field 2 (username::19652:...) means the account has no password required!

When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.

Practical Basic and Advanced Usage Examples

1. Inspect Raw /etc/shadow Record and Decode with chage -l

Compare a user's raw 9-field line in /etc/shadow with the human-readable translation produced by sudo chage -l.

sudo getent shadow alex
sudo chage -l alex

# Illustrative output (reviewed against documentation)
alex:$y$j9T$SaltExampleString$HashValueExampleString1234567890:20748:1:90:14:7:20818:
Last password change                                    : Oct 22, 2026
Password expires                                        : Jan 20, 2027
Password inactive                                       : Jan 27, 2027
Account expires                                         : Dec 31, 2026
Minimum number of days between password change          : 1
Maximum number of days between password change          : 90
Number of days of warning before password expires       : 14

In the raw /etc/shadow line, 20748 is the number of days since Jan 1, 1970 (Oct 22, 2026), 1 is min days, 90 is max days, 14 is warn days, 7 is inactive grace days, and 20818 is the hard account expiration (Dec 31, 2026). chage -l calculates all the calendar dates automatically.

2. Distinguish Active ($y$/$6$), Locked (!), and Daemon (*) Accounts in /etc/shadow

Inspect Field 2 across root, a daemon account (sshd), and a human account (alex).

sudo awk -F: '/^(root|sshd|alex):/ { printf "%-10s Field2=%-20.20s LastChg=%s\n", $1, $2, $3 }' /etc/shadow

# Illustrative output (reviewed against documentation)
root       Field2=!                    LastChg=20680
sshd       Field2=*                    LastChg=20680
alex       Field2=$y$j9T$SaltExampleSt LastChg=20748

root has ! (locked password, SSH/sudo only), sshd has * (daemon account that can never authenticate with a password), and alex has a $y$ (yescrypt) password hash.

3. Set Password Expiration and Account Expiration Policy with chage

Configure a 90-day password rotation policy (-M 90), 7-day warning (-W 7), and an account expiration date (-E 2027-06-30) in a single command.

sudo chage -m 1 -M 90 -W 7 -I 14 -E 2027-06-30 alex
sudo chage -l alex | grep -E "expires|Maximum"

# Illustrative output (reviewed against documentation)
Password expires                                        : Jan 20, 2027
Account expires                                         : Jun 30, 2027
Maximum number of days between password change          : 90

chage updates Fields 4, 5, 6, 7, and 8 of /etc/shadow atomically while holding the password lock.

4. Force Immediate Password Reset at Next Login (chage -d 0)

Setting Field 3 (last password change date) to 0 (chage -d 0) forces PAM to demand a password change the next time the user logs in.

sudo chage -d 0 alex
sudo chage -l alex | head -n 2

# Illustrative output (reviewed against documentation)
Last password change                                    : password must be changed
Password expires                                        : password must be changed

sudo chage -d 0 alex has the exact same effect on /etc/shadow as sudo passwd -e alex.

5. Remove Account and Password Expiration Limits (-E -1 -M 99999)

For service accounts or SSH-key-only administrators whose accounts should never expire automatically, clear the expiration fields.

sudo chage -E -1 -I -1 -m 0 -M 99999 alex
sudo chage -l alex | grep "expires"

# Illustrative output (reviewed against documentation)
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never

Passing -E -1 clears Field 8 (account expiration), -I -1 disables Field 7 (inactivity lockout), and -M 99999 disables password rotation.

6. Generate a Shadow-Compatible Hash Using openssl passwd

Test how salted password hashes stored in Field 2 of /etc/shadow are formatted without modifying any account.

openssl passwd -6 -salt "MyRandomSalt" "ExamplePass123!"

# Illustrative output (reviewed against documentation)
$6$MyRandomSalt$7wR9vQp8k3xLmN1z2B4cV6nM8qW0eR2tY4uI6oP8aS0dF2gH4jK6lZ8xC0vB2nM4qW6eR8tY0uI2oP4aS6dF8/

The output shows the Modular Crypt Format: $6$ selects SHA-512, MyRandomSalt is the salt up to the next $, and the 86-character base64 string is the resulting hash.

Real-World Workflows, Pipelines, and Shell Automation

Security Audit: Scan /etc/shadow for Empty Passwords and Weak Hashing Algorithms

Audit /etc/shadow across a production server to verify strict file permissions, ensure zero accounts have an empty password field ($2 == ""), and flag any legacy MD5 ($1$) or DES password hashes.

#!/usr/bin/env bash
set -euo pipefail

echo "=== 1. /etc/shadow Permissions ==="
sudo stat -c "%a %U:%G %n" /etc/shadow

echo "=== 2. Accounts with EMPTY Passwords (CRITICAL) ==="
sudo awk -F: '$2 == "" {print "CRITICAL - Empty password:", $1}' /etc/shadow

echo "=== 3. Active Password Hash Algorithms ==="
sudo awk -F: '$2 ~ /^$/ {
  split($2, parts, "$")
  printf "User: %-15s AlgoID: $%s$
", $1, parts[2]
}' /etc/shadow

# Illustrative output (reviewed against documentation)
=== 1. /etc/shadow Permissions ===
640 root:shadow /etc/shadow
=== 2. Accounts with EMPTY Passwords (CRITICAL) ===
=== 3. Active Password Hash Algorithms ===
User: ubuntu          AlgoID: $y$
User: alex            AlgoID: $y$

Splitting Field 2 on $ extracts the algorithm identifier ($y$ = yescrypt, $6$ = SHA-512). If any user shows $1$ (MD5), force a password reset (sudo chage -d 0 user) so their hash is upgraded on next login.

Convert Epoch Days in /etc/shadow to Calendar Dates in Bash

When writing custom reporting tools that parse Field 3 or Field 8 of /etc/shadow, convert the epoch-day integer into an ISO-8601 date (YYYY-MM-DD) using GNU date.

sudo awk -F: '$2 ~ /^$/ {print $1, $3}' /etc/shadow | while read -r user days; do
  iso_date="$(date -u -d "1970-01-01 + $days days" +%F)"
  printf "%-15s LastChangedDays=%-6s Date=%s
" "$user" "$days" "$iso_date"
done

# Illustrative output (reviewed against documentation)
ubuntu          LastChangedDays=20727  Date=2026-10-01
alex            LastChangedDays=20748  Date=2026-10-22

date -u -d "1970-01-01 + $days days" +%F accurately converts the epoch day count stored in /etc/shadow into a human-readable date.

Pipelines, Redirection, Quoting, and Shell Expansion

When embedding chage inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.

Permissions, Security Implications, and Safe Execution

On RHEL, Rocky Linux, AlmaLinux, and Fedora, /etc/shadow is installed with mode 0000 (---------- 1 root root). Even with 0000 permissions, root (and setuid root binaries like /usr/bin/passwd) can read and write the file because the kernel grants CAP_DAC_OVERRIDE to UID 0. On Debian and Ubuntu, /etc/shadow defaults to 0640 (-rw-r----- 1 root shadow) so setgid shadow helpers (unix_chkpwd) can verify passwords without full root privileges.

Always monitor /etc/shadow for unauthorized reads or modifications using auditd (-w /etc/shadow -p wra -k shadow-access).

GNU/Linux, BSD, and BusyBox Implementation Differences

The 9-field /etc/shadow file and the chage command are standard across all Linux distributions using shadow-utils (Ubuntu, Debian, RHEL, Rocky Linux, Fedora, SUSE, Arch) and Solaris/illumos.

On FreeBSD and OpenBSD, password hashes and expiration timestamps are stored in /etc/master.passwd (managed with pw and chpass) rather than /etc/shadow. On Alpine Linux, /etc/shadow uses the standard 9-field format, and chage can be installed via apk add shadow.

Troubleshooting Common Errors and Diagnostics

User with valid SSH key gets: 'Your account has expired; please contact your system administrator'

Root Cause: Field 8 (Account Expiration Date) or Field 7 (Password Inactivity grace period after Field 5 expiry) in /etc/shadow has passed. PAM's account phase rejects expired accounts even when authenticating with SSH public keys.

Resolution: Inspect the account's aging metrics with sudo chage -l username and clear or extend the expiration with sudo chage -E -1 -I -1 -M 99999 username.

sudo chage -l username
sudo chage -E -1 -I -1 -M 99999 username

User cannot change their own password: 'You must wait longer to change your password'

Root Cause: Field 4 (Minimum number of days between password change) in /etc/shadow is set to a positive integer (e.g., 7), and the user changed their password fewer than 7 days ago.

Resolution: Reset the minimum password age to 0 (sudo chage -m 0 username) or change the password as root.

sudo chage -m 0 username

sudo pwck -r reports: 'user username: no user in /etc/shadow'

Root Cause: A user was added manually to /etc/passwd without creating a matching record in /etc/shadow.

Resolution: Run sudo pwconv to automatically generate the missing /etc/shadow entry from /etc/passwd.

sudo pwconv
sudo pwck -r

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
The 9 colon-separated fields are: 1) Username, 2) Salted password hash (or ! / *), 3) Last password change (days since Jan 1, 1970), 4) Minimum days between changes, 5) Maximum days password is valid, 6) Warning days before expiry, 7) Inactive grace days after expiry, 8) Account expiration date (days since Jan 1, 1970), and 9) Reserved field.
Both block password-based logins because no password can hash to a single '!' or '*' character. By convention, '*' is placed on system/daemon accounts (like sshd or www-data) that were never meant to have passwords, while '!' is used on newly created accounts or prepended to an existing hash (via usermod -L or passwd -l) to lock a password.
The prefix between the first two dollar signs identifies the cryptographic hashing algorithm used by PAM: $y$ is yescrypt (modern memory-hard default on Ubuntu/Debian/Fedora), $6$ is SHA-512, $5$ is SHA-256, and $2b$ is bcrypt.
In Linux, the root user (UID 0) holds the CAP_DAC_OVERRIDE capability, which bypasses standard read/write permission bits on files. Setting /etc/shadow to 0000 ensures that only root (and setuid root binaries like passwd) can access the file while blocking all other users and groups.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout