Different ways to Update/Change users password in Linux

It’s a basic thing to set a user password whenever you create an user account in Linux.

Everybody uses passwd command followed by the user name passwd USERNAME to set a password for a user.Make sure you have to set a hard and guess password that will help you to make the system more secure.I mean to say, it should be the combination of Alphabets, Symbols and numbers.

Also, we recommend  to change the password at least once in a month for security reason.

If you are working as a Linux admin you might have got the below questions many times in your mind.

  • How to Update/Change Users Password in Single Command?
  • How to Update/Change a Same Password for Multiple users in Linux?
  • How to Update/Change Multiple Users Password in Linux?
  • How to Update/Change Password for Multiple Users in Linux?
  • How to Update/Change Different Password for Multiple Users in Linux?
  • How to Update/Change Users Password in Multiple Linux Servers?
  • How to Update/Change Multiple Users Password in Multiple Linux Servers?

When you use the passwd command it will ask you to enter the password twice to set it. It’s a native method to set a user password.

If you don’t want to update the password twice and would like to do this in different way? Let us see in this article

Method-1: Using passwd Command

passwd command is a standard method to set or update or change password for users in Linux. The below way is a standard method to do it.

# passwd renu
Changing password for user renu.
New password: 
BAD PASSWORD: The password contains the user name in some form
Retype new password: 
passwd: all authentication tokens updated successfully.

Run the following command if you would like to set or change password with single command. This allow users to update password in a single command.

# echo "new_password" | passwd --stdin thanu
Changing password for user thanu.
passwd: all authentication tokens updated successfully.

Method-2: Using chpasswd Command

chpasswd is an another command will allow us to set or update or change password for users in Linux. Use the following format if you would like to use chpasswd command to change password for user in a single command.

# echo "thanu:new_password" | chpasswd

Method-3: How to set Different Password for Multiple Users

Use the below script if you would like to set or update or change a password for multiple users in Linux with different password.

To do so, first we need to get a users list by using the following command. The below command will list the users who’s having /home directory and redirect the output to user-list.txt file.

# cat /etc/passwd | grep "/home" | cut -d":" -f1 > user-list.txt

List out the users using cat command. Remove the user from the list if you don’t want to reset the password for the specific user.

# cat user-list.txt
centos
magi 
daygeek 
thanu 
renu

Create a small shell script like below to achieve this.

# vi password-update.sh

#!/bin/sh
for user in `more user-list.txt`
do
echo "$user@123" | passwd --stdin "$user"
chage -d 0 $user
done

Set an executable permission to password-update.sh file.

# chmod +x password-update.sh

Finally run the script to achieve this.

# ./password-up.sh

magi
Changing password for user magi.
passwd: all authentication tokens updated successfully.
daygeek
Changing password for user daygeek.
passwd: all authentication tokens updated successfully.
thanu
Changing password for user thanu.
passwd: all authentication tokens updated successfully.
renu
Changing password for user renu.
passwd: all authentication tokens updated successfully.

Method-4: How to set same password for multiple users

Use the below script if you would like to set or update or change a same password for multiple users in Linux.

# vi password-update.sh

#!/bin/sh
for user in `more user-list.txt`
do
echo "new_password" | passwd --stdin "$user"
chage -d 0 $user
done

Method-5: How to change user password in multiple servers

Use the following script if you want to change a user password in multiple servers. In my case, we are going to change a password for renu user. Make sure you have to give the user name for which you want to update the password.

Make sure you have to update the servers list into server-list.txt file. Each server should be in separate line.

# vi password-update.sh

#!/bin/bash
for server in `cat server-list.txt`
do
ssh root@$server 'passwd --stdin renu <<EOF
new_passwd
new_passwd
EOF';
done

You will be getting the output similar to us.

# ./password-update.sh

New password: BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: Changing password for user renu.
passwd: all authentication tokens updated successfully.
New password: BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: Changing password for user renu.
passwd: all authentication tokens updated successfully.

Method-6: How to change user password in multiple servers

Use the following script if you want to change a user password in multiple servers. In my case, we are going to change a password for user1 user. Make sure you have to replace your user instead of us.

Make sure you have to update the servers list into server-list.txt file. Each server should be in separate line.

$ cat server-list.txt 
192.168.1.8
192.168.1.9
192.168.1.10

Use the following script to achieve this.

# vi password-update.sh

#!/bin/bash
for server in `cat server-list.txt`
do
echo -e "Server IP is: $server"
ssh root@$server 'echo "new_passwd" | passwd --stdin user1'
done

You will be getting the output similar to us.

# ./password-update.sh

Server IP is: 192.168.1.8
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Server IP is: 192.168.1.9
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Server IP is: 192.168.1.10
Changing password for user user1.
passwd: all authentication tokens updated successfully.

Method-7: How to change user password in multiple servers using pssh Command

pssh is a program for executing ssh in parallel on a number of hosts. It provides features such as sending input to all of the processes, passing a password to ssh, saving output to files, and timing out. Navigate to the following link to know more about PSSH Command.

# pssh -i -h /tmp/server-list.txt "printf '%s\n' new_pass new_pass | passwd --stdin root"

You will be getting the output similar to us.

[1] 07:58:07 [SUCCESS] CentOS.2daygeek.com
Changing password for user root.
passwd: all authentication tokens updated successfully.
Stderr: New password: BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
[2] 07:58:07 [SUCCESS] ArchLinux.2daygeek.com
Changing password for user root.
passwd: all authentication tokens updated successfully.
Stderr: New password: BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple

Method-8: How to change user password in multiple servers using chpasswd Command

Alternatively we can use the chpasswd command to update the password for user in multiple servers.

# ./password-update.sh

#!/bin/bash
for server in `cat server-list.txt`
do
ssh root@$server 'echo "magi:new_password" | chpasswd'
done

Method-9: How to change multiple users password in Linux servers using chpasswd Command

To do so, first create a file and update username and password in the below format. In my case I have created a file called user-list.txt.

See the details below.

# cat user-list.txt
magi:new@123
daygeek:new@123
thanu:new@123
renu:new@123

Create a following small shell script to achieve this.

# vi password-update.sh

#!/bin/bash
for users in `cat user-list.txt`
do
echo $users | chpasswd
done

Method-10: How to change multiple users password in multiple Linux servers using chpasswd Command?

To do so, create the following files and update the required information.

Make sure you have to update the servers list into server-list.txt file. Each server should be in a separate line.

$ cat server-list.txt
192.168.1.8
192.168.1.9

The same way you have to update the users list into user-list.txt file. Each user should be in a separate line.

$ cat user-list.txt
user1:User@1
user2:User@2

Use the following script to achieve this.

$ vi pass-multi-user.sh

#!/bin/bash
for server in `cat server-list.txt`
do
for user in `cat user-list.txt`
do
echo -e "Server IP is: $server"
ssh root@$server "echo '$user' | chpasswd"
done
done

You will be getting the output similar to us when you ran the script.

$ sh pass-multi-user.sh
Server IP is: 192.168.1.8
Server IP is: 192.168.1.8
Server IP is: 192.168.1.9
Server IP is: 192.168.1.9

Method-11: How to change multiple users password in multiple Linux servers using passwd command?

Use the following script if you want to change the multiple users password in multiple servers. In my case, we are going to change a password for user1 and user2 users. Make sure you have to replace your users name instead of ours.

Make sure you have to update the servers list into server-list.txt file. Each server should be in a separate line.

$ cat server-list.txt
192.168.1.8
192.168.1.9

The same way you have to update the users list into user-list.txt file. Each user should be in a separate line.

$ cat user-list.txt
user1
user2

Use the following script to achieve this.

$ vi pass-multi-user.sh

#!/bin/bash
for server in `cat server-list.txt`
do
for user in `cat user-list.txt`
do
echo -e "Server IP is: $server"
ssh root@$server "echo 'User@123' | passwd --stdin '$user'"
echo ""
done
done

You will be getting the output similar to us when you ran the script.

$ sh pass-multi-user.sh
Server IP is: 192.168.1.8
Changing password for user user1.
passwd: all authentication tokens updated successfully.

Server IP is: 192.168.1.8
Changing password for user user2.
passwd: all authentication tokens updated successfully.

Server IP is: 192.168.1.9
Changing password for user user1.
passwd: all authentication tokens updated successfully.

Server IP is: 192.168.1.9
Changing password for user user2.
passwd: all authentication tokens updated successfully.

About Vinoth Kumar

Vinoth Kumar has 3.5+ years of experience in Linux server administration & RHEL certified professional. He is currently working as a Senior L2 Linux Server administrator.

View all posts by Vinoth Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *