Using the `passwd` command to Change User Passwords (with examples)
The passwd
command in Unix/Linux systems is used to change a user’s password. It is primarily a command-line tool and provides several options to modify user passwords. In this article, we will explore different use cases of the passwd
command with code examples, motivations, explanations, and example outputs.
1: Changing the password of the current user interactively
To change the password of the current user interactively, simply execute the passwd
command without any arguments:
passwd
Motivation: This use case allows the current user to change their password in an interactive mode, which is useful when the user wants to set a new password or update an existing one.
Explanation: When executed, the passwd
command prompts the user to enter their current password followed by the new password. It also asks the user to confirm the new password to ensure accuracy.
Example Output:
Changing password for user abc.
Current password:
New password:
Retype new password:
passwd: password updated successfully
2: Changing the password of a specific user
To change the password of a specific user, provide their username as an argument to the passwd
command:
passwd username
Motivation: This use case allows system administrators to change the passwords of other users on the system. It is particularly useful when managing user accounts and ensuring password security.
Explanation: By specifying the username as an argument, the passwd
command changes the password for that particular user. It may require appropriate privileges, such as root or administrative access, depending on the system configuration.
Example Output:
Changing password for user jane.
New password:
Retype new password:
passwd: password updated successfully
3: Getting the current status of the user
To get the current status of a user, including password-related information, use the -S
option with the passwd
command:
passwd -S
Motivation: This use case enables users or system administrators to check the current status of a user, such as password aging information, password lock status, and more.
Explanation: The -S
option queries the status of the user and displays it on the terminal. It provides information such as the last password change date, password expiration date, minimum and maximum password age, as well as a password lock status (e.g., “L” if the account is locked or “NP” if the account has no password).
Example Output:
jane P 07/12/2022 -1 -1 -1 -1
4: Making the password of an account blank (passwordless)
To set the named account passwordless, use the -d
option with the passwd
command:
passwd -d username
Motivation: This use case can be useful in scenarios where a user needs temporary passwordless access or if the system administrator wants to allow passwordless login for a specific account.
Explanation: By specifying the username as an argument and using the -d
option, the passwd
command removes the password for that account, effectively making it passwordless.
Example Output:
Removing password for user testuser.
passwd: password updated successfully
In this article, we explored the passwd
command and its different use cases. We learned how to change the password interactively, change the password of a specific user, get the current status of a user, and make an account passwordless using various options and arguments provided by the command. The passwd
command is a versatile tool for managing user passwords in Unix/Linux systems.