We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

KjeldFlarup
Posts: 10
Joined: Thu Jan 12, 2017 9:58 pm

Change password on SD card

Tue Mar 30, 2021 7:10 am

I have a script to create a RPi SD card from my Intel Linux.
After doing DD, I mound the SD card, and modifies some files.

I would like to change the default password from "raspberry" to something else.

However when I try to modify the passwd file I get an error

Code: Select all

# echo -e "linuxpassword\nlinuxpassword" | passwd -R /media/kfa/rootfs pi
passwd: Cannot determine your user name.
What other tricks exists to change this password?


Kjeld
Kjeld Flarup
http://jegkalinux.dk

GlowInTheDark
Posts: 2331
Joined: Sat Nov 09, 2019 12:14 pm

Re: Change password on SD card

Tue Mar 30, 2021 8:07 am

The most straightforward way is to figure out what the second field of the /etc/shadow file should be (by manually changing the password to what you want it to be, then checking the file) and then you can have your script do some sort of automated edit of the mounted /etc/shadow, setting the second field to what you want it to be.

Details left as an exercise for the reader.
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

markkuk
Posts: 326
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: Change password on SD card

Tue Mar 30, 2021 8:44 am

Use the chpasswd command:

Code: Select all

# chpasswd pi:linuxpassword -R /media/kfa/rootfs

Heater
Posts: 19722
Joined: Tue Jul 17, 2012 3:02 pm

Re: Change password on SD card

Tue Mar 30, 2021 12:18 pm

As you are doing this from a script how about generating the password hash manually first. For example:

Code: Select all

$ openssl passwd -6 -salt xyz  verysecretpassword
$6$xyz$uOfY4/o3BE1BZ2/cNLbARGNORxJauX6Y7D679TDiUpfcgeDpBAHzAwq4ZQaMlb5TgXzH7G/bU9.sKKftzR9aI0
Then have your script replace the line in /etc/shadow with one that contains that hash string.

Hint, use 'sed' to do that replacement in your script.

The advantage of this is that you won't have a password in plain text embedded in your script for anyone to read. Which is a security hazard. Do remember that password like any other.

See this page for other methods of generating a password hash:
https://unix.stackexchange.com/question ... etc-shadow
Slava Ukrayini.

GlowInTheDark
Posts: 2331
Joined: Sat Nov 09, 2019 12:14 pm

Re: Change password on SD card

Tue Mar 30, 2021 2:12 pm

Then have your script replace the line in /etc/shadow with one that contains that hash string.
Exactly as I said.

Note, BTW, that methods that use a program (e.g., passwd or chpasswd) will need to do some sort of "chroot"ing so they (the program) works on the passwd/shadow files on the mounted drive (SD card) rather than on the system on which you are actually running (which, in OP's case is an x86 Linux box). Although this can, of course, be done correctly and safely by a knowledgeable user, it seems dangerous to recommend to a newcomer. For this reason, it is actually safer to directly edit the file (/path/to/mounted/etc/shadow).
Poster of inconvenient truths.

Back from a short, unplanned vacation. Did you miss me?

Return to “General discussion”