Change user password with one Bash command line

14,542

Solution 1

echo 'user:passwd' | sudo chpasswd

This helped me.

Solution 2

Please see the man chpasswd(8): man 8 chpasswd

The chpasswd command reads a list of user name and password pairs from
standard input and uses this information to update a group of existing
users. Each line is of the format:

user_name:password

Based on the man you can use: echo 'user:passwd' | sudo chpasswd

Solution 3

Kindly try below one one-liner command for user & password creation

useradd himanshi ; echo -e "1234\n1234" | passwd himanshi
Share:
14,542
Lenton
Author by

Lenton

Updated on June 13, 2022

Comments

  • Lenton
    Lenton almost 2 years

    I am making a program with Python 2.7, I use shell commands to create new users on Ubuntu and I want to setup the password for the new user. I use command prompt (Bash) commands to create users. Is there a command line that would change user's password just with 1 shell command? I tried:

    echo "newpass" | passwd --stdin user1 
    

    but it appears the current Ubuntu 16.04 version I have doesn't have the --stdin parameter anymore. And I need that after the command is executed, the password would be set, I would not need to retype to confirm password etc (like using terminal).

  • Andriy Berestovskyy
    Andriy Berestovskyy almost 7 years
    Well, that is basically what I suggested? And it should be vice versa, i.e.: echo 'user:passwd' | sudo chpasswd
  • Raja Anbazhagan
    Raja Anbazhagan about 5 years
    Thanks for digging out the MAN page. I never knew this was possible.
  • T04435
    T04435 over 4 years
    I'll suggest changing the correct answer to @AndriyBerestovskyy 's one as he replied first and this is just a sample of his answer. In the best case, you could remove your answer and add it as an edit on his answer.
  • Robino
    Robino almost 4 years
    This is a (poor) copy of the earlier answer by @AndriyBerestovskyy!
  • linux.cnf
    linux.cnf over 2 years
    above trick is also useful when need to create user with password in Ansible. command module will work here.