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.
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.
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.
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
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
psoutput and shell history. For automation usechpasswd(readsuser:passwordfrom stdin, oruser:hashwith-e) from a protected pipe or file. - Locking is not disabling.
passwd -lonly blocks password authentication. SSH keys still work; also expire ornologinthe account to block it entirely. - Policy lives in PAM.
pam_pwqualityorpam_cracklibchecks length and dictionary words; see PAM and sudoers. Root may override the checks with a warning. - Aging fields come from
/etc/login.defsfor new accounts (PASS_MAX_DAYSand others).chage -l aliceshows them in words. - Terminal required.
passwdreads the password directly from the terminal, not from stdin, soecho pw | passwddoes 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.
Related
useradd,usermod,su,sudo,last- Lessons: PAM and sudoers, Users and permissions, Hardening