Changing Linux User’s Password in One Command Line

By Zhiqiang Ma On Feb 26, 2013

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'
By: Zhiqiang Ma Last updated: Feb 26, 2013 Views: 16,794
Tags: ,

About Zhiqiang Ma

Zhiqiang Ma is a PhD candidate at Dep. of CSE, HKUST. He is interested in system software for cloud computing, virtualization of large-scale distributed system, etc. Find Zhiqiang on Facebook, Twitter, LinkedIn and Google+.

23 Comments on Changing Linux User’s Password in One Command Line | Add Comment
  • 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:

          $ FAKEPASSWORD="TEST"
          $ echo -e $FAKEPASSWORD\n$FAKEPASSWORD
          TESTnTEST
          $ echo -e "$FAKEPASSWORD\n$FAKEPASSWORD"
          TEST
          TEST
          

          And,

          $ sh ./test.sh 
          [sudo] password for zma: 
          Changing password for user eric.
          New password: BAD PASSWORD: The password is shorter than 8 characters
          Retype new password: passwd: all authentication tokens updated successfully.
          
          $ cat ./test.sh 
          #!/bin/bash
          
          echo -e "fake\nfake" | sudo passwd eric
          
          • 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..

  • the corrent command, on modern linux is:
    echo bbb |passwd –stdin aaa

  • Is it secure to transmit password in clear text over the network?
    How to achieve the same by crypting the passwd?

  • Works like a charm!

  • i tried it in ubuntu, something must have been wrong?
    ‘Authentication token manipulation error’

  • 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.

  • sathya bhaarathy says

    # echo -e “linuxpassword\nlinuxpassword” | passwd linuxuser

    this command perfectly works for me…. thanks…

  • it is not working on rhel anyway. sould like good one.

Add your comments, share your thoughts

Be nice. Keep it clean. Stay on topic. No spam.