Installing Debian on a headless Raspberry Pi

EDIT: if you are planning to install Debian on a Raspberry 4 on an (USB) SSD by following this page, then make sure the Pi 4 bootloader is up to date first, because older bootloader versions won’t boot from USB.

The simplest way to achieve this is by flashing the Raspberry Pi OS on an SD card, booting it on the RPi 4 (with the USB SSD unconnected), and running a full upgrade (which will update the bootloader).

Then you can turn the RPi 4 off, put away the SD (you won’t need it any more), and reconnect the SSD with the Debian image written on it as described below.

Yes, I speak from direct experience. 🙂

EDIT #2: the buster-backports repository is pre-enabled.

EDIT #3: correction: Raspberry Pi OS 64-bit version does indeed use Debian repositories. Thanks to @gegeweb@stoneartprod.xyz for pointing this out!

I use quite a few Raspberry Pi as headless servers, with no display, no keyboard, just the wired interface.

Also, I like my servers to run as close to a standard Debian system as they can.

But Raspberry Pi OS (né Raspbian) is not close enough to a Debian system for me, if only because it does not have any Debian repositories as its package sources it only has Debian repositories in its 64-bit, not 32-bit,versions. Therefore:

  • On 32-bit Raspberry Pi OS, I cannot apply Debian security updates;
  • On 32-bit Raspberry Pi OS, I cannot install some packages which Debian repositories provide but Raspberry Pi repositories do not;
  • The Raspberry Pi OS system, 32 or 64-bit, is far from minimal.

Enter Debian for the Raspberry Pi : https://wiki.debian.org/RaspberryPi

The Good

The Debian image for the Raspberry Pi is just what I want:

  • It boots off USB mass storage natively (whereas Raspberry Pi OS, at least until 2020-05-27, requires manually updating the boot files)
  • It is a truly minimal install:
    • absolutely no frills, not even console-tools or bash-completion!
    • no non-system user defined at all — only root can log in
  • It has an SSH server already enabled (but see below)

The Bad

There’s a few gotchas about these images though:

  • The machine name is set to a weird (but predictable) name of the form rpiX-YYYYMMDD where X is the model number and YYYYMMDD is the date of the image. For instance, my Raspberry 3b+ running the image from 20200809 has the name rpi3-20200809.
  • The root user has no password.

Both issues can be mitigated by patching the image beforehand, more precisely, by editing file sysconf.txt in the boot partition…

… but I prefer doing it the Debian enthusiast way: by modifying the root partition:

  • Set up public key SSH root access:
    • Create directory /root/.ssh
    • Set the mode of /root/.ssh to 700
    • Create file /root/.ssh/authorized_keys
    • Copy the relevant public key(s) there
    • Set the mode of /root/.ssh/authorized_keys to 600

Once this change is done, I make a copy of the patched image, and that is the “master” image I keep around to quickly create new server images by applying the following:

  • Edit /etc/hostname and set the hostname there (there is no other file to change, contrary to Raspberry Pi OS where /etc/hosts would have to be edited too)

Now I have a server-specific image which can boot off an SD card or USB mass storage.

The Ugly

There is one thing to complain about these images. At first I tried the allegedly stable 20200501_raspi_3.img image… which failed bringing up the eth0 wired interface.

Then I tried the daily 20200809_raspi_3.img image, and it worked out of the box.

Go figure. 🙂

Of course, things are bound to improve over time. Just keep tabs on the last image which worked for you, and check new images periodically.

For a few $#s more

Once the system has booted, wait for it to respond to ping, then SSH into it as root, using one of the SSH keys you have set up previously.

Now run the Debian updates:

# apt update
# apt upgrade

(If the upgrades included a new kernel version, reboot.)

Your system is now good to go!

A fistful of $#s

I use bash completion a lot, so I usually install it right away:

# apt install bash-completion

Lastly, I like to enable history search so that I can use PgUp and PgDn to look up the history for commands which start similar to what I have types so far (e.g. Type m o u n t PgUp to find the latest mount command I entered). For this I uncomment the history-search-backward and history-search-forward lines in /etc/inputrc.

Note

This change will apply to new shells only.
To apply it to the current shell, hit Ctrl-X Ctrl-R.

Note that there is no locale installed, so if you SSH from a machine where the locale is specified (I do, with a locale set to fr_FR.UTF-8), then all messages will be in English (due to fallback C locale) and you will sometimes get complaints like:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LANG = "fr_FR.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

The solution consists in installing the locales package, enabling your locale(s) in /etc/locale.gen, and running locale-gen.

Then, if you want the buster-backports repository enabled, just un-comment the corresponding line in /etc/apt/sources.list! (or, if you prefer, you can enable it in your master image). The buster-backports repository is already enabled, because apparently you need it for things to work on the Raspberry Pi 4.

For people who want the quick recap, here is a list of the commands I run on a freshly installed Debian Raspberry Pi server to get all of the above in place

apt update
apt upgrade
apt install bash-completion
sed -i -E 's/^#[ \t]*(.*history-search-(back|for)ward)$/\1/' /etc/inputrc
Ctrl-X Ctrl-R
apt install locales
sed -i -E 's/^#[ \t]*(.*fr_FR.UTF-8.*)$/\1/' /etc/locale.gen 
locale-gen

2 comments on Installing Debian on a headless Raspberry Pi

  1. Nope, still doesn’t let me log in remotely at all. Still wants that non-existent root password. And as they no longer have that 20200809_raspi_3.img on the download page, I can’t try that one either. And I don’t have a monitor the rPi4 likes, so the ONLY option is to get it to work headless.

    Could probably try the Ubuntu image, but I did want to have something more pure Debian. (I’d use Fedora if they had the rPi4 support at a decent state), and the NextcloudPi image doesn’t appear to be bootable.

    1. Hey jelabarre59,

      Regarding the fact 20200908_raspi3.img not being available any more — just grab the latest stable one, things should be the same.

      Regarding not letting you log in: how exactly did you add your SSH key to the image’s root partition before letting the Pi boot on it? I use kpartx and mount — and lately I’ve moved to using guestfish.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.