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'

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

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

  1. you have an error in the second example.
    orig:
    # echo -e “linuxpasswordnlinuxpassword” | passwd linuxuser
    new:
    # 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)

  2. 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 variable..it’s not working..

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    USER=”FAKEUSER”
    FAKEPASSWORD=”TEST”

    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:

          $ FAKEPASSWORD="TEST"
          $ echo -e $FAKEPASSWORDn$FAKEPASSWORD
          TESTnTEST
          $ echo -e "$FAKEPASSWORDn$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 "fakenfake" | sudo passwd eric
          
          1. 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 “$PASSWORDn$PASSWORD” | sudo passwd $USER
            done
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            run this script with “sudo”
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            have fun….thanks..

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

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

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

  5. # 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 Fclose.com Posts by Email if you have not.