Website Vidya
SecurityStep-by-Step Guide

passwd Command in Linux: Change User Passwords and Manage Expiration

Learn passwd command in linux 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
passwd Command in Linux: Change User Passwords and Manage Expiration
AI Executive SummaryWebsiteVidya AI

12 min read

Learn passwd command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

Key Highlights
  • What passwd 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

Mastering passwd command in linux is an essential skill for Linux system administrators, developers, and DevOps engineers. Mastering the passwd command in Linux is essential for managing local authentication credentials, enforcing password rotation policies, and securing user accounts across servers and workstations. While every Linux user encounters passwd when updating their own login passphrase, system administrators rely on its administrative flags to audit password states (-S), lock and unlock password hashes (-l and -u), and force newly onboarded employees to choose a new password upon their first SSH login (-e).

When a regular user invokes passwd, the utility first prompts for their current password to prevent someone from walking up to an unlocked terminal and hijacking the account, and then enforces strict PAM password complexity rules (pam_pwquality.so or pam_cracklib.so) checking length, dictionary words, and character classes. By contrast, when root runs sudo passwd username, passwd skips the old-password prompt and allows setting a temporary password directly.

Beyond interactive password changes, understanding how passwd writes salted cryptographic hashes (such as yescrypt or SHA-512) and aging parameters into /etc/shadow—and how it pairs with chpasswd for non-interactive automation—is critical for secure Linux systems administration.

Advertisement
passwd Command in Linux: Change User Passwords and Manage Expiration — syntax, flags, and practical terminal workflow diagram
passwd Command in Linux: Change User Passwords and Manage Expiration — command syntax, key options, and production workflow reference.

What passwd Does and When to Use It

Because /etc/shadow has strict permissions (0640 root:shadow or 0000 root:root) that forbid write access to unprivileged users, /usr/bin/passwd is compiled as a setuid root binary (-rwsr-xr-x 1 root root). When an unprivileged user runs passwd, the kernel elevates the process's effective UID (EUID) to 0, allowing passwd to invoke the PAM chauthtok (change authentication token) stack configured in /etc/pam.d/passwd, verify that the caller only modifies their own record (RUID == target UID), generate a random cryptographic salt, hash the new secret, and atomically rewrite /etc/shadow.

In /etc/shadow, passwd manages fields 2 through 7: the salted password hash (field 2), the date of last password change in epoch days (field 3), minimum password age -n (field 4), maximum password age -x (field 5), warning period -w (field 6), and inactivity grace period -i (field 7). When passwd -e username is executed, passwd sets field 3 (last change date) to 0, which signals PAM (pam_unix.so) to immediately force a password change the next time the user logs in.

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 passwd 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 passwd 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 passwd is installed and inspect which binary path and version your shell resolves:

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

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

# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y passwd

# Arch Linux
sudo pacman -S --noconfirm shadow

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

Command Syntax, Arguments, and Exit Status Codes

passwd [OPTIONS] [USERNAME]

The syntax passwd [OPTIONS] [USERNAME] behaves differently based on the caller's privileges. Run with no arguments (passwd), it interactively changes the password of the currently logged-in user. Run as sudo passwd USERNAME, it changes or modifies the password state of USERNAME.

Note that characters typed at the New password: prompt are intentionally invisible—passwd disables terminal echo (ECHO flag in termios) so bystanders or screen recordings cannot see the password length. If you need to set passwords non-interactively in a provisioning script, use chpasswd (or passwd --stdin on RHEL/Fedora systems) rather than piping raw input into Debian/Ubuntu passwd.

  • 0 — Password or account aging attributes updated successfully.
  • 1 — Permission denied (unprivileged user attempted to modify another user or use admin flags).
  • 2 — Invalid combination of options supplied.
  • 3 — Unexpected failure while updating the shadow file.
  • 4 — /etc/passwd or /etc/shadow file is missing.
  • 5 — Shadow file is currently busy/locked by another process (.pwd.lock).
  • 6 — Invalid argument passed to a numeric aging option.

Important Options and Flags Reference

Option / FlagLong Form / ArgumentTechnical Behavior and Purpose
(no args)passwdInteractively change the password for the currently logged-in user account.
-S--statusDisplay account password status (P = usable password, L = locked, NP = no password) and aging metrics.
-a--allCombine with -S (sudo passwd -S -a) to display the password status of all accounts in /etc/shadow.
-l--lockLock the password of the named account by prefixing the encrypted hash in /etc/shadow with '!'.
-u--unlockUnlock the password of the named account by removing the leading '!' prefix from /etc/shadow.
-e--expireImmediately expire the user's password, forcing them to set a new password at their next login.
-d--deleteDelete a user's password (makes the password empty/passwordless—use with extreme caution).
-x--maxdays MAX_DAYSSet the maximum number of days a password remains valid before it must be changed.
-n--mindays MIN_DAYSSet the minimum number of days required between password changes.
-w--warndays WARN_DAYSSet the number of days of advance warning a user receives before their password expires.

For security auditing, sudo passwd -S -a is one of the fastest commands to verify the authentication posture of every account on a Linux host. The second column of the output reports L (locked password), NP (no password), or P (active usable password). On a well-hardened server, only interactive human accounts should show P, all system accounts should show L, and zero accounts should ever show NP.

Advertisement

When onboarding a user who requires a password (for example, to authenticate sudo commands alongside SSH key login), always run sudo passwd username followed immediately by sudo passwd -e username. This ensures the temporary password you transmitted cannot be used permanently and forces the user to enter a secret known only to them on first login.

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. Change Your Own Password or Another User's Password

Run passwd to update your own password, or sudo passwd username as an administrator to reset another user's password without knowing their current one.

sudo passwd alex

# Illustrative output (reviewed against documentation)
New password: 
Retype new password: 
passwd: password updated successfully

sudo passwd alex prompts for the new password twice (without echoing characters to the terminal), hashes it with a cryptographic salt, and updates /etc/shadow.

2. Check Password Lock Status and Aging Policy (-S)

Inspect whether an account has an active password, is locked, or has password expiration rules applied.

sudo passwd -S alex
sudo passwd -S sshd

# Illustrative output (reviewed against documentation)
alex P 10/22/2026 0 99999 7 -1
sshd L 08/15/2026 0 99999 7 -1

The seven fields returned by passwd -S are: username (alex), status (P = active password, L = locked), last password change date (10/22/2026), minimum age (0 days), maximum age (99999 days), warning period (7 days), and inactivity period (-1 = disabled).

3. Force a User to Change Their Password at Next Login (-e)

After assigning a temporary initial password to a user, expire the password immediately so PAM prompts them to pick a new secret as soon as they log in.

sudo passwd -e alex
sudo passwd -S alex

# Illustrative output (reviewed against documentation)
passwd: password expiry information changed.
alex P 01/01/1970 0 99999 7 -1

Notice that the last password change date changed to 01/01/1970 (epoch day 0), which triggers a mandatory password change prompt during the user's next login.

4. Lock and Unlock a User's Password (-l and -u)

Disable password-based logins for an account (for example, when enforcing SSH-key-only authentication) and unlock it if needed.

sudo passwd -l alex
sudo passwd -S alex
sudo passwd -u alex
sudo passwd -S alex

# Illustrative output (reviewed against documentation)
passwd: password expiry information changed.
alex L 10/22/2026 0 99999 7 -1
passwd: password expiry information changed.
alex P 10/22/2026 0 99999 7 -1

passwd -l prepends ! to the encrypted hash in /etc/shadow (changing the status to L), and passwd -u removes the ! prefix.

5. Configure Password Rotation and Warning Policies (-x, -n, -w, -i)

Enforce a 90-day maximum password lifetime, a 1-day minimum interval between changes, a 14-day warning window, and a 7-day post-expiry grace period.

sudo passwd -x 90 -n 1 -w 14 -i 7 alex
sudo passwd -S alex

# Illustrative output (reviewed against documentation)
passwd: password expiry information changed.
alex P 10/22/2026 1 90 14 7

The account now requires a password change every 90 days, warns the user 14 days in advance, and locks the account 7 days after an expired password goes unchanged.

6. Audit All Accounts for Empty / Passwordless Status (-S -a)

Scan every account on the server to verify that no account has an empty password (NP).

sudo passwd -S -a | head -n 5
sudo passwd -S -a | awk '$2 == "NP" {print "WARNING - Empty password:", $1}'

# Illustrative output (reviewed against documentation)
root L 08/15/2026 0 99999 7 -1
daemon L 08/15/2026 0 99999 7 -1
bin L 08/15/2026 0 99999 7 -1
sys L 08/15/2026 0 99999 7 -1
sync L 08/15/2026 0 99999 7 -1

Filtering sudo passwd -S -a for $2 == "NP" immediately highlights any dangerously misconfigured passwordless accounts.

Real-World Workflows, Pipelines, and Shell Automation

Non-Interactive Password Updates in Automation Using chpasswd

Because Debian and Ubuntu's passwd binary intentionally omits the --stdin flag, automated provisioning scripts should use chpasswd (or even better, chpasswd -e with pre-hashed secrets generated by openssl passwd -6) so plaintext passwords never appear in process arguments.

# Generate a salted SHA-512 hash and apply it with chpasswd -e:
HASH="$(openssl passwd -6 'TempP@ssw0rd!2026')"
printf 'alex:%s
' "$HASH" | sudo chpasswd -e
sudo passwd -e alex
sudo passwd -S alex

# Illustrative output (reviewed against documentation)
passwd: password expiry information changed.
alex P 01/01/1970 0 99999 7 -1

Passing a pre-computed hash to sudo chpasswd -e updates /etc/shadow across both Debian/Ubuntu and RHEL/Fedora without requiring interactive TTY prompts, and sudo passwd -e alex forces the user to pick their own password at first login.

Compliance Audit: Find All Accounts with Active Password Hashes

In zero-trust production servers where only SSH keys should be used, list every account that currently has an active, unlocked password hash (P).

sudo passwd -S -a | awk '$2 == "P" { printf "%-15s LastChanged=%-12s MaxDays=%s\n", $1, $3, $5 }'

# Illustrative output (reviewed against documentation)
ubuntu          LastChanged=10/01/2026   MaxDays=99999
alex            LastChanged=01/01/1970   MaxDays=90

This one-liner gives security auditors an immediate list of every account capable of password authentication alongside its last change date and rotation policy.

Pipelines, Redirection, Quoting, and Shell Expansion

When embedding passwd 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

Modern Linux distributions configure PAM (pam_unix.so) to hash passwords with yescrypt (Ubuntu 22.04+, Debian 11+, Fedora 35+, prefix $y$) or salted SHA-512 ($6$). You can verify your system's hashing algorithm by inspecting ENCRYPT_METHOD in /etc/login.defs and /etc/pam.d/common-password (or /etc/pam.d/system-auth on RHEL).

To enforce strong password complexity (minimum length, uppercase/lowercase/digit/symbol requirements, and dictionary checks), configure /etc/security/pwquality.conf (used by libpwquality / pam_pwquality.so). Remember that when root runs sudo passwd username, PAM still prints complexity warnings for weak passwords but allows root to override them—regular users cannot bypass pwquality rejections.

GNU/Linux, BSD, and BusyBox Implementation Differences

There are two distinct passwd implementations on Linux: Debian, Ubuntu, and Arch Linux ship passwd from shadow-utils, whereas RHEL, CentOS, Rocky Linux, and Fedora ship a PAM-specific passwd package that supports --stdin (reading a password from a pipe) and uses -f (--force) alongside -u. By contrast, chpasswd works identically across both families.

On Alpine Linux (BusyBox), passwd supports basic interactive changes, -l, -u, and -d. On FreeBSD and macOS, passwd changes local or OpenDirectory passwords (dscl on macOS).

Troubleshooting Common Errors and Diagnostics

BAD PASSWORD: The password fails the dictionary check / is too short

Root Cause: The PAM module pam_pwquality.so rejected the new password because it violates rules in /etc/security/pwquality.conf or is too similar to the old password or username.

Resolution: Choose a longer passphrase (12+ characters with mixed character classes) that does not contain the username or dictionary words.

grep -v '^#' /etc/security/pwquality.conf | grep -v '^$'

Authentication token manipulation error

Root Cause: The root filesystem / is mounted read-only, /etc/shadow has the immutable attribute (chattr +i) set, disk space/inodes on / are 100% full, or /etc/.pwd.lock is stuck.

Resolution: Check filesystem mount state (findmnt /), free space (df -h /), file attributes (lsattr /etc/shadow), and stale lock files.

findmnt /
df -h /
lsattr /etc/shadow

passwd: Unlocking the password would result in a passwordless account.

Root Cause: You ran sudo passwd -u username on an account that was created by useradd without ever having a password hash set (its /etc/shadow field is a bare ! rather than !<hash>).

Resolution: Set an initial password with sudo passwd username instead of trying to unlock an empty hash.

sudo passwd username

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
For security, the passwd command disables terminal character echoing so no one looking at your screen can see your password or even count how many characters long it is. Type your password normally and press Enter.
Run sudo passwd -e username (or sudo chage -d 0 username). This marks the user's current password as expired in /etc/shadow so PAM requires them to set a new password the next time they authenticate.
passwd -l locks the password by adding a '!' in front of the encrypted hash in /etc/shadow, disabling password login while keeping the old hash saved. passwd -d deletes the hash completely, leaving the account with an empty (blank) password.
Pipe username:password into chpasswd: echo 'username:NewStrongPass!' | sudo chpasswd, or pass a pre-encrypted SHA-512/yescrypt hash using echo 'username:$6$...' | sudo chpasswd -e.
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