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)


# 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: to 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'

About Eric Zhiqiang Ma

Eric 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 systems and etc. Also find Eric on Facebook, Twitter, LinkedIn and Google+. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties. More Posts - Website

27 thoughts on “Changing Linux User’s Password in One Command Line”

  1. Hi sir

    Can you help me with outputing the value of a variable in the following command.
    # for ((i=1;i&lt;=100;i++)); do ssh 10.1.0.$i ‘echo -e “$1\n$1″ | passwd linuxuser’; done;

    Currently it prints $1, I want it to take the value of the variable $1

  2. you have an error in the second example.
    # echo -e “linuxpasswordnlinuxpassword” | passwd linuxuser
    # echo -e “linuxpasswordnlinuxpassword” | passwd linuxuser

    backslash was missing, it have to be added before ‘n’.
    cos n means new line (for retyping password line, it has to be breaked to a second line)

  3. It’s working if this apply directly through command line..

    UBUNTU 12.10

    echo -e “fakenfake” | 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’s not working..


    echo -e $FAKEPASSWORDn$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???

      1. what do mean? I’m not quite understand..I tried to put the working echo -e “fakenfake” | sudo passwd fakeuser in a bash file the same problem happen..

        1. I tried this in the shell:

          $ echo -e "$FAKEPASSWORDn$FAKEPASSWORD"


          $ 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 ./ 
          echo -e "fakenfake" | sudo passwd eric
          1. In my case it doesn’t work..but I found the solution…remove the -e from the it work like charm..this is the code that will automatically register user from list
            USER1 PASSUSER1
            USER2 PASSUSER2


            cat ${NEW_USERS} |
            while read USER PASSWORD GROUP
            echo $USER $PASSWORD
            useradd -m -p $PASSWORD $USER -g GROUP1 -d $HOME_BASE$USER
            echo “$PASSWORDn$PASSWORD” | sudo passwd $USER
            run this script with “sudo”

            have fun….thanks..

              1. that just example of work like charm..I just register 1000 users to my server using this 20 sec..nice..

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

    1. 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).

  5. in Fedora u have to use strong passwords. below one worked for me

    echo -e “@lexm@honen@lexm@hone” | passwd linususer
    (newpassword and re type password is @lexm@hone)

    It Works!!

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

  6. # echo -e “linuxpasswordnlinuxpassword” | passwd linuxuser

    this command perfectly works for me…. thanks…

    1. Which version of RHEL are you using?

      I test it on CentOS 5.3: ‘echo -e “linuxpasswordnlinuxpassword” | passwd linuxuser’ works fine for me.

      1. The same command is not working on Solaris Sparc 10.
        When i execute the command
        echo -e “linuxpasswordnlinuxpassword” | passwd newuser
        it again prompts me with New Password: Any ideas please?.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Click to subscribe to Posts by Email if you have not.