Linux chpasswd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we explore the Linux chpasswd command, which allows you to change user passwords in batch mode. The lab covers understanding the chpasswd command, changing user passwords in batch mode, and automating password changes using shell scripts. This can be particularly useful in corporate environments where you need to regularly update passwords for a large number of users. The lab provides practical examples and step-by-step instructions to help you effectively manage user passwords in a Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/cat -.-> lab-422600{{"`Linux chpasswd Command with Practical Examples`"}} linux/echo -.-> lab-422600{{"`Linux chpasswd Command with Practical Examples`"}} linux/mkdir -.-> lab-422600{{"`Linux chpasswd Command with Practical Examples`"}} linux/ls -.-> lab-422600{{"`Linux chpasswd Command with Practical Examples`"}} linux/sudo -.-> lab-422600{{"`Linux chpasswd Command with Practical Examples`"}} linux/touch -.-> lab-422600{{"`Linux chpasswd Command with Practical Examples`"}} end

Understanding the chpasswd Command

In this step, we will explore the chpasswd command in Linux, which allows you to change user passwords in batch mode. The chpasswd command reads a list of user and password pairs from standard input and uses this information to update the passwords.

First, let's create a file with a list of user and password pairs:

labex:newpassword1
labuser:newpassword2

Now, we can use the chpasswd command to update the passwords for these users:

cat users_passwords.txt | sudo chpasswd

Example output:

labex:newpassword1
labuser:newpassword2

The chpasswd command reads the user and password pairs from the users_passwords.txt file and updates the passwords accordingly. Note that the passwords are provided in plain text, so this method should be used with caution and only in secure environments.

Automating Password Changes with Shell Scripts

In this final step, we will learn how to automate the process of changing user passwords using shell scripts. This can be useful when you need to regularly update passwords for a large number of users, such as in a corporate environment.

First, let's create a simple shell script that uses the chpasswd command to change user passwords:

#!/bin/bash

## Define the list of users and their new passwords
users_and_passwords=(
  "labex:newpassword1"
  "labuser:newpassword2"
)

## Loop through the list and update the passwords
for user_and_password in "${users_and_passwords[@]}"; do
  user=$(echo "$user_and_password" | cut -d':' -f1)
  password=$(echo "$user_and_password" | cut -d':' -f2)
  echo "$user:$password" | sudo chpasswd
done

Save this script as update_passwords.sh in the ~/project directory and make it executable:

chmod +x ~/project/update_passwords.sh

Now, you can run the script to update the passwords for the users:

~/project/update_passwords.sh

Example output:

labex:newpassword1
labuser:newpassword2

The script reads the list of users and their new passwords, then uses the chpasswd command to update the passwords in batch mode.

Summary

In this lab, we learned how to use the chpasswd command in Linux to change user passwords in batch mode. We created a file with a list of user and password pairs, and used the chpasswd command to update the passwords accordingly. Additionally, we explored how to automate the process of changing user passwords using a shell script, which can be useful in corporate environments where passwords need to be regularly updated for a large number of users.

Finally, we learned how to create a shell script that uses the chpasswd command to change user passwords, and how to run the script to update the passwords for the users.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like