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.
12 min read
Learn passwd command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.
- What passwd Does and When to Use It
- Availability and Installation Across Linux Distributions
- Command Syntax, Arguments, and Exit Status Codes
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).
The passwd command in Linux changes a user account's authentication password and manages password aging, locking, and expiration policies in /etc/shadow. Any user can run passwd with no arguments to change their own password, while root (or a sudo administrator) can run sudo passwd username to reset any user's password, lock an account (-l), or force a password reset on next 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.
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:
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/passwdor/etc/shadowfile 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 / Flag | Long Form / Argument | Technical Behavior and Purpose |
|---|---|---|
(no args) | passwd | Interactively change the password for the currently logged-in user account. |
-S | --status | Display account password status (P = usable password, L = locked, NP = no password) and aging metrics. |
-a | --all | Combine with -S (sudo passwd -S -a) to display the password status of all accounts in /etc/shadow. |
-l | --lock | Lock the password of the named account by prefixing the encrypted hash in /etc/shadow with '!'. |
-u | --unlock | Unlock the password of the named account by removing the leading '!' prefix from /etc/shadow. |
-e | --expire | Immediately expire the user's password, forcing them to set a new password at their next login. |
-d | --delete | Delete a user's password (makes the password empty/passwordless—use with extreme caution). |
-x | --maxdays MAX_DAYS | Set the maximum number of days a password remains valid before it must be changed. |
-n | --mindays MIN_DAYS | Set the minimum number of days required between password changes. |
-w | --warndays WARN_DAYS | Set 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.
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
Never run sudo passwd -d username (--delete) on an account exposed to SSH or console login unless PermitEmptyPasswords no is strictly enforced—-d removes the password hash completely, creating a passwordless account (NP). To disable password login safely, always use sudo passwd -l username (--lock).
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
Alternatives, Related Commands, and Related Tutorials
- How to Create Users in Linux with the useradd Command — Practical syntax, flags, and examples for
useradd. - usermod Command in Linux: Modify User Accounts, Shells, and Groups — Practical syntax, flags, and examples for
usermod. - su Command in Linux: Switch Users and Run Commands with Substitute Identity — Practical syntax, flags, and examples for
su. - Understanding the /etc/passwd File in Linux: Format, Fields, and Security — Practical syntax, flags, and examples for
passwd. - 25 Best Linux Commands Every Sysadmin Should Know — Core command-line utilities for daily Linux server administration.
- How to Find a File in Linux from the Command Line — Practical file search patterns across Linux directories.
- Linux Commands Knowledge Hub (#linux-commands) — Browse all Linux command-line tutorials and system administration references.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
/etc/shadow so PAM requires them to set a new password the next time they authenticate./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.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
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.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides


Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!