How to use the command chpasswd (with examples)
- Linux
- December 25, 2023
The chpasswd command in Linux is used to change the passwords for multiple users by using stdin
. It is a convenient and efficient way to update passwords for multiple users in one go. This article will illustrate some of the common use cases of the chpasswd command with examples.
Use Case 1: Change the password for a specific user
Code:
printf "username:new_password" | sudo chpasswd
Motivation: This use case is helpful when you need to change the password for a specific user quickly and easily. By using this command, you can update the password without any manual intervention.
Explanation:
- printf: This command is used to format and print the given string. In this case, it is used to generate the input string for chpasswd.
- “username:new_password”: The format username:new_password specifies the username for which the password needs to be changed, followed by a colon, and the new password for that user.
- | (pipe): The pipe symbol is used to redirect the output of printf to the input of chpasswd.
- sudo: This command is used to run chpasswd with administrative privileges.
- chpasswd: This is the main command that changes the password for the specified user.
Example Output: If the username is “john” and the new password is “newpass123”, the command will change the password for the user “john” to “newpass123”.
Use Case 2: Change the passwords for multiple users
Code:
printf "username_1:new_password_1\nusername_2:new_password_2" | sudo chpasswd
Motivation: This use case is useful when you need to change passwords for multiple users at once. By providing the input as a formatted string, you can update the passwords for multiple users in a single command.
Explanation:
- printf: This command is used to format and print the given string. In this case, it is used to generate the input string for chpasswd.
- “username_1:new_password_1\nusername_2:new_password_2”: The format username:new_password specifies the username for which the password needs to be changed, followed by a colon, and the new password for each user. The newline character (\n) is used to separate each user’s entry.
- | (pipe): The pipe symbol is used to redirect the output of printf to the input of chpasswd.
- sudo: This command is used to run chpasswd with administrative privileges.
- chpasswd: This is the main command that changes the passwords for the specified users.
Example Output: If the usernames are “john” and “jane” and their respective new passwords are “newpass123” and “pass123new”, the command will change the passwords for the users “john” and “jane” accordingly.
Use Case 3: Change the password for a specific user, and specify it in encrypted form
Code:
printf "username:new_encrypted_password" | sudo chpasswd --encrypted
Motivation: This use case is helpful when you have the password in encrypted form and need to change the password for a specific user. By using this command, you can provide the encrypted password as input and update the password without revealing the actual password in plain text.
Explanation:
- printf: This command is used to format and print the given string. In this case, it is used to generate the input string for chpasswd.
- “username:new_encrypted_password”: The format username:new_encrypted_password specifies the username for which the password needs to be changed, followed by a colon, and the encrypted form of the new password for that user.
- | (pipe): The pipe symbol is used to redirect the output of printf to the input of chpasswd.
- sudo: This command is used to run chpasswd with administrative privileges.
- chpasswd: This is the main command that changes the password for the specified user.
- –encrypted: This option is used to tell chpasswd that the provided password is already encrypted.
Example Output: If the username is “john” and the new encrypted password is “Q@mupGK6u8QGM”, the command will change the password for the user “john” to the encrypted password “Q@mupGK6u8QGM”.
Use Case 4: Change the password for a specific user, and use a specific encryption for the stored password
Code:
printf "username:new_password" | sudo chpasswd --crypt-method NONE|DES|MD5|SHA256|SHA512
Motivation: This use case is useful when you want to specify the encryption method for the stored password while changing the password for a specific user. By using this command, you can choose the appropriate encryption method based on your requirements.
Explanation:
- printf: This command is used to format and print the given string. In this case, it is used to generate the input string for chpasswd.
- “username:new_password”: The format username:new_password specifies the username for which the password needs to be changed, followed by a colon, and the new password for that user.
- | (pipe): The pipe symbol is used to redirect the output of printf to the input of chpasswd.
- sudo: This command is used to run chpasswd with administrative privileges.
- chpasswd: This is the main command that changes the password for the specified user.
- –crypt-method: This option is used to specify the encryption method for the stored password. The available options are NONE, DES, MD5, SHA256, and SHA512.
Example Output: If the username is “john” and the new password is “newpass123”, and the encryption method is SHA256, the command will change the password for the user “john” to “newpass123” using the SHA256 encryption method.
Conclusion: The chpasswd command is a powerful tool for changing passwords for multiple users. Whether you need to update passwords for individual users or multiple users, or specify encryption methods, chpasswd provides a flexible and efficient solution. By understanding the different use cases and examples provided in this article, you can effectively utilize the chpasswd command in your Linux environment.