Linux chpasswd command

In your organization, as a policy you might need to change the password of a lot of users on a regular basis. And changing password of each user with passwd command is a daunting task. But don't worry, chpasswd command is a lifesaver for you.

chpasswd command reads a list of username and password separated by colon from the standard input or file, and then encrypt as per the options specified and then set it for user account.

username and password pair must be of the following format-

username:password

Note: chpasswd command can only be executed by root user.

Syntax of Linux chpasswd command

chpasswd [options]

Command line options of Linux chpasswd command

Options Description
-S Displays encrypted password to STDOUT instead of changing the passwd file.
-c METHOD Uses the specified method to encrypt the password. Methods that are available- MD5, DES, SHA256, SHA512, and NONE.
-m Encrypts the password using MD5 algorithm.

Example of Linux chpasswd command

It is always better to practice Linux commands by following the instructions mentioned in the examples.

Use standard input to provide username and password

When you provide a list of login name and password from the standard input then you must follow the below syntax-

#chpasswd
username1:password1
username2:password2
...
usernamen:passwordn
ctrl+d

Once you enter all the username and password, you have to press ctrl + d to inform chpasswd command that you have finished entering the list.

Suppose, we want to change password of 6 users whose username are- govind, gopal, vrinda, priya, vishnu, surbhi. So, run the below command-

#chpasswd
govind:govindpass
gopal:gopalpass
vrinda:vrindapass
priya:mystyle
vishnu:lotus
surbhi:cheese

After entering these hit ctrl + d to tell chpasswd command that you are done with the list.

Using file to provide username and password

Instead of entering the list to standard input, we can save the list of users in a file and provide that file to chpasswd. Syntax of doing this-

#chpasswd < usernamepasswordfile

Let's enter the above list in a mypassword.txt file.

#cat mypassword.txt
govind:govindpass
gopal:gopalpass
vrinda:vrindapass
priya:mystyle
vishnu:lotus
surbhi:cheese

Now, we will run chpasswd command like this-

#chpasswd < mypassword.txt

If you want to encrypt the password using MD5 algorithm then run the below command-

#chpasswd -m < mypassword.txt

If you want to encrypt the password using SHA256 algorithm then run the below command-

#chpasswd -c SHA256 < mypassword.txt