I have been setting up lots of Raspberry Pis, mostly for use with my home automation projects. It is time to document how I setup and get the most out of these fantastic little machines.
Grab Raspberry Pi OS Lite
Head over to raspberrypi.org and download the latest copy of the Raspberry Pi OS (Lite). Every single Pi I have ever setup has been a headless machine so you don’t need the bloat of the desktop environment. Etcher is a great tool for flashing the downloaded image to your SD card. Whilst you have the SD card in your computer, create a blank file called
ssh in the
boot directory to enable
ssh and consider adding wireless network information if your Pi will be using WiFi.
Set a fixed IP
A fixed IP will make it easier to reliably
ssh into the machine and browse to any services it may be running. I always set a static IP via my router. I find this a little quicker and simpler to do than on the Raspberry Pi itself and it makes the Pi more portable should I ever change my network, or take it elsewhere. Your router documentation should be able to guide you through the process.
Login and update
Once you have set your static IP you can login via terminal with the
pi username and the default password of
raspberry. You can then update the default software stack using:
sudo apt-get update sudo apt-get upgrade
This little utility will do a number of things. It will expand the file system, giving you access to the entire SD card, set a memorable hostname and lower the GPU memory to minimum (16mb). With the Pi running headless you shouldn’t really need any graphics memory and this gives you the maximum amount of RAM for your projects and code. Run the utility with
raspi-config, key around to change the above settings. Once that is done you will need to reboot and login again.
After rebooting, it is worth checking your time and timezone with
dpkg-reconfigure tzdata. It has a similar interface to
raspi-config and should be easy to find your way around with the keyboard.
Add a new user
It is a good idea to create your own user account and trash the original pi user. This will keep your Pi nice and safe should you expose it to the internet or invite a hacker to stay in your house. Run the following commands in sequence replacing
<name> with your chosen username.
sudo useradd -m <name> -G sudo sudo passwd <name> exit ssh <name>@192.168.1.10 sudo adduser <name> video sudo deluser -remove-home pi
These steps will:
- Create you new user and add it to the
- Setup your password for your new user
- Log back in again (remember to change the IP to that of your Pi)
- Add your new user to the video group which fixes a few Raspberry Pi tools like checking CPU temp
- Delete the original pi user and all files in the home directory (make sure you have backed up what you need there first)
If you find you are unable to autocomplete (tab) commands in your terminal with your new user, it may be because you are using the wrong shell. You can fix this following the guide here.
sudo apt-get install git-core
I try to store most of my project code on GitHub if possible and the
git-core package will make it possible to push and pull code from the command line. Whilst you are messing around with Git, I recommend taking a look at the log2ram project. It will store all of your system logs in RAM which is useful for reducing SD card wear and prolonging the life of the card. I don’t use it on all my Pi projects but it can be useful on busy systems where you have some RAM to spare.
As well as the usual processor overclocking that you can do (I haven’t felt the need yet), you can also improve the performance of the SD card. To do so edit
boot/config.txt and add
dtoverlay=sdhost,overclock_50=100 on a new line in the file. The overclock value will need to be adjusted to suit the SD card you are using. It is a fairly involved process to get the best value for your card but can make a real difference.
dmesg is also useful for spotting issues. You should note that this change can cause issues with WiFi too if you are relying on that for your network connection.
One of my favourite things about Raspberry Pis is how power efficient they are. It means you can leave them all the time without worrying about the environmental impact or huge energy bills. With a few additional tweaks you can save a few more milliamps. There are some great tips come from Jeff Geerling in these excellent posts that are worth running through. My favourite tweak is disabling the HDMI circuitry by adding
/usr/bin/tvservice -o to
Docker is a containerisation software that allows you to isolate software applications. Containers are more lightweight than traditional virtual machines. I find Docker the best way to run services on the Raspberry Pi as it allows me to get the most out of the resources available and removes the need to for reformat the SD card every time my tinkering goes a bit too far. Install docker with the following command and then add your user to the docker group:
curl -sSL https://get.docker.com | sh sudo usermod -aG docker <name>
Once again replace
<name> with your chosen username. I recommend installing docker compose too. This will allow you to configure and start you containers reliably from
docker-compose.yml config files.
sudo apt-get install python-pip sudo pip install docker-compose
This last Docker trick is optional. Docker can use quite a bit of space on your SD card so if you have external storage (like a hard drive) it can sometimes be beneficial to move dockers internals to a different storage location. Edit the docker.service file (
sudo nano /lib/systemd/system/docker.service) and modify the line starting with
ExecStart to be:
ExecStart=/usr/bin/dockerd -H fd:// -g /path/to/external/storage/
The path should be to your external storage. You can then restart Docker and check the change has saved by running:
sudo systemctl daemon-reload sudo systemctl restart docker docker info | grep "loop file\|Dir"
Updating Docker in future may require you to make this change again. You will know if it needs doing as your containers and images will disappear after an update.
Almost done. These next few steps will configure SSH, allow for a simpler login (SSH via public key) and mount an external server using
sshfs. First generate some keys with
ssh-keygen -b 2048 -t rsa and add public keys from your other computers to:
sshfs should be installed with
sudo apt-get install sshfs. Once installed you can mount your external server by editing
/etc/fstab and adding:
<name>@192.168.1.11:/media/data/ /media/path/ fuse.sshfs _netdev,nonempty,allow_other,IdentityFile=/home/<name>/.ssh/id_rsa,reconnect,ServerAliveInterval=45,ServerAliveCountMax=2 0 0
Make sure you set the relevant paths, IP addresses and usernames in the fstab entry above. Test it without a reboot by typing
sudo mount -a.
With all that completed you are now set with the perfect slice of Pi. Time to get hacking on your projects!
- Tweak your swap if RAM is tight (works well with compute modules)
- Encrypt your home folder
- Encrypt an external drive
- Mount a samba/Windows share
- Adjust the thermal properties of the CPU