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 [email protected] '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:
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.
This site uses Akismet to reduce spam. Learn how your comment data is processed.