aristosv
Posts: 151
Joined: Mon Dec 08, 2014 7:47 pm

configure "pi" password before booting for the first time

Fri Dec 14, 2018 6:00 am

I am preparing a raspbian image for digital signage, and I am trying to automate as many things as possible before booting for the first time.

I know I can enable ssh server, configure WiFi and plenty of other options through cmdline.txt and config.txt, before even booting raspbian for the first time.

But can I set user "pi" password before booting for the first time? Is there an official way, or a workaround to do this?

Thanks

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: configure "pi" password before booting for the first time

Fri Dec 14, 2018 7:09 am

Some context would help.

What do you want to set it to? Is it just because if you don't change it, then a message pops up warning you to change it?

Are you using the Lite or the Desktop version of Raspbian?

Or something else?

Incidentally, if you just want to change it to a known string, then you should be able just manually edit the /etc/shadow file.

Finally, is the context of this problem that whatever methods you are using to access the image can only access the FAT partition? And, for that matter, whatever these methods are, do you apply them directly to the image file itself or are you talking about applying them to the burned SD card (i.e., after the image has been written to the SD card)?
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

aristosv
Posts: 151
Joined: Mon Dec 08, 2014 7:47 pm

Re: configure "pi" password before booting for the first time

Fri Dec 14, 2018 7:24 am

Hi,

- I want to change it because I don't want it to be the default one. The fact that it won't warn me any more is an added bonus.
- I am using raspbian stretch lite.
- Are you suggesting that after I burn the .img to the microSD, I modify /etc/shadow file? That could be a way I guess.
- I would love a way to modify the .img file, but modifying the microSD after I burn the .img would also do it for me.

I'll give a try to modifying /etc/shadow. I'm guessing I'll have to generate an encrypted password, and place the string in /etc/shadow.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: configure "pi" password before booting for the first time

Fri Dec 14, 2018 7:37 am

aristosv wrote:
Fri Dec 14, 2018 7:24 am
Hi,

- I want to change it because I don't want it to be the default one. The fact that it won't warn me any more is an added bonus.
OK - so you want to change it to just "something else". I.e, perhaps change it to "Thunder Bay", right? But the end user(s) will have to know what it is and will have to type it in?

In that case, yeah, the best thing to do is to change it manually on your test machine, see what gets put into /etc/shadow, and then cut and paste that string into the target /etc/shadow file.
- I am using raspbian stretch lite.
OK. So you are booting to a login prompt, at which point the end user will have to log in (log in the old-fashioned way...)
- Are you suggesting that after I burn the .img to the microSD, I modify /etc/shadow file? That could be a way I guess.
- I would love a way to modify the .img file, but modifying the microSD after I burn the .img would also do it for me.
It's not that hard. You just have to have another Linux machine as your test/work machine, then mount the partitions in the image file using the "loop" device. There is also "kpartx" which makes this process a little easier.
I'll give a try to modifying /etc/shadow. I'm guessing I'll have to generate an encrypted password, and place the string in /etc/shadow.
Yep.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
HawaiianPi
Posts: 4531
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: configure "pi" password before booting for the first time

Fri Dec 14, 2018 11:33 pm

As long as you're just editing files, you can unzip the .img file, mount the .img file partitions, modify any files you need, then unmount and re-zip it. That's how I make my Pi-Zero USB Gadget mode images. There are 2 partitions in a standard Raspbian image so you'll need to supply an offset to tell the mount command where the partitions start.

Check the existing partitions in the image file with:

Code: Select all

fdisk -l 2018-11-13-raspbian-stretch-lite.img
Which should produce an output similar to this:

Code: Select all

Disk 2018-11-13-raspbian-stretch-lite.img: 1.8 GiB, 1866465280 bytes, 3645440 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7ee80803

Device                                Boot Start     End Sectors  Size Id Type
2018-11-13-raspbian-stretch-lite.img1       8192   98045   89854 43.9M  c W95 FAT32 (LBA)
2018-11-13-raspbian-stretch-lite.img2      98304 3645439 3547136  1.7G 83 Linux
Multiply the Start offset by the sector size (usually 512 for flash drives).
8192 x 512 = 4194304
98304 x 512 = 50331648

If you just want to mount the "boot" partition to edit cmdline.txt or config.txt (or add an ssh file).

Code: Select all

sudo mount -t vfat -o loop,offset=4194304 /path-to-image/2018-11-13-raspbian-stretch-lite.img /mnt

Or, if you want to mount the rootfs partition:

Code: Select all

sudo mount -t ext4 -o loop,offset=50331648 /path-to-image/2018-11-13-raspbian-stretch-lite.img /mnt

Then, if you want to mount the /boot partition into the mounted rootfs partition, you need to specify a size limit:

Code: Select all

sudo mount -t vfat -o loop,offset=4194304,sizelimit=46005248 /path-to-image/2018-11-13-raspbian-stretch-lite.img /mnt/boot
The "sizelimit" parameter is the partition size (Sectors x 512) and is included to prevent overlap.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

Return to “Raspbian”