Setting Up A Raspberry Pi Home Server
If you are looking to start a homelab a Raspberry Pi computer is a great option as it is low noise, low power and is easily expanded with further modules if you need new storage or other features out of your Raspberry Pi like connecting cameras or displays. As a homelab device, it is a great way to start building out a lab and working on your projects in a cheaper and smaller form factor. Or if you want the challenge you can try and run as much as possible to get the most out of this small device.
In this article, we will cover how you install the Rasbian OS, how you can configure remote access to that newly set-up device and finally how you can start using configuration as code tools like Ansible to manage what is running on your device easily.
Any code snippets in this post will be on the Raspberry Pi OS but should work on Debian-like Linux systems.
Installing And Setting Up Your OS
To start deploying applications to your Raspberry Pi and making use of it you will need to install an operating system. You should select this operating system based on what you wish to use this machine for; being that a basic machine to run services on or a full virtualisation machine with something like Docker.
Some common operating systems to look into include; Raspberry Pi OS and Ubuntu. For any operating system, you choose to try and pick a minimal install to keep the size of the OS small and make running things easier on less powerful devices. A step-by-step guide for how you can install a Raspberry Pi OS to your Raspberry Pi can be found here.
One thing you will want to make note of when running an operating system on a Raspberry Pi and using the SD card slot is that SD card memory does fail so be ready for it. With how the Raspberry Pi cycles power and with the low write capacity of SD cards it will be likely to get damaged the SD card's memory with usage over time.
There is some mitigation you can make that you should be aware of like switching your SD card mounts to read-only or using more stable storage like an add-on SSD or HDD for your operating system like the one seen here.
Configuring Remote Access
With your operating system, we will want to configure it so we can access it from another machine via SSH. By default, your operating system will automatically get an IP address from your router via DHCP but we will want to configure a static address so we can always connect to the same address to access our Raspberry Pi that will persist between restarts.
To update your network interface to use a static IP address edit the contents of the /etc/dhcpcd.conf
file. Simply switch out RASPBERRY_PI_IP
and GATEWAY_IP
for the IP address you want the Raspberry Pi to use and the IP address of your router respectively.
---------
interface eth0
static ip_address=RASPBERRY_PI_IP/24
static routers=GATWAY_IP
static domain_name_servers=1.1.1.1
---------
With your Raspberry Pi now accessible at a static IP address, we will want to enable SSH on the Raspberry Pi OS so we can access it remotely. On the Raspbian OS using the command line, you can easily enable the SSH service using the following commands.
---------
# Set the ssh service to start on boot
sudo systemctl enable ssh
# Start the ssh service immediately without a reboot
sudo systemctl start ssh
---------
With the ssh service running on the raspberry pi you can now SSH into the raspberry pi from another machine. Using the ssh client from another machine run the following command substituting your raspberry pi’s static IP address configured earlier.
---------
ssh pi@RASPBERRY_PI_IP
---------
You'll be prompted for your Raspberry Pi’s password and once logged in you can run commands on the Raspberry Pi machine as the pi user. This is a very helpful pattern for accessing the machine through other tooling and is just more conveniently than having to set up another set of IO devices to work on it. In the next section, we will cover how you can use some configurations as code tools to manage the services and config on the raspberry pi in an easy and convenient manner.
Running Services With Config As Code
Your Raspberry Pi is now set up to be accessed remotely, you can start running some of your own services on it or configure it further for your homelab’s needs. To make this process easier and more importantly repeatable if you wanted to run the same configuration steps on another Raspberry Pi we can use tools that accept configuration as code which is a concept that states that we can define our computers configuration and services through the use of simple configuration written as code that can be run repeatably and get the same results each time. This enables more predictable management of your machines and allows for the same configuration settings to be run multiple times against one or many hosts.
In this section, we will provide an example of how you can use ansible to write simple “playbooks” that define the sequential tasks you want to run on a target host. For the example ansible playbook, we will install some packages to the host, and create another user on the host.
---------
# example-playbook.yaml
- name: Configure the raspberry pi
hosts: RASPBERRY_PI_IP
tasks:
- name: "Update Apt Cache"
apt:
update_cache: yes
tags: installation
- name: "Install Common packages"
apt:
name: ['runc', 'python-pip', 'docker.io', 'python3-venv', 'docker-compose']
state: latest
tags: installation, packages
- name: "Python Docker"
pip:
name:
- docker
tags: python
- name: "Install Minikube"
shell:
cmd: curl -Lo ~/minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-arm && chmod +x ~/minikube && mkdir -p /usr/local/bin/ && install ~/minikube /usr/local/bin/
tags: installation, minikube
---------
As you can see in the playbook we configured the IP address of the host along with what login credentials we would like ansible to use such as the ssh username and password along with the sudo password ansible can use to escalate privileges when needed for performing some actions on a host. Ansible has many modules available for common Linux system functionality and further modules available for other services or applications you may want to interact with such as AWS, other operating systems or something like Docker.
You can now use the following script to run the created playbook against the configured target host.
---------
ansible-playbook example-playbook.yaml
---------
You will see the output for each task as it is run on your raspberry pi. As you can see this configuration as a code tool can allow for a very easy way to configure and run a set of commands on a machine to produce the desired result. This will save you more time debugging as the results of each command should be a lot more predictable. And the worst-case scenario if everything is broken, just start again with a fresh OS and run your playbook again.
If you're looking for some more services to run on your stack check out the below article for 10 of the most popular self-hosted apps you can run.
Wrapping Up
The Raspberry Pi platform is very easy to work with and provides you with an excellent level of customizability for whatever you are looking at learning or running on it. Now you should have a better idea about some of the good considerations to make when setting up the pi, how you can configure it for easier remote access and how you can use configuration as code to manage your pi and the software on it a lot easier. I would recommend once you have your raspberry pi setup look at setting up some important services on the machine such as backups for important configurations and files you need to be saved off the raspberry pi, and Setup alerting for important services or logs.
Connect Further
- If you are in the market for some homelab or home server products check our store's products.
- You can also find some of our other blog posts.
- Connect on our social media accounts over on Twitter, Facebook and Instagram.