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'

If you want to update your own password as a normal user, you may use

$ echo -e "your_current_pass\nlinuxpassword\nlinuxpassword" | passwd

Eric Z Ma

Eric Ma is interested in operating systems and distributed computing and processing systems. 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.


  1. I got a problem,

    when I was trying to changing the password of aaa,

    sudo echo -e "abcd1234\nabcd1234" | passwd aaa

    however, error pumps up,

    passwd: You may not view or modify password information for aaa

    I think there are some security setting with the system, which did not allow me to do so,

    how to solve this out.

    thanks a lot.

  2. Neither solution works for /bin/sh
    Enter new UNIX password: Retype new UNIX password: passwd: Authentication token manipulation error
    passwd: password unchanged

    They work only for /bin/bash
    Any solution to /bin/sh?

    Thanks in advance

    1. Did it work for you under /bin/bash? That’s interesting.

      What’s your exact command and the output?

      And which OS are you working on? I believe my OS (Fedora 22) has quite different messages printed out from yours:

      $ passwd
      Changing password for user zma.
      Changing password for zma.
      (current) UNIX password:

      Showing us `passwd –help` will also be helpful to answer your question.

  3. What if I have normal user access on each remote server and I want to change all remote servers
    password which contains same username. I think it will ask for password if haven’t configure password less auth.

    In this case what command helps.

    Thanks in advance

  4. its giving token manipulation error.
    for ((i=1;i<=100;i++)); do ssh 10.1.0.$i 'echo -e "linuxpassword\nlinuxpassword" | passwd linuxuser'; done;

    firstly its giving error that only root user can specify the username as I was logging with a standard user.
    After this I tried with this one
    for ((i=1;i<=100;i++)); do ssh 10.1.0.$i 'echo -e "linuxpassword\nlinuxpassword" | passwd '; done;

    then its token manipulation error.

    thanks a lot

    1. When you update your own password, `passwd` command requires 3 input, one of your current password and twice of your new password. So the command may be:

      for ((i=1;i< =100;i++)); do ssh 10.1.0.$i 'echo -e "your_current_pass\nlinuxpassword\nlinuxpassword" | passwd '; done;
    1. In general, you have at least two methods to do actions to each item in a line in a text file txt:

      for i in `cat txt`; do echo $i; done

      Here, there should be no space in each item.


      while read i; do echo $i; done <txt

      You can replace the `echo $i` with the actions you would like to do (calling passwd in this example).

Leave a Reply

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