Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Permissions and Users

Linux passwd command

Reference for the Linux passwd command: change your password, set another user's, lock and unlock accounts, expire passwords and read the status with -S.

passwd changes a user's password. Run without arguments it changes your own: it asks for the current password, then the new one twice, checks it against the password policy (length and complexity rules from PAM) and writes the hash to /etc/shadow. Root can set any user's password without knowing the old one.

The same command also manages the state of an account: lock it, unlock it, force a change at next login, and show a one-line status. Password hashes never appear in /etc/passwd; the second field there is just x, meaning "look in /etc/shadow", a file only root can read.

Terminal
passwd [OPTION]... [USER]

Try it

Changing a password needs a terminal and (for another user) root, so these examples look at what is safely readable: the shadow-file structure and the status line format.

Bash Why the password field is x

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Reading status lines (sample data)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
passwd [OPTION]... [LOGIN]
Option Description
-l, --lock Lock the password by prefixing the hash with ! (root only).
-u, --unlock Remove that prefix again.
-d, --delete Remove the password so the account has none. Dangerous: possibly passwordless login.
-e, --expire Mark the password as expired, so the user must choose a new one at next login.
-S, --status Print a one-line status: P usable, L locked, NP none, plus aging fields.
-n DAYS, -x DAYS Minimum / maximum days between changes.
-w DAYS Days of warning before expiry.
-i DAYS Days after expiry until the account is disabled.
-q Quiet.

Returns: 0 on success; 1 for a permission problem; 3 if the shadow file cannot be written; 10 when the password policy rejects the new password. Exact codes differ slightly by distribution.

Usage notes

  • Never pass a password on the command line. It would show in ps output and shell history. For automation use chpasswd (reads user:password from stdin, or user:hash with -e) from a protected pipe or file.
  • Locking is not disabling. passwd -l only blocks password authentication. SSH keys still work; also expire or nologin the account to block it entirely.
  • Policy lives in PAM. pam_pwquality or pam_cracklib checks length and dictionary words; see PAM and sudoers. Root may override the checks with a warning.
  • Aging fields come from /etc/login.defs for new accounts (PASS_MAX_DAYS and others). chage -l alice shows them in words.
  • Terminal required. passwd reads the password directly from the terminal, not from stdin, so echo pw | passwd does not work.
  • Directory services. With LDAP or Active Directory the change may be forwarded to the directory server through PAM.
  • Prefer keys for remote access. A strong password plus SSH key authentication is the usual pairing; see SSH and remote.