Raspberry Pi Server
This page will get you started running a Raspberry Pi-based headless server in your home.
- What is a server?
- Getting a Raspberry Pi
- Setting up your Raspberry Pi
- Developing a project
- Problems, Troubleshooting, etc.
This is going to be awesome. Let's get started!
1. What is a server?
One way of categorizing different types of computers is by their intended use. A laptop is a portable computer that can be used by people for a variety of purposes—you're probably reading this on a laptop right now. A desktop computer has a different form factor—a separate monitor and keyboard, maybe, with a "tower" nearby—but the essential use is the same as the laptop in most cases. The term "desktop" can also be used to describe the visual interface that most people use to interact with the computer, with its Desktop background, cute folder icons to represent your directories, etc.
A server is similar in most ways to a desktop or laptop environment; it, too, has files organized into directories, and runs programs, accepts input, produces output, etc. The term "server," however, often indicates that the computer is intended to be used in a networked environment, perhaps "serving" up webpages for a website on the internet. A server is a little more industrial, in that sense.


Another distinction is the fact that most servers are operated "headless"—they don't have a monitor attached to them presenting a window-based desktop environment like macOS or Windows do. They don't even have a keyboard connected to them. If you want to interact with one of these machines, you'll need to use a terminal to SSH in and interact with this machine.
You wouldn't listen to Spotify or play Overwatch on a server, but there are other things one uses a server for.
This project is going to explore some of those ideas.
2. Getting a Raspberry Pi
We're going to use a Raspberry Pi for this project, a small-but-mighty computer that will be running essentially the same software that a commercial-grade server would run.
The specific items we're using in this project include:
- Raspberry Pi 4 computer, 2GB
- Power supply (5V, 3.1A)
- Ethernet cable (for initial connection to home modem or router)
- Micro SD card with Ubuntu Server 20.04 LTS copied onto it (ask me if you want one of these and I'll give one to you)
These items are available from various sources online. Note that many business sell RPi bundled "kits" or "packs" that come with additional materials, including additional adapters, cases, add-on boards, fans, dongles, displays, etc. For this project, you won't need most of those items. Feel free to focus on getting just the essentials: the computer, a power supply, the Ethernet cable, and the microSD card, which you may already have lying around somewhere.
You can get Raspberry Pi 4 computers and power supplies from a variety of sources, including Adafruit, Pi Shop, and Amazon.com.
You'll also need a Micro SD card (see the next section) and a short Ethernet cable.
2.a. Running Ubuntu on a Raspberry Pi
A computer uses Random Access Memory (RAM) to hold the operating system and run programs when it's turned on, and an external memory drive of some sort (a spinning disk or a solid-state drive) to store information that can be retained even when the computer is turned off. The Raspberry Pi that we're using in this project has 2GB of RAM, and uses a microSD card as long-term memory.
If you've been provided with a microSD card with the OS already installed, you can proceed on to the next step below. (Again, I'll be happy to give you one of these if you don't have one—just let me know!) Otherwise, you'll need to take the microSD card and an adapter that can be plugged into your computer and install Ubuntu Server 20.04 LTS (64-bit), as described here.
3. Setting up your Raspberry Pi
Before we begin, you'll need your own laptop and its terminal, as well as the name and password for your home wifi so that we can get the Pi set up on your home network.
With these items assembled, it's time to boot up your server and get it running!
3.a. Booting your server
- Examine your home router or cable modem and find an open ethernet jack.
- Take the short orange ethernet cable and plug it into both the router/modem and the Pi.
- Take the power supply and plug it into the Pi, then a wall outlet. Avoid unplugging the Pi when you haven't formally shut it down. Leave it plugged in until you've manually issued a shutdown command. We'll see how to do that in a few minutes.
- The Pi should have a small red power light that comes on, and a green light that blinks from time to time, indicating activity. Give the Ubuntu Server operating system a minute or two to boot up.
3.b. Logging into your server
- With your laptop, open a terminal and issue the command
arp -na. You should see code that is similar to what is given here.$ arp -na ? (192.168.7.1) at 14:22:db:85:45:d on en0 ifscope [ethernet] ? (192.168.7.47) at 0:e:58:a3:64:22 on en0 ifscope [ethernet] . . . ? (192.168.7.196) at 36:91:84:fd:bc:78 on en0 ifscope [ethernet] ? (192.168.7.206) at e4:5f:1:a:cc:41 on en0 ifscope [ethernet] ? (192.168.7.207) at e4:5f:1:a:cc:42 on en0 ifscope [ethernet] ? (224.0.0.251) at 1:0:53:0:0:fb on en0 ifscope [ethernet]All the192.168.7.xnumbers indicate that that's the subnet our home network is on. - Our server, just booted up, is presumably at one of these addresses. I'm going to choose one of the most recent ones, the
192.168.7.206address, and see if I can connect there. - The default address for a new installation of Ubuntu Server is username
ubuntuand passwordubuntu. Using those credentials and the IP address we've discovered, let's log on to the server from the terminal on our laptop:$ ssh ubuntu@192.168.7.206 The authenticity of host '192.168.7.206 (192.168.7.206)' can't be established. ECDSA key fingerprint is SHA256:alud6gbEsoYNuklArOflCtPcypsCKIALgzIngX67/94. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.7.206' (ECDSA) to the list of known hosts. ubuntu@192.168.7.206's password: ubuntuNote that the password will not appear as you type it in. Once you have successfully entered the password, you'll see something like this:You are required to change your password immediately (administrator enforced) Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-1028-raspi aarch64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Thu Mar 11 02:07:17 UTC 2021 System load: 3.26 Temperature: 51.5 C Usage of /: 6.5% of 29.05GB Processes: 134 Memory usage: 27% Users logged in: 0 Swap usage: 0% IPv4 address for eth0: 192.168.7.206 * Introducing self-healing high availability clusters in MicroK8s. Simple, hardened, Kubernetes for production, from RaspberryPi to DC. https://microk8s.io/high-availability 37 updates can be installed immediately. 12 of these updates are security updates. To see these additinoal updates run: apt list --upgradable The list of available updates is more than a week old. To check for new updates run: sudo apt update The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. WARNING: Your password has expired. You must change your password now and login again! Changing password for ubuntu. Current password:We're in! - Do what it says. Give the server a new password, a seriously good password—you don't want your machine to be compromised and become part of some botnet. I used the password
!!^q$CG232WRClvoOVUMzand wrote it down in a text file on my computer. Do something similar.
If you make a mistake, or don't type the current password "ubuntu" again before entering your new password, it will kick you off the server.
It will also kick you off the server after you've entered your new password! Just log in as userubuntuwith the new password and you'll be ready for the next step.
What if I can't find my IP address?
Finding the correct address for your server so that you can log in is perhaps the trickiest part of this process. If you are unable to find the address for your server using arp, there are a few other strategies you can try.
- Do a port scan using
nmap
You'll need to havenmapinstalled on your computer—a process in itself—but once there you can do a port scan on your local subnet for an open port 22:$ nmap -p22 192.168.7.0/24
Look for an IP address that has Port 22/tcp open for ssh. That should be your Raspberry Pi, and then you can try to connect to it. - Check your network software
If you have network software running on your phone, or if you can log into your modem/router, you should be able to see devices that are currently connected to your network. The Raspberry Pi will show up with the name ubuntu, and should have an IP address listed for it. Try using ssh to connect to that address. - Connect a keyboard and monitor to your Raspberry Pi
- Start with your Raspberry Pi powered off.
- Plug in an Ethernet cable between your modem/router and the Raspberry Pi.
- Into the Pi plug a microHDMI-to-HDMI dongle.
- Into the dongle, plug in an HDMI monitor—your TV works if your cables are long enough.
- Into the Pi plug a USB keyboard (which may need a USB-C to USB-A dongle).
Now you can see why we wanted to try to find the IP using network tools first. ;)
Once everything boots up, you should be able to see what's happening on the monitor that you've plugged in.
With the keyboard directly attached to the Raspberry Pi you'll get a login prompt. Then you can:
- log in using username
ubuntu, passwordubuntu - change your password to a new, strong password
- Run the command
ip aand take note of the section labeled2: eth0. - Find the IP address after
inet:inet 192.168.7.206/24....
That IP address,192.168.7.206, is the one we've been looking for! - We no longer need the monitor, keyboard, etc. Use the keyboard to shut down the server:
sudo shutdown now
Wait a few moments for the server to shut down (make sure the green light is no longer blinking on the Pi), and then unplug everything. - When you're ready to continue with the set-up process, plug in the ethernet cable to your router/modem again, and power up the pi.
- You can now log in knowing the Ethernet IP address!
3.c. Setting up your server
As the owner of a running server, you are now officially a Systems Administrator, or sysadmin, and that involves taking care of some basic installation chores.
For all of these, you need to be logged into the headless server using a Terminal on your laptop.
Also, many administrative commands require that you have special administrative powers which you get by using sudo at the beginning of a command.
- Update the server to the latest software
Updates to your computer include patches for newly discovered security flaws. You can learn later how to keep your server automatically updated, but for now:$ sudo apt update [Lots of output here as the server looks for where it can get updates from...] . . . Fetched 3227 kB in 4s (764 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done 50 packages can be upgraded. Run 'apt list --upgradable' to see them. $ sudo apt upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done . . . 50 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 99.7 MB of archives. After this operation, 270 MB of additional disk space will be used. Do you want to continue? [Y/n] yThe server will go through the process of downloading and installing the upgrades it needs to. - Shutting down the server
By their nature, most servers are assumed to be constantly running so they can do the things they need to do 24/7. If you need to shut the server down, however, so you can move it, or just because you don't want to leave it running:$ sudo shutdown now Connection to 192.168.7.206 closed by remote host. Connection to 192.168.7.206 closed.Once you've issued this command, give it a few more moments for the server to do its shutdown cleanup. Once the green activity light is no longer flashing, go ahead and unplug the Pi.
To restart the server, just plug it back in again. It usually takes a minute or two to completely boot, after which you'll be able to usesshto log in again. - Configure Wifi access
The Raspberry Pi 4 hardware that you're running has a wifi radio built in, but the Ubuntu Server is not set up to run wifi by default. If you'd like to enable wifi on your router (so it doesn't have to be plugged in via that Ethernet cable all the time):- Log on to the server
- Identify the wireless network interface
$ ls /sys/class/net eth0 lo wlan0 wlan1Thewlan0is what we're looking for. - Identify your
netplanconfiguration files
$ ls /etc/netplan/ 50-cloud-init.yamlWe're going to need to edit this file, as well as one other. We'll use the nano text editor that's already installed on the server. - Edit
50-cloud-init.yaml
$ sudoedit /etc/netplan/50-cloud-init.yamlAt this point you'll be using the nano editor to reconfigure that file with your wifi information for your home wifi router. The information you'll need to add to that file is highlighted in bold. DO NOT USE TABS! Only use spaces in this file, and make sure the lines are all lined up exactly as shown.# This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: ethernets: eth0: dhcp4: true optional: true version: 2 wifis: wlan0: optional: true access-points: "SSID-NAME-HERE": password: "PASSWORD-HERE" dhcp4: trueOnce you've made the modifications, use ^X (ctrl-x) to save the file and close the nano text editor. - Edit
/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
As mentioned in the comments of the previous file, we need to create a new file, again using nano:$ sudoedit /etc/cloud/cloud.cfg.d/99-disable-network-config.cfgWhen nano launches, enter this single line into the file, and save and close it.network: {config: disabled} - Apply these changes to netplan
$ sudo netplan apply - Identify wifi IP address
The server is configured to use wifi now, but the wifi uses a different IP address. Identify the new address that you can use to log in to:$ ip a 1: lo:Note that themtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether b8:27:eb:6a:df:3d brd ff:ff:ff:ff:ff:ff 3: wlan0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether b8:27:eb:3f:8a:68 brd ff:ff:ff:ff:ff:ff inet 192.168.7.207/24 brd 192.168.7.255 scope global dynamic wlan0 valid_lft 13403sec preferred_lft 13403sec inet6 fe80::ba27:ebff:fe3f:8a68/64 scope link valid_lft forever preferred_lft forever 4: wlan1: mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 74:da:38:db:28:d4 brd ff:ff:ff:ff:ff:ff wlan0interface is using inet192.168.7.207. That's the port we can log into using wifi. - Confirm login via wifi
Unplug the ethernet cable from your Pi, and confirm that you can log in using the wifi IP:$ ssh ubuntu@192.168.7.207
- Ensure that the time is set correctly on the server
$ sudo dpkg-reconfigure tzdataUse the arrow keys to select first
US, thenPacific OceanOnce this has been set, confirm the current local date and time are correct by using the
datecommand:$ date Sat Mar 20 11:40:47 PDT 2021 - Give your server a (host)name
To more clearly identify your computer on a network you should give it a name. This is called the hostname and it can be anything you want, although it shouldn't be anything too generic. People who have lots of servers to keep track of name their different servers after scientists, or flowers, or cities, or characters in Harry Potter.
I'm going to name mine after my dog, Lola:$ sudo hostnamectl set-hostname lola
The next time you log in, you'll see the new hostname listed in your prompt. You can also check that it worked right now:$ hostname lola - Add another account to your server
Although the userubuntuis fine and you can continue to use it, some people like to have another account, perhaps with their own name. Let's add another user to the server, and make sure that person hassudoprivileges:$ $ sudo adduser rwhite Adding user `rwhite' ... Adding new group `rwhite' (1001) ... Adding new user `rwhite' (1001) with group `rwhite' ... Creating home directory `/home/rwhite' ... Copying files from `/etc/skel' ... New password: Retype new password: passwd: password updated successfully Changing the user information for rwhite Enter the new value, or press ENTER for the default Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n]I just hit [Enter] to skip past the "user information" requested--that isn't necessary.
Let's be sure to add that new account to thesudogroup so that I'll be able to manage the server.$ sudo adduser rwhite sudo Adding user `rwhite' to group `sudo' ... Adding user rwhite to group sudo Done. $There are now two accounts on the server, the defaultubuntuaccount and a newrwhiteaccount. I can log in to either one, and use "sudo" with either one. - Improve nano
We're going to be using nano on our server a bit to create and modify files, so let's fix it up a little so it works better for us. We're going to use nano to create a.nanorcfile that will set some sane defaults for us, making the editor a little easier to use.
While logged on to the server and in your home directory:$ nano .nanorcYou'll be presented with a nano editor. Enter:set tabsize 4 set tabstospaces set softwrap set atblanks set autoindent set linenumbersThen^oto save ("write Out") the file to disk. Save it under the default name that you've already given it. Then^xto exit the editor.
As long as we're discussing nano, why don't you take a look at introduction to nano.
4. Developing a project on your server
Well... now what? You've got a server running. What would you like to do with it?
Take a look at these introductory projects and get started!
- Install some games!
- Learn more about the command line
- Scrape a website to get the weather report
- Get your server to email and text you
- Scheduling events with cron
- Set up a static IP address and port forwarding
- Securing/hardening the server
- Start a webserver
- Build a (small) website
- Write a Twitter bot
- Display the weather using an Open Weather's API
5. Problems, Troubleshooting, etc.
There are a lot of ways things can go wrong with a computer. Occasional issues with a Raspberry Pi include the Pi not getting enough power (your power brick should be sufficient, however), the Pi being unable to boot up (the memory card has become corrupted somehow), and not being able to access the Pi over the network (the wifi network, the radio on the Pi, or Ubuntu's netplan configuration are having issues).
In all cases, try unplugging the Pi to power it down, using an Ethernet cable to directly connect the Pi to your home modem or router, and then trying to log in again. Once you get in you can try to reset the wifi configuration following the instructions above. If you can't log in even when the Pi is connected via an Ethernet cable, you may have more serious issues. See the instructor about resetting your memory card.
Some people find it disconcerting to be working on a computer that has no monitor, keyboard, and mouse. If you have a micro-HDMI to HDMI cable, an external USB keyboard from another PC, and an external USB mouse, you can plug these into the Raspberry Pi and boot it up for a more "normal" desktop experience. (Try sudo apt install lubuntu-desktop and then rebooting your computer to see what this looks like!