passwd (change user password)
The passwd command is used to set or change passwords for user accounts. A regular user can only change their own password, while the root user can change any user's password. It is a critical command for maintaining account security.
Example 1: Change your own password
# Change the password for the currently logged-in user
passwd
Explanation
When run without any arguments, passwd initiates an interactive prompt for the current user to change their password. It will first ask for the current password for verification, then prompt for the new password twice for confirmation.
Example 2: Change another user's password (as root)
# As the root user, change the password for the user 'jdoe'
sudo passwd jdoe
Explanation
A privileged user (like root) can change any user's password by specifying the username as an argument. The system will not ask for the old password in this case, directly prompting for a new password for jdoe.
Example 3: Lock a user's account
# Lock the account for user 'msmith'
sudo passwd -l msmith
Explanation
The -l option locks the specified user's account, preventing them from logging in with a password. This is an alternative method to usermod -L and achieves the same result by placing a '!' before the encrypted password in /etc/shadow.
Example 4: Unlock a user's account
# Unlock the account for user 'msmith'
sudo passwd -u msmith
Explanation
The -u option is the counterpart to -l and unlocks a previously locked account. It removes the '!' from the beginning of the password hash, re-enabling password-based authentication for the user.
Example 5: Forcing a user to change their password on next login
# Expire the password for user 'asmith', forcing a change at next login
sudo passwd -e asmith
Explanation
The -e option expires a user's password. The next time the user tries to log in, they will be required to set a new password before they can proceed. This is a common security procedure for newly created accounts.
Example 6: View password status for a user
# Display password status information for user 'rjay'
sudo passwd -S rjay
Explanation
The -S option displays a short status line for the user's password. It shows the username, whether the account is locked (L) or has no password (NP), the date of the last password change, and other aging information.
Example 7: Remove a user's password
# Delete the password for user 'guest', allowing login without a password
sudo passwd -d guest
Explanation
The -d option deletes the user's password, making it an empty string. This allows the user to log in without providing a password. This should only be used in highly controlled and secure environments.
Example 8: Set a password from standard input (for scripting)
# Set a new password for 'testuser' using a pipe (use with caution)
echo "NewStrongPassword123" | sudo passwd --stdin testuser
Explanation
The --stdin option allows passwd to read the new password from standard input instead of an interactive terminal. This is useful for scripting user creation but should be used carefully, as the password may be visible in command history or process lists.
UNIX Variants
The passwd command is one of the most standardized commands across all UNIX and Linux systems. Its basic operation and most common flags (-l, -u, -d, -S) are virtually identical on RHEL, Solaris, and FreeBSD.