How to set a new password from bash?

10,410

Busybox has chpasswd(8) which is a utility best used to create/update a lot of users very quickly and with one command. It accepts data from STDIN in username:password form. That means that you can do something like this:

$ cat pwdfile | chpasswd

or

$ < pwdfile chpasswd

Note that pwdfile must have username:new_password syntax.

Then again, you could always edit /etc/shadow yourself -- but please don't.

Share:
10,410

Related videos on Youtube

michelemarcon
Author by

michelemarcon

Hello, I'm a Java software engineer. I also have some Android and Linux experience.

Updated on September 18, 2022

Comments

  • michelemarcon
    michelemarcon over 1 year

    I'm using busybox with a limited passwd (I don't have --stdin option) and without chpasswd and I need to change the password of an user from bash. Here is my best result:

    echo newpassword > pwdfile
    echo newpassword > pwdfile
    cat pwdfile | passwd myuser
    Changing password for myuser
    Enter the new password (minimum of 5, maximum of 8 characters)
    Please use a combination of upper and lower case letters and numbers.
    Enter new password:
    Bad password: too simple.
    
    Warning: weak password (continuing).
    Re-enter new password:
    passwd: The password for myuser is unchanged.
    
  • clerksx
    clerksx over 12 years
    There's no need to cat to a pipe, just use IO redirection; < pwdfile chpasswd.
  • michelemarcon
    michelemarcon over 12 years
    My busybox does not have chpasswd.
  • michelemarcon
    michelemarcon over 12 years
    passwd: invalid option -- - BusyBox v1.00 (2011.04.28-16:58+0000) multi-call binary Usage: passwd [OPTION] [name] Change a user password. If no name is specified, changes the password for the current user. Options: -a Define which algorithm shall be used for the password (Choices: des, md5) -d Delete the password for the specified user account -l Locks (disables) the specified user account -u Unlocks (re-enables) the specified user account
  • n0pe
    n0pe over 12 years
    Is the user you're changing the password for in both /etc/passwd and /etc/shadow?
  • michelemarcon
    michelemarcon over 12 years
    Yes, it is present on both files. The user can log in and out normally.
  • n0pe
    n0pe over 12 years
    Can you try with a non-weak password? Maybe that's the issue.