The chpasswd command is a utility in Linux that is used to change the passwords of multiple users at once. It reads a list of user names and password hashes from standard input and updates the password for each user in the system password file (usually /etc/passwd).
To use the chpasswd command, you will need to provide a list of user names and password hashes in the following format:
Where username is the user name of the user whose password you want to change, and password_hash is the password hash for the new password.
Here is an example of using the chpasswd command to change the password for the user john to password123:
# echo "john:$6$salt$password123" | chpasswd
In this example, $6$salt$ is the salt for the password hash (which is used to increase the security of the password), and password123 is the plaintext password. The echo command is used to print the user name and password hash to standard output, which is then piped to the chpasswd command.
You can also use the chpasswd command with the -c option to specify the password hash algorithm to use. For example, to use the SHA-256 algorithm, you can use the following command:
# echo "john:$5$salt$password123" | chpasswd -c SHA-256
chpasswd Command Examples
1.Change the password for a specific user:
# printf "username:new_password" | sudo chpasswd
2.Change the passwords for multiple users (The input text must not contain any spaces.):
# printf "username_1:new_password_1\nusername_2:new_password_2" | sudo chpasswd
3.Change the password for a specific user, and specify it in encrypted form:
# printf "username:new_encrypted_password" | sudo chpasswd --encrypted
4.Change the password for a specific user, and use a specific encryption for the stored password:
# printf "username:new_password" | sudo chpasswd --crypt-method NONE|DES|MD5|SHA256|SHA512
The chpasswd command can be useful for managing multiple users on a system, especially when you need to reset the passwords for a large number of users at once. It is also useful for automated password management, as it can be used in scripts to change the passwords of multiple users as part of a regular maintenance routine.