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:
username:password_hash
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.
If you encounter the below error while running the command chpasswd:
chpasswd : command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install passwd |
Ubuntu | apt-get install passwd |
Alpine | apk add shadow |
Arch Linux | pacman -S shadow |
Kali Linux | apt-get install passwd |
Fedora | dnf install shadow-utils-2 |
Raspbian | apt-get install passwd |
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