26

In the current version of Raspian, I know it is possible to change the password of the current logged in user from the command line like so:

sudo passwd

which will then prompt the user to enter a new password twice. This will produce output like so:

Changing password for pi.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

I was wondering if there is a possible way to change a password programmatically, like from a shell script.

I'm trying to make a configuration script to deploy on my Raspberry Pis and I don't want to manually have to type in new passwords for them.

3
  • 2
    To change the password of the current user you don't need to prefix with sudo. If you use sudo then you can force a new password for any user without needing to know that user's current password. Apr 20, 2015 at 20:21
  • expect(1) could help as well.
    – SailorCire
    Apr 20, 2015 at 22:03
  • Be sure to consider the security implications when programmatically changing passwords.
    – vhs
    Dec 24, 2019 at 9:08

3 Answers 3

42

You're looking for the chpasswd command. You'd do something like this:

echo 'pi:newpassword' | chpasswd # change user pi password to newpassword

Note that it needs to be run as root, at least with the default PAM configuration. But presumably run as root isn't a problem for a system deployment script.

Also, you can do multiple users at once by feeding it multiple lines of input.

0
15

Another alternative is to use the yes command in your script.

yes newpassword | passwd youruser

This will send newpassword to the passwd command for youruser.

It should be mentioned that setting/modifying user passwords via scripts may present security risks and should be avoided whenever possible.

EDIT:

This answer requires root access. Apologies for not mentioning this previously. It is a method that I use when performing administration tasks which require root access.

8
  • Have you tried this? Apr 20, 2015 at 19:24
  • Yes. I have been using this method for years. I tested it before posting my answer and again just now. Apr 20, 2015 at 19:54
  • Even assuming that passwd read from stdin rather than a terminal (keyboard), how can this work when passwd prompts first for a user's old password and then prompts for the new? Apr 20, 2015 at 20:18
  • 2
    @roaima The passwd command from Linux's shadow utilities accepts redirected input (unlike e.g. OpenSSH ssh). Using yes is not a good idea though: it exposes the password to snoopers who look at the process list at the wrong time. Using echo would not have this defect because it's a shell builtin. Apr 20, 2015 at 22:45
  • 2
    This one was actually the only of all these answers that worked in our application. We are on an embedded device running an ancient version of Busybox and none of the other programs used exist here.
    – Dakkaron
    Feb 22, 2017 at 13:20
4

The @derobert's answer is correct but I had to execute the command as superuser like this:

echo 'pi:newpassword' | sudo chpasswd # change user pi password to newpassword

Note the sudo after |. sudo at the start won't help you because it applies only to echo

1
  • Thanks, this is the correct answer!
    – BHP
    Aug 17, 2021 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .