chpasswd Command in Linux with Examples

January 25, 2023

Introduction

The chpasswd command allows updating the passwords of multiple users one at a time or in bulk. Even though the passwd command is a go-to tool for changing individual Linux user passwords, the chpasswd utility is helpful when multiple changes are necessary.

This tutorial will show you how to use the chpasswd command in Linux with examples.

chpasswd Command in Linux with Examples

Prerequisites

  • A Linux system (this tutorial uses Ubuntu 22.04).
  • Access to the terminal.
  • Root or sudo privileges.
  • A text editor of your choice (this tutorial uses Vim).

chpasswd syntax

The basic syntax for the chpasswd command is:

chpasswd [options]
[username]:[password]

The chpasswd works without any options, as long as the user provides usernames and passwords as shown in the syntax. Arguments are not mandatory, but they provide additional functionality.

chpasswd Options

Sysadmins use several options to adjust how chpasswd works. The most common options are listed below:

OptionDescription
-e, --encryptedEncrypts passwords before storing in the password file.
-cValidates the password before it is stored.
-m, --md5Encrypts the password using the MD5 algorithm.
-RSpecifies the password file location.
-SDisplays the encrypted password to standard output instead of modifying the password file.
-c, --crypt-methodSpecifies the method to be used for encrypting the password. Available methods include MD5, DES, SHA256, SHA512, and NONE.
-pSpecifies the prefix for the crypt(3) algorithm, such as $6$ for SHA512-CRYPT, $5$ for SHA256-CRYPT, and $2a$ for Blowfish.
-s, --sha-roundsUses the Blowfish encryption algorithm for the password with a minimum value of 1000 and a maximum value of 999,999,999. This option only works with the SHA256 or SHA512 crypt method.
-h, --helpDisplays the help message and exits.

chpasswd Examples

The chpasswd command is the primary tool for bulk-managing passwords on Linux systems. Therefore, the utility is useful for setting up a new system or resetting hundreds of passwords. The following instructions provide practical examples of using the chpasswd command.

Note: The chpasswd command updates passwords for current users. To add a new user, use the adduser command. Running chpasswd for nonexistent users results in an error message.

Update Passwords from Standard Input

When used without any options, chpasswd reads a list of user names and corresponding new passwords from standard input and updates the system's password database with the new values.

To use chpasswd in this way, follow these steps:

1. Run the chpasswd command as sudo without any options:

sudo chpasswd

2. Provide the list of current user names and new passwords to be updated. The syntax is:

username:password

For example, to update passwords for three existing users, pnapuser1, pnapuser2, and pnapuser3, run:

pnapuser1:newpassword1

pnapuser2:newpassword2

pnapuser3:newpassword3
sudo chpasswd input passwords

Note: Do not use common words as passwords. When that happens, the terminal prints an error: BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word.

3. Hit ctrl+d to confirm that the list is complete.

Update Passwords from a File

Another way to bulk-update passwords with chpasswd is to create a file with current usernames and passwords to be updated. The command reads data from the file and not from the standard input.

To update passwords this way, follow these steps:

1. Create a file in Vim called mypasswords.txt with:

vim mypasswords.txt

2. List the three usernames with new passwords in the document:

pnapuser1:newpassword01

pnapuser2:newpassword02

pnapuser3:newpassword03
update usernames and passwords in vim

3. Save and exit the file.

4. Verify the file contents with cat:

cat mypasswords.txt
The cat command mypasswords terminal output

5. Run chpasswd by redirecting data from the mypasswords.txt file using <:

sudo chpasswd < mypasswords.txt
sudo chpasswd mypasswords terminal output

The command prints no output.

Use Different Encryption Methods when Updating Passwords

By default, the chpasswd command uses the Pluggable Authentication Modules (PAM) library to authenticate users and encrypt passwords. Alternative encryption methods like bcrypt and SHA-512 exist, but are considered less secure.

To change the encryption method, use the argument -c with chpasswd. For instance, to change the encryption method from PAM to NONE, follow these steps:

1. Execute the command with the appropriate arguments:

sudo chpasswd -c NONE
sudo chpasswd c terminal output

2. Input the username and password to update:

pnapuser1:newpassword11

pnapuser2:newpassword12

pnapuser3:newpassword13
sudo chpasswd -c input usernames and passwords

3. Hit ctrl + d to complete the entry.

Switch to MD5 Encryption

Use a predetermined argument as a shortcut to changing encryption. For instance, to switch to the MD5 algorithm, follow these steps:

1. Run the -m argument with chpasswd:

sudo chpasswd -m

2. Provide username and password.

pnapuser:newpassword111
sudo chpassw -m input username and password terminal output

3. Hit ctrl + d to finish the entry.

Conclusion

After reading this tutorial, you know how to change passwords for multiple users at once using the chpasswd command.

Next, learn how to reset or change the root password in Ubuntu.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Change Sudo or Root Password in Ubuntu
April 16, 2024

Are you looking to change the root password in Ubuntu? Changing passwords is a good practice and should be done periodically. Linux allows multiple user accounts...
Read more
21 Server Security Tips to Secure Your Server
January 11, 2023

Hackers are always on the lookout for server vulnerabilities. Minimize risks and be confident your data is safe on secure servers by implementing our...
Read more
useradd vs. adduser: What Are the Differences?
September 1, 2022

Linux provides two commands to add users to the system: useradd and adduser. Learn about the difference between these two commands and when to use which...
Read more
How to Use the usermod Command in Linux
March 4, 2021

The usermod command modifies user account details: username, password, home directory location, shell, and more. This tutorial will show you how...
Read more