- Unix Commands Reference
- Unix Commands - Home
chpasswd Command in Linux
chpasswd is a command used in Linux to change passwords for multiple users simultaneously. It reads a list of usernames and passwords associated with them from the standard input. After that, it updates the password database accordingly. The chpasswd command is extremely useful in cases when you have to do multiple changes in a quick way.
Table of Contents
Here is a comprehensive guide to the options available with the chpasswd command −
- How to Install chpasswd Command in Linux?
- Syntax for chpasswd Command
- Different Options Available for chpasswd Command
- Examples of chpasswd Command in Linux
How to Install chpasswd Command in Linux?
The chpasswd command is a core command of the passwd package that is pre-installed in most Linux distributions. You can verify the presence of this command on your Linux system by running the following command −
which chpasswd
If, for some reason, you have accidentally removed this command, you can add it back by installing the passwd package on your system.
For systems like Ubuntu, Debian that are using the apt package manager, simply use the below-given command to install the passwd package −
sudo apt install passwd
For systems like CentOS and Fedora, you can use the yum package manager to install the passwd package on your system −
sudo yum install passwd
Syntax for chpasswd Command
The basic syntax to use the chpasswd command in Linux is provided below −
chpasswd [options] [username]:[password]
The options enhance the functionality of the command, however, you can omit them if you want to provide usernames and passwords directly in the syntax.
Different Options Available for chpasswd Command
Here is a table that summarizes the common options for the chpasswd command in Linux −
Option | Description |
---|---|
-e, --encrypted | Encrypts passwords before storing them in the password file. |
-m, --md5 | Encrypts the password via the MD5 algorithm. |
-c, --crypt-method | Specifies the method for encrypting the password (options: MD5, DES, SHA256, SHA512, NONE). |
-R, --root | Specifies the password file location. |
-s, --sha-rounds | Uses the Blowfish encryption algorithm for the password that has a minimum value of 1000. |
-h, --help | Displays the help with the information of command and exits. |
Examples of chpasswd Command in Linux
Let’s explore a few examples of chpasswd command in Linux −
- Update Passwords from Standard Input
- Update Passwords from a Text File
- Update Password for a Specific User
- Update Password Using Different Encryption Method
Update Passwords from Standard Input
The basic function of the chpasswd command is to update the password from the standard input, it can be done by using the chpasswd command without any options. It will read a list of usernames and their corresponding updated passwords from standard input. After that, it updates the system’s password database with the new rules that you set during the process.
sudo passwd
Once the above command is executed, you will then be above to provide your current user names and new passwords to be updated in the following way −
username:password
For example, if you want to update the password for an existing user ubuntu, you can do it in the following way −
To update the password of multiple usernames, simply press enter after entering the first username and then write another username with password in a similar fashion. In this way, you can update passwords of multiple usernames simultaneously.
Once done, you can use CTRL+d to save the changes.
Update Passwords from a Text File
You can also update the password using the chpasswd command from a text file as well. For that purpose follow the below-given steps −
Step 1 − First create a text file according to your choice that contains the usernames and their corresponding new passwords. As an example, we have created a file called password.txt.
Step 2 − Inside the file, add the information like usernames and passwords in the following format −
username1:password username2:password username3:password
Step 3 − Save the user information file using CTRL+X, add Y and press Enter. Once, done, execute the below-given command to run the password.txt file using chpassword command to update the password −
sudo chpasswd < password.txt
Update Password for a Specific User
You can also update the password for a specific user directly by piping the echo command with the chpasswd command. For example, if your user is ubuntu and want to update its password directly, you can simply use the below-given command −
echo ubuntu:ab01-wlc | sudo passwd
Note − Ensure using a strong password because the simplest password won’t work here.
Update Password Using Different Encryption Method
By default, chpasswd uses PAM (Pluggable Authentication Modules) to encrypt passwords. When you provide passwords in clear text, chpasswd automatically encrypts them using PAM, for example −
echo username:password | sudo chpasswd
To use md5 encryption instead of PAM, you can use the following command −
sudo chpasswd -m
Then add your specified username and password associated with it.
You can also use a specific encryption method by adding the -c option. For example, if you want to use SHA25 encryption method, simply use the following command −
sudo chpasswd -c SHA256
The rest of the process of updating the password is similar to the one we already covered in the previous examples.
Conclusion
chpasswd is a powerful command that helps you change the password of multiple users simultaneously. You can use this command directly to update your usernames’ passwords from standard output or from a text file. Apart from that, you can also update the password for a specific user or use different encryption methods to change your password for specified usernames.
This tutorial covered the syntax and usage of the chpasswd command in Linux with examples.