I'd like to know, whether there is an option to execute a script on the first boot of Raspberry. There is a catch though.

I'd like to be able to put the script in the boot folder. Since it is available on the SD card from Windows, I could prepare the script there before the first boot.

The reason for my specific requirements is that I'm using 5" Waveshare display in custom enclosure and I have to disconnect HDMI connector, connect a regular HDMI display, boot, download and install drivers and then replace the cable with connector again after reboot.

I've been thinking, if there is a way I could craft a script, which would configure the WiFi connection, clone repo with driver scripts and run one of them (it finishes with sudo reboot). If I needed to prepare a new SD card for my Raspberry setup, I'd simply put that script in boot folder, power Raspberry up and just wait until it reboots after installing drivers.

Is it possible?

  • Has it to be the very first boot or could it be the second after raspbian has resized the filesystem? Because on first boot there is already a script running, doing some things and then reboot the pi. I think you could do the same, but it would be tricky on the very first boot...
    – jake
    Commented Apr 14, 2019 at 21:26
  • @jake May be either, actually the second - most likely - will be even more convenient.
    – Spook
    Commented Apr 15, 2019 at 6:27
  • You want to change Wifi, repos, add drivers, etc. You should learn how to use Linux to modify the sd image by mounting both filesystems. You might still use a script in /boot. This approach has the advantage of running on the RPi and using commands on the RPi. Mounting and modifying the sd image is more powerful, but less convenient. It typically can't execute software from the SD image, but nothing in the image is busy or in use. Commented Apr 18, 2019 at 21:10

1 Answer 1


If you have a virgin Raspbian image that was never booted before you will find a script that is executed on first boot up. Look at a fresh flashed CD Card with mounted boot partition for example at /mnt/p1:

laptop ~$ sudo cat /mnt/p1/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=c1dc39e5-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh

As you see at the end there is a script /usr/lib/raspi-config/init_resize.sh called. It mainly does what it's name says but also some other things. It has more than one exit point, some with error condition but with some know how of scripting it should be able to insert a call to your custom script.

With some care it should be possible to run your script at very first boot as a hook to init_resize.sh.

  • 1
    There's also the second stage that runs the configuration wizard piwiz after the resize, although that's less "hackable" without being able to mount the second partition. See raspberrypi.stackexchange.com/a/94801/93934. Commented Apr 15, 2019 at 9:07
  • So my goal would be to replace this line with call to my own script placed in boot, which does some hacking and then calls init_resize.sh afterwards? Then my other script would run on the second boot. Or: take init_resize.sh script, modify it, put in /boot folder and replace call to /usr/lib/raspi-config/init_resize.sh with like /boot/my_init_resize.sh, which does some magic after resizing?
    – Spook
    Commented Apr 15, 2019 at 12:02
  • @Spook I wouldn't touch the entry in cmdline.txt because something is looking at it and removes it (maybe the script itself?). If it doesn't match, it remains and the workflow may be broken. Or you manage your own init script in general.
    – Ingo
    Commented Apr 15, 2019 at 15:51
  • 2
    @Ingo This is the line, that removes the entry: sed -i 's| init=/usr/lib/raspi-config/init_resize.sh||' /boot/cmdline.txt. It's part of init_resize.sh.
    – jake
    Commented Apr 15, 2019 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.