Build and test a pocket Ubuntu server in your lunch break
About 30 years ago I told my teacher that we would soon be fitting powerful computers into a matchbox. I kindly drew her a picture, showing wires for the TV, power and keyboard — and she brutally told me I was an idiot and gave me lines. Today I’m going to have the last laugh by telling you how to build a fag-packet computer running full-fat Linux.
My choice of computer today is the Raspberry Pi — I have experimented with these tiny single-board machines on an off since their first release, but this is my first time hands-on with the latest revision which has been out for almost two years. Sporting 4k graphics and — in this case — 8GB of RAM, it may not be the fastest machine on the planet but it’s on a par with a general purpose T2 micro instance from AWS.
MacOS is not Linux — and there are many times I find myself needing the convenience of a local Ubuntu instance but don’t want to tie up my laptop with a VM. Small enough to carry in my pocket, cheap — this whole setup cost around £100 — and with the right case it needs no fan, even under load. A simple micro SD card not only provides cheap persistent storage, it takes seconds to swap it for another — perhaps with a different OS ready to do something completely different.
My parts list
- Raspberry Pi 4 Model B with 8GB RAM. The extra memory gives plenty of headroom — especially as I’m planning to use it with Docker.
- Raspberry Pi official USB-C power supply. Others are available but the price difference is literally just pence.
- FLIRC aluminium case. As well as being a work of art, it’s practical too. The integrated heatsink keeps the CPU cool in complete silence.
- WD Purple micro SD card. Designed for the security camera market, this is a high-endurance card that supports repeated writes.
An HDMI cable was also on my shopping list but isn’t necessary in this is going to be a headless install. I shall be keeping it in case I want to re-purpose this into a media player or some other cool project…
Step-by-step
Assembly takes no time at all. The case comes with a small thermal pad which aligns neatly with the integrated case heatsink.
The first step is to remove the sticky film from one side of the thermal pad and attach it to the SOC housing the CPU on the board.
Having carefully removed the board from its box, simply align with the case’s mounting holes.
All that remains is to fit the base over the top of this and secure with the four screws provided.
Installing the software
Creating a disk for your new server used to require some skill — but these days there’s an app for that — in the form of the Raspberry Pi Imager.
This takes out all the legwork and means any beginner like me can be up and running in minutes.
My operating system of choice (and there are many choices) is Ubuntu Server 20.04 LTS.
Don’t be fooled — this is a fully certified 64-bit operating system with extended security support reaching beyond 2030.
Time to go and make a cup of tea! This won’t take too long over a fast connection as it’s only about 700MB for the download. I’m just glad we recently had a fibre upgrade…
Initial configuration
If you want to boot over WiFi, then don’t remove the SD card just yet. There is a small YAML file called network-config on the card which contains some example settings, commented out, for you to adjust to your needs.
And we’re live! There are handy LEDs — red for power and green for disk activity.
Wait for a couple of minutes and you should see a new device on your network, you can use arp -na to spot the new device on your network or check your router / firewall logs.
Logging in
Log into the ubuntu user account using ssh — the default password is ubuntu, which you will be required to change on first login.
ssh ubuntu@<ip_address>
Et voila!
If you add your public key to .ssh/authorised_keys and you’ll be able to log in to your Pi without a password. If you don’t have a public key already, you can generate one on a Mac with the following command — note that I’m using Ed25519 rather than RSA here.
ssh-keygen -a 100 -t ed25519 -f ~/.ssh/id_ed25519 -C "fred@bloggs.com" && cat ~/.ssh/id_ed25519.pub
To avoid re-typing your passphrase each time you use ssh — which kind-of defeats the point of password-less access — you can add to your keychain:
ssh-add -K ~/.ssh/id_ed25519
The next step is to update the operating system. Use:
sudo apt updatesudo apt upgrade
…to bring the system up to date.
Increasing the life of the SD card
One of the advantages of all that available memory is that we can mount parts of the filesystem into RAM using tmpfs. This reduces wear on the SD card which — like all forms of SSD — has only a limited write capacity and will eventually wear itself out. Add the following lines to your /etc/fstab file and you’ll mount /tmp and /var/tmp into memory rather than on the disk.
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=100m 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=30m 0 0
You can use the df command to see the new mount points following a reboot. You could also potentially extend this to other locations e.g. /var/log if you don’t require persistence there.
ubuntu@ubuntu:~$ df -h /tmpFilesystem Size Used Avail Use% Mounted ontmpfs 100M 0 100M 0% /tmp
How good is the case?
For me, the major selling point of the case was being able to run the CPU at full tilt without needing a noisy or unreliable fan. The CPU itself starts to throttle at 80 Celsius — and I wasn’t sure if the case (or my installation) was up to the mark. So I downloaded a handy stress-tester:
sudo apt install stress-ng
The current CPU temperature of a Raspberry Pi 4 can be found in the file /sys/class/thermal/thermal_zone0/temp — you just take the number and divide by 1,000. I wrote a very basic script to capture this over a half-hour period:
#!/bin/bashfor i in {1..1800} do
cat /sys/class/thermal/thermal_zone0/temp >> cputemp.csv
sleep 1done
From the comfort of your Mac, you can use scp to securely copy the file created from your Pi ready to load into Excel:
scp ubuntu@<ip_address>:/home/ubuntu/cputemp.csv .
And here you go — even after 26 minutes of running the stress test, the CPU remained almost 20 degrees under the throttling limit — meaning the case (and the way I had assembled it) was working well.
I hope you found this an interesting experiment — I will be putting my new ‘machine’ through its paces over the coming weeks with Docker, and will keep you posted how I get on.