Creating users and managing passwords is one of the primary responsibilities of a system administrator. A good policy requires modification of passwords on regular intervals. But changing the passwords for a number of users with passwd command becomes difficult. In this article we provide a method to change the passwords in batch mode.
1. The chpasswd command:
The command chpasswd is used to modify the passwords in batch mode. Only root user can execute this command. It takes the passwords in the following format:
This syntax and usage of the command will be explained shortly. For this article, we consider following 5 users for which passwords will be modified:
We provide here two methods in which chpasswd command can be used to modify the passwords in batch mode:
1.1 Method1: (STDIN)
In this method, just issue the command chpasswd and then it will prompt for the user passwords. The passwords will be given in the format shown above.
After you have entered all the users and passwords, you need to hit ctrl+d to tell the chpasswd command that this is end of input. You will get back your prompt after hitting ctrl+d and the passwords will be changed.
1.2 Method2: (Input from file)
You can alternatively maintain a file that contains all the users and the passwords. We can then redirect the input from this file.
Let us go ahead and create a file with cat command (You can use other methods to create the file as well like vi or emacs editor):
Now all we need to do is issue the command chpasswd with input redirected from the file:
2. Command options:
2.1 Providing encrypted passwords:
There are various useful options to this command. You can provide the passwords already encrypted with -e or --encrypted option.
2.2 MD5 passwords:
The -m or --md5 option will encrypt the password using md5 algorithm.
2.3 Other encryption methods:
The encryption algorithm to be used for passwords can be provided with -c or --crypt-method option. In the example, we use SHA512 as our encryption algorithm:
2.4 Command help:
As with most commands, the -h or --help will print out the command usage for chpasswd command.