Using a script to automate a Raspbian installation

First Boot

Certain requirements need to be fulfilled to create a running system. One example would be retrofitting the system by installing software packages with the /usr/local/sbin/apiinst2 script. The script for the offline phase is fairly complicated but it is also static. Each user will have to create a phase two script according to their needs. Listing 7 shows a few possible variations..

Listing 7


01 #!/bin/bash
02 # Add/Remove packets
03 apt-get update
04 apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y install bc jed zip
05 apt-get -y --auto-remove remove scratch squeak-vm
06 # Authorise Users
07 addgroup --gid 4000 tux
08 echo -e "init#pw\ninit#pw" | adduser  --gecos "" --home /home/tux --uid 4000 --gid 4000 tux
09 chage -d 0 tux
10 # Dactivate
11 chmod -x "$0"

When the script starts, the package administration takes care of updates and uninstalling important or unnecessary packages (lines 5 to 7). The script then creates the user tux together with an initial password which the user has to enter at the first log in. The final action of the script is self-deactivation (line 20 removes the execution bit), because the script is intended to run only once.

In order to make sure that the script actually starts when the Pi boots, you will need to copy /path/to/template_folder/usr/local/sbin/apiinst2 in the template directory. Name this path exactly as it appears. This ensures that apiinst will cause the script to automatically start when the Pi is booted. Head over to the GitHub Project for a more advanced template than the example in Listing 7.

The offline script apiinst also offers a series of functions for more advanced users. One of these functions provides support for multiple, as opposed to single, template directories. Distributing files across several directories is useful for outfitting differing target systems, while keeping some of the files the same. Typically, these would be user specific files.

In addition, some scripts can be mounted in various places such as before and after copying the template directories. This is helpful if you need to perform a task such as simultaneously installing Raspbian on multiple Pis without changing anything other than the host name and certificate of each device.


Automatic installation of one or more Pis does not work without doing some preparatory work beforehand. You will need to weigh the advantages and disadvantages in terms of time saved for the next installations. The benefits here are reaped by the user who regularly sets up and installs Pis. Once prepared, it is easy to quickly boot a Raspberry Pi, leaving time for more important pursuits.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content