What is chpasswd Command in Linux with Examples

Linux systems rely on strong password management to maintain security and prevent unauthorized access. Passwords render as the primary line of defense against potential breaches. Regularly updating Linux passwords not only protects user accounts but also reduces the chances of attacks exploiting outdated credentials.
For administrators, manually changing multiple passwords is time-consuming. The chpasswd command simplifies this process. It allows bulk updates of user passwords using the Linux password file. This is specifically helpful when managing systems with numerous accounts. By using chpasswd, you can update Linux passwords from standard input or from a file, such as passwd.txt.
This guide will explore the syntax, options, and various chpasswd examples. You’ll learn how the Linux changepassword utility enhances the efficiency of managing user accounts. It’s a vital tool for improving password security with minimal effort.
Definition of chpasswd Command in Linux

The chpasswd command in Linux is a versatile system administration utility. It enables bulk updates of user passwords, an activity that would otherwise need considerable manual effort. The command reads username-password pairs, provided either through standard input or a file such as passwd.txt, to update the Linux password file efficiently.
This Linux changepassword tool is particularly helpful in environments with many user accounts, such as corporate servers, academic systems, or multi-user setups. For instance, administrators can manage all Linux passwords in one go instead of modifying them individually with the passwd command in Linux.
The tool supports encryption methods like SHA256, SHA512, or MD5. These options enhance security by ensuring passwords are hashed before storage. Its versatility as well as ease of usage make the chpasswd command a vital part of Linux password management. It does not matter if you are administering some users or hundreds, this command simplifies the process and improves security.
chpasswd syntax
The syntax for the chpasswd command is straightforward:
chpasswd [options]
The input is in the format of username:password pairs. These pairs can be provided directly via standard input (passwd stdin) or from a file like passwd.txt. This syntax is simple yet powerful, making it a go-to tool for password updates in Linux.
Examples of Input Formats:
Standard Input:
echo "user1:password123" | sudo chpasswd
This command updates the password for user1 using passwd stdin.
File-Based Input:
Create a file (passwd.txt) with username-password pairs:
user1:password123
user2:password456
Then run:
sudo chpasswd < passwd.txt
This updates passwords for all users listed in the file.
– chpasswd Options
The chpasswd command in Linux comes with several options that enhance its functionality and security during password updates. Below is a list of common options you can use when executing the command:
-e: Specifies that the passwords provided in the input are already encrypted. This prevents the command from attempting to hash them again. It’s useful when the password input is already in an encrypted format like MD5 or SHA512.
-m: Enables MD5 hashing for passwords. This option is helpful when you prefer to use the MD5 algorithm for password encryption, though it is considered less secure than more modern hashing methods like SHA512.
-s <hash_algorithm>: Defines the hashing algorithm used for password encryption. You can use algorithms like SHA256, SHA512, or others as per your security necessities. For instance:
chpasswd -s SHA512
This ensures that passwords are hashed using the SHA512 algorithm, which is commonly used for enhanced security.
–help: Shows a list of all available choices for the chpasswd command. This is a quick way to see the full range of functionalities and how to use them properly.
-i <file>: Specifies a file containing username and password pairs in the username:password format. This allows you to update passwords in bulk from a file rather than inserting them manually.
sudo chpasswd -i passwd.txt
This updates the passwords of users listed in the passwd.txt file.
-R <directory>: This option allows you to specify a different chroot directory. It can be used in restricted environments or when the password file is located elsewhere on the system.
sudo chpasswd -R /home/username
-v: Increases verbosity by showing more detailed output during execution. It’s helpful for troubleshooting or confirming the exact changes being made to the password file.
chpasswd Examples

Update Passwords from Standard Input
The chpasswd command allows you to update Linux passwords using standard input, making it ideal for scripting or quick manual updates. By piping username-password pairs into the command, you can easily update passwords for one or more users.
For a single user, you can use:
echo "user1:password123" | sudo chpasswd
This updates user1’s password. The passwd stdin method is also efficient for batch updates. For example, to update multiple users at once:
echo -e "user1:password123\nuser2:password456" | sudo chpasswd
Alternatively, you can use a file like passwd.txt containing username:password pairs:
sudo chpasswd < passwd.txt
This method saves time and ensures accuracy, especially when updating multiple Linux passwords. Whether updating a few or many accounts, the chpasswd command streamlines the process, improving efficiency and security.
Update Passwords from a File
Updating passwords from a file is a highly efficient method for managing bulk password changes. You can construct a file, such as passwd.txt, containing multiple username:password pairs, each on a new line. This approach is perfect for administrators needing to update passwords for many users quickly.
Here’s an example of what passwd.txt might look like:
user1:password123
user2:password456
user3:password789
Once your file is ready, you can update all the passwords at once using the following command:
sudo chpasswd < passwd.txt
This command reads the passwd.txt file and updates the passwords for each user listed in it. This bulk updating method is much more efficient than changing each password individually, especially on large systems with many users.
By using a file, you can easily manage password changes in a systematic way, which is essential for maintaining security in environments with hundreds or even thousands of user accounts. For instance, if you’re managing a Linux password file for a large organization or server farm, this method reduces the risk of errors and minimizes the time needed for password updates.
The chpasswd command in Linux, when used with a file like passwd.txt, allows for quick, secure updates to user passwords without needing to interact with each account manually, saving administrators time and effort while ensuring consistency across the system.
Utilize Different Encryption Methods when Updating Passwords
By default, the chpasswd command employs SHA512 encryption for passwords, providing a secure hashing algorithm. However, if you desire to utilize a other encryption approach, such as SHA256, the command allows for flexibility. This is helpful when you are required to sync specific security policies or compatibility necessities.
For example, to explicitly set the SHA256 encryption method for password updates, you can run:
echo "user1:password123" | sudo chpasswd -s SHA256
In this case, the -s option specifies the hashing algorithm, and SHA256 replaces the default SHA512. This allows administrators to customize password security based on system requirements or preferences. Using different hashing algorithms adds flexibility, enabling you to adapt to different security standards or legacy systems.
This method is particularly valuable in environments where you need to ensure consistency across various systems that may be using different encryption protocols. Whether you’re working with modern or legacy systems, chpasswd provides the necessary tools to manage Linux passwords with the encryption method that best suits your needs.
Switch to MD5 Encryption
While more secure algorithms like SHA512 and SHA256 are commonly used today, some systems may still require MD5 encryption for compatibility reasons. The chpasswd command in Linux supports this older method, allowing administrators to configure passwords using MD5 when needed.
To update a password using MD5 encryption, use the -m option:
echo "user1:password123" | sudo chpasswd -m
This command configures the password for user1 using MD5 hashing. While MD5 is considered less secure compared to newer hashing algorithms, it remains supported in Linux password management for compatibility with older systems. For example, if you’re working with legacy systems that require MD5 hashes for authentication, this method ensures that your password management processes remain functional.
Despite the vulnerabilities associated with MD5, its continued support in chpasswd allows administrators to maintain compatibility across different systems. It’s important to note that using MD5 should be avoided for new systems or when stronger encryption methods are required. For most modern Linux setups, SHA512 or SHA256 is recommended due to their superior security.
Also Read: What is AWK Command in Linux? Explain with Examples
Final Words
The chpasswd command is an essential tool for administrators who manage Linux systems. It allows bulk updates to the Linux password file, saving time and guaranteeing consistency. With options to encrypt passwords using different algorithms, chpasswd provides flexibility in enforcing strong password security.
Whether you are updating a single user or managing hundreds of accounts, the Linux changepassword process becomes efficient with this tool. Using files like passwd.txt or passwd stdin input, you can handle password updates with ease.
Integrating chpasswd into your workflows helps secure Linux systems effectively. It ensures robust password management, reduces manual errors, and supports various encryption methods for added security. The command is an indispensable part of maintaining safe and efficient Linux environments. To strengthen your system’s defenses, start exploring the potential of this Linux password command today.