Raspberry Pi OS for a Server

Raspbery Pi Model 2 held in an adult human hand


For this article I have used a Model B+ and a Pi 2 but any model should work.

Not using Wireless

While we could use wireless, for basic infrastructure I prefer the stability and reliability of a wired connection.

Why? Small Footprint Ideal for Low Demands

I use this as a bare metal provisioning and configuration server (using Ansible) that has no dependencies on other hosts, for the case where I need to bring up bare metal and virtual machine infrastructure (or recover from failure). This is for tens of hosts not hundreds.

Since this is a low demand environment the small size, low electricity use, fan-less operation, and low price of the Pi make it ideal.

In the past, I have used a similar configuration to serve network booting and installs of CentOS, Debian, and Fedora and related projects (like Atomic), as well as allowing network boots of System Rescue CD. In addition, the same server can easily act as the repository server for package updates on an isolated network.

A further use of this server is to act as a Debian apt cache for isolated networks.

With these capabilities all other servers on the network can be installed, provisioned, and configured from this server.

A Note on Using Swap and /var on an External Drive

This article discusses techniques for using swap and /var on an external drive as there are time this can be a useful technique. In particular if one has an external spinning disk or other external media which is faster and has more write cycles than an SD card, then it can make to move both swap and the /var directory (and maybe /tmp) to the external drive(s) because it will increase the speed and robustness of the system as swap and /var, and often /tmp are written frequently. In addition, the faster swap is, the better, although you still want to avoid situations where you exceed available memory and end up where swap is thrashing and bringing the system to a crawl.


Obtain Base Image for RPi SD Card

The Easy Way

  1. Follow the instructions on the Raspberry Pi Foundation Download Page to get the Raspberry Pi Imager for your operating system.
  2. Launching the imager will give you the option to select an image to download. You want to select ‘Raspberry Pi OS (Other)’ and the ‘Raspberry Pi OS (32-bit) Lite’.
  3. You then select your SD card
  4. Select write, and the imager will write and verify the image.

In Case of Slow Download Speeds or You Want a Copy of the Image

A Raspberry Pi OS (32-bit) Lite image provides a solid base for this configuration.

If you find download speeds are slow you may wish to try the Raspberry Pi OS (32-bit) Lite Torrent.

  1. You still want the ‘Raspberry Pi Imager’.
  2. Launch the imager and select ‘Use custom’ (which is the last item on the list of choices when you select ‘Choose OS’)
  3. Select the ZIP file you downloaded above (either via HTTPS or Torrent).
  4. You then select your SD card
  5. Select write, and the imager will write and verify the image.

Boot the Pi and Perform Initial Configuration

Boot the Pi for the First Time (with this image)

  1. Boot the Pi.
  2. The root partition will be resized and the Pi will reboot.
  3. Once the Pi boots, you can log in.

Initial Configuration

  1. Login (username: pi, password: raspberry)
  2. Use the Raspberry Pi configuration tool sudo raspi-config
    1. Select 4 Localisation Options
      1. Select I1 Change Locale
      2. Select your locale and desired encoding (usually the encoding should be UTF-8).
    2. Select 4 Localisation Options again
      1. Select I3 Change Keyboard Layout
      2. Follow the prompts to set your keyboard configuration.
    3. Select 1 Change User Password – it is important you set locale and keyboard layout before changing password as those options affect what characters are actually in the password. The Pi defaults to a UK UTF-8 locale and UK keyboard layout because it is created by a UK non-profit.
      1. Enter a new password the the pi user when prompted.
    4. Select 2 Network Options
      1. Select N1 Hostname
      2. Enter a new hostname for the Pi when prompted
    5. If you want to use wireless select 2 Network Options again, and then N2 Wireless LAN and follow the prompts.
    6. Select 4 Localisation Options again
      1. Select I2 Change Time Zone
      2. Select your time zone (note that Canadian cities are under ‘America’)
    7. If using wireless, select 4 Localisation Options again
      1. Follow the prompts to set your country code to ensure you remain in compliance with your countries wireless regulations.
    8. Select 5 Interfacing Options
      1. Select SSH
      2. Enable the SSH server
    9. Select Finish
    10. Choose Yes when prompted ‘Would you like to reboot now?’
    11. Your Pi will reboot.
  1. If you want to use public key authentication for SSH (recommended), then you need to:
    1. Locally create a SSH keypair (or keypairs) if you don’t already have one. See one of the many guides on the internet if you need more information, for example:
    2. scp yourkey.pub pi@<hostname>: (substituting as appropriate)
    3. ssh pi@<hostname>
    4. mkdir -p .ssh
    5. chmod 700 .
    6. chmod 700 .ssh
    7. mv yourkey.pub .ssh/authorized_keys
    8. chmod 600 .ssh/authorized_keys
  2. To disable password authentication for SSH (and thus require public key authentication):
    1. sudoedit /etc/ssh/sshd_config
    2. Ensure one line has PasswordAuthentication no and that any other PasswordAuthentication lines are commented out.
    3. Ensure there are no lines with PubkeyAuthentication no.
    4. Restart ssh: systemctl restart sshd

Next Steps if Using Swap on an External Drive

  1. First you need to partition your drive so that you have a partition for swap.
  2. Next execute sudo mkswap /dev/your-drive-swap-partition-x (e.g. sudo mkswap /dev/sdd2 if your drive is /dev/sdd and you’ve made the second partition the swap partition).
  3. Use the blkid command to discover the UUID of the swap partition.
  4. Execute sudoedit /etc/fstab
    1. Add a line like UUID=uuid-of-your-swap-partition-from-blkid none swap sw 0 0
    2. Save and exit
    3. Execute sudo swapon -a
    4. Execute sudo dmesg and verify that there haven’t been any errors using the swap partition.
  5. sudo systemctl stop dphys-swapfile
  6. sudo systemctl disable dphys-swapfile
  7. sudo rm -f /var/swap
  8. sudo apt remove --purge dphys-swapfile

Next Steps if Using /var on an External Drive

  1. This is much easier with rsync, so execute sudo apt install rsync.
  2. You need to be logged into the Pi on a physical console rather than SSH. This because using SSH uses /var and we’re wanting to move /var.
  3. Next, you need to partition and format your drive so that you have a partition for /var
  4. Before you can add the new partition to fstab you need to move /var.
  5. Execute sudo mount /dev/your-new-var-partition /mnt
  6. Execute the following commands:
    sudo systemctl stop syslog.socket rsyslog sudo systemctl stop systemd-journal*.socket systemd-journal*.service sudo rsync -arHAXx --info=progress2 /var/ /mnt/ sudo umount /mnt sudo mv /var /var.old sudo mount /dev/your-new-var-partition /var sudo blkid sudoedit /etc/fstab 
  7. Add a line such as UUID=uuid-of-your-var-partition /var ext4 defaults,relatime 1 1

  8. Save and exit
  9. Reboot
  10. If you’ve done things right, df -h should show your new partition mounted on /var.
  11. Execute sudo rm -rf /var.old

Install & Configure the Desired Services

This is basically a Debian system so the many guides for installing and configuring services on Debian (and in many cases Ubuntu) will be applicable.


By Daniel F. Dickinson

Daniel Dickinson has been using and exploring computer and electronic technologies for over three decades. He is proficient with various flavours of Linux (including varieties of desktop, server, and embedded systems) as well as modern Windows. Daniel still enjoys software and firmware development, but prefers integration and extending existing open source solutions to quickly achieve desired goals. Fo more details see https://www.wildtechgarden.com/about/about-daniel-f

Leave a comment

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