Changing Linux User’s Password in One Command Line
I frequently create new user accounts and change or set password for these accounts on a batch of Linux boxes. The create new user can be done by one command line. The problem is to change the password. In Linux, we use passwd to change password, but passwd requires input from stdin to get the new password. With the help of pipe and a little tricky, we can change user’s password in one command line. This will save much time especially when creating a batch of user accounts.
We use one example to introduce how to change Linux user’s password in one command line. Suppose we login as root and want to change user linuxuser‘s password to linuxpassword.
The passwd command asks for the new password twice. And these two inputs (the same password) is separated by one “Enter”. We can emulate this by the echo command with ‘-e’ option set. When ‘-e‘ is in effect, ‘\n‘ in echo’s input is echoed as “new line”.
So to change the password in our example, we just execute this one command:
# echo "linuxpassword" | passwd --stdin linuxuser
on modern Linux. (Thanks to DAVID for this tip)
or
# echo -e "linuxpassword\nlinuxpassword" | passwd linuxuser
This can also be put into one bash script or executed on remote node by the
ssh command. For example, we can change the password of linuxuser on a batch of servers (100 servers: 10.1.0.1 to 10.1.0.100) by:
# for ((i=1;i<=100;i++)); do ssh 10.1.0.$i 'echo -e "linuxpassword\nlinuxpassword" | passwd linuxuser'; done;
Even further, we can create one user and set its initial password remotely by:
# ssh remoteserver 'useradd newuser; echo -e "passwdofuser\npasswdofuser" | passwd newuser'
It’s working if this apply directly through command line..
UBUNTU 12.10
echo -e “fake\nfake” | sudo passwd fakeuser
–>
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
But when I tried to apply in bash script..with variable..it’s not working..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
USER=”FAKEUSER”
FAKEPASSWORD=”TEST”
echo -e $FAKEPASSWORD\n$FAKEPASSWORD | sudo passwd USER
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
–>
Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match
passwd: Authentication token manipulation error
passwd: password unchanged
What’s wrong???
Simply your 2 `echo` commands used are different.
what do mean? I’m not quite understand..I tried to put the working echo -e “fake\nfake” | sudo passwd fakeuser in a bash file the same problem happen..
I tried this in the shell:
And,
In my case it doesn’t work..but I found the solution…remove the -e from the echo..so it work like charm..this is the code that will automatically register user from list
data.txt
~~~~~~~~~~~~~~~~~~~
USER1 PASSUSER1
USER2 PASSUSER2
~~~~~~~~~~~~~~~~~~
script
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NEW_USERS=”/home/admin/data.txt”
HOME_BASE=”/home/”
cat ${NEW_USERS} | \
while read USER PASSWORD GROUP
do
echo $USER $PASSWORD
useradd -m -p $PASSWORD $USER -g GROUP1 -d $HOME_BASE$USER
echo “$PASSWORD\n$PASSWORD” | sudo passwd $USER
done
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
run this script with “sudo”
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
have fun….thanks..
Looks nice.
But:
“read USER PASSWORD GROUP” in the script while the data.txt has “USER1 PASSUSER1″ in one line.
Does this cause problem?
that just example of data..it work like charm..I just register 1000 users to my server using this script..in 20 sec..nice..
Glad to know this help ;)
the corrent command, on modern linux is:
echo bbb |passwd –stdin aaa
Great tip. Thanks! I have also included this in the post.
Is it secure to transmit password in clear text over the network?
How to achieve the same by crypting the passwd?
The security replies on SSH for encrypting the communication.
The command history (e.g. ~/.bash_history) may leave a copy of the command on the servers (root account).
Works like a charm!
I am glad that it help you!
i tried it in ubuntu, something must have been wrong?
‘Authentication token manipulation error’
What’s the command that you are using? (use a fake password)
in Fedora u have to use strong passwords. below one worked for me
echo -e “\@lexm\@hone\n\@lexm\@hone” | passwd linususer
(newpassword and re type password is @lexm@hone)
It Works!!
Thanks Alex.
I agree that we should use a strong password (the one we can remember ;) ) especially for the system that can be reached by the Internet. We see lots tries to guess our root password on one node.
# echo -e “linuxpassword\nlinuxpassword” | passwd linuxuser
this command perfectly works for me…. thanks…
it is not working on rhel anyway. sould like good one.
Which version of RHEL are you using?
I test it on CentOS 5.3: ‘echo -e “linuxpassword\nlinuxpassword” | passwd linuxuser’ works fine for me.
The same command is not working on Solaris Sparc 10.
When i execute the command
echo -e “linuxpassword\nlinuxpassword” | passwd newuser
it again prompts me with New Password: Any ideas please?.
What is the output if you execute:
echo -e “linuxpassword\nlinuxpassword”