I have a group of users that have the same account on a few of my Linux systems. They seem to forget their passwords at least once every two months and like to blame my password complexity rules. I am tasked every so often with changing a users password on multiple systems which really is a pain. Here we will examine a nice way to change a users password with a one line command. We will also talk about how we can do this remotely on multiple servers.
First, let’s look at how we can change a users password in one line using echo and the pipe.
echo -e "'NEWPASS'\n'NEWPASS'" | passwd USERNAME
NOTE: There are doube quotes ( " ) surrounding the passwords, but each password is wrapped in a single quote ( ' ) to allow for special characters.
Here we use echo with the “-e” switch. This tell echo to read the “\n” as a newline. Then is is piped into the passwd “USERNAME” command.
We can also use this to change the password on a remote machine with ssh.
ssh root@server 'useradd newuser; echo -e "'NEWPASS'\n'NEWPASS'" | passwd USERNAME'
This assumes your allowing ssh from root or a user with elevated privileges, which is not recommended for security reasons.
You can also run this command (or any command) on multiple servers using a loop. So for this example we will say we have several servers named server1 through server8. What I would do is make a text file containing the server names, one per line like so:
server1
server2
server3
server4
server5
server6
server7
server8
Now we can use a for loop to loop through the lines in the file and connect to each machine.
for i in `cat filename.txt`; do ssh $i 'echo -e "'NEWPASS'\n'NEWPASS'" | passwd USERNAME'; done
There are easier ways to accomplish this if your servers are actually named server1 through server8. But in the real world I doubt your servers and named so conveniently.
Related Articles
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
25 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
the for script in the end, worked just fine. Thanks. Exactly what i needed.
Thanks for this, you saved me a lot of hassle and I've learn something new.
not working for rhel 4 though.
No this will not work on rhel 4 which is end of life and should not be used in any production environment.
If the above solution is not working, use this one in the loop:
printf 'NewPasswordnNewPassword' | passwd username
Did you lost a password that just so happens to also be saved in WinSCP?
The first command is what you need, as you know that WinSCP only allows one-liners to jump in.
You saved me a lot of time 🙂
Thank you, this script worked perfectly for an Ubuntu 14.04 Server.
-Blake
What if I have normal user access on each remote server and I want to change all remote servers
password which contains same username. I think it will ask for password if haven’t configure password less auth.
In this case what command helps.
Thanks in advance
You need to set up SSH keys without a passphrase first:
https://www.putorius.net/2011/12/how-to-setup-ssh-key-authenitication.html
Then you will be able to change your password on the remote machine without it asking for a password.
thanks a lot. Have a nice day........
I am saving all the server IP in a file (server_ip.txt) as suggested above.
And providing root password for each entry in the server_ip.txt.
However, I am getting below errors:
Permission denied (publickey,gssapi-with-mic,password).
Note: I am running the script being a root user for changing the password of a normal user.
Do you have SSH keys setup for the root user?
Its giving token manipulation error while I'm trying to change password of normal user on multiple servers. I'm logging with a normal user and SSH keys without a passphrase configured.
A normal user can not change another users account password. You will need to be root or elevated privileges to do that.
But ssh keys configured for normal user & same username exist on all servers. Is this not enough? Please guide.
Thanks in advance
Oh so you are trying to change your own password? What version of Red Hat are you using?
RHEL 6.3 I want to change all remote servers password which contains same username and on one of them ssh keys configured for all servers.
It should work without an issue, can you post the command you are using?
for i in `cat filename.txt`; do ssh $i 'echo -e "NEWPASSnNEWPASS" | passwd'; done
Do you have any special characters in the password you are using?
The script on the website above is for root. It would have to be modified to work for a regular user because you have to enter your current password before changing your password.
Hi,
I have a Solaris based machine and I want to create a java app that connects to the machine and changes the password. I figured out how to connect to the machine and send commands to be executed. I want to change my password for this server(I am not root user) and in order to change it via java app that i have created i need to send the command in only one line. I tried different methods but it doesn't work.
It first asks for the old password then new password then confirm the new password.
Do you have any idea how to do it? How to write the change password command in only one line ?
Thanks a lot. Exactly what I was looking for.
I've same user on 12 linux servers. Passwordless ssh is setup from one of the server to all the other servers. I'm trying to change the password using below command but it is giving me "(current) UNIX password: passwd: Authentication token manipulation error." I'm using RHEL 6.6.
Command:
for i in `cat servers.txt` do ssh $i 'echo -e "oldpasswordnnewpasswordnnewpassword" | passwd'; done;
In the server.txt file I've mentioned the IP details of other server.
Please help me on this.
try putting the username like so:
for i in `cat servers.txt`; do ssh username@$i 'oldpasswordnnewpasswordnnewpassword" | passwd'; done
also, watch you semi colons. You are missing on before "do"
I am saving all the server IP in a file (server_ip.txt) as suggested above.
And providing root password for each entry in the server_ip.txt.
However, I am getting below errors:
Permission denied (publickey,gssapi-with-mic,password).
Note: I am running the script being a root user for changing the password of a normal user.
Kindly suggest.