Headless Raspberry Pi Server

This post covers setting up a headless Raspberry Pi 4 B from scratch.

Raspberry Pi has excellent documentation on how to setup and configure a Raspberry Pi. This post leans on the official documentation and focus on the parts that are essential to get up and running.

This is part 1 of 4 in a mini series where we will configure a headless Raspberry Pi 4 B as an efficient home server, capable of hosting Network Attached Storage (NAS), TimeMachine and Plex Media Server.
Through this series we will also look at how we can make automatic backups to keep uor data safe.

Prerequisites

Hardware Requirements

Software Requirements


Installing Raspberry Pi OS

Raspberry Pi have made it easy to install Raspberry Pi OS using Raspberry Pi Imager which can download the Raspberry Pi OS image automatically and install it on your micro SD card.

For this install you are going to be using the Recommended Raspberry Pi OS (32-bit) with Desktop.

For security reasons, WiFi and SSH is not enabled by default. To enable it you have to open the Advanced Menu.

In the Advanced options you are going to Enable SSH, Set username and password, Configure wireless LAN (don’t forget to set the correct Wireless LAN country).

Having chosen the options you want, select your micro SD card from the menu and write the Raspberry Pi OS image to it.

Eject the micro SD card, remove it from your computer and insert it in to the Raspberry Pi.
Plug the USB-C power supply in to the Raspberry Pis power port.
Give the Raspberry Pi some time to boot up - about 90 seconds or so on first boot.

Login and setup Raspberry Pi

With your Raspberry Pi now booted and on the network you can connect to it using SSH.
If you are on Windows, and need to get acquainted with SSH read this before continuing: Secure Shell (SSH).
macOS and other UNIX-like systems have SSH preinstalled. Here is a quick guide: OSXDaily - How to SSH

Your Raspberry Pi can be found on your network using its hostname (example: raspberrypi.local). However to do this on Windows, Bonjour for Windows needs to be installed.


Logging in

First lets clear out any previous references to raspberrypi.local.
Open a Terminal window and run the following command:

ssh-keygen -R raspberrypi.local

Don’t worry if you get a host not found error. This just means there where no previous references to raspberrypi.local.

Then sign in to your Raspberry Pi with the following command:

ssh pi@raspberrypi.local

If the command hangs, press Ctrl-C and try again.
If prompted with a warning, hit enter to accept the default (yes).
When prompted, type the password you set in Raspberry Pi Imager and hit Enter - don’t worry if the prompt doesn’t seem to register that you are typing, it is.


Getting the latest updates

Before you start using your Raspberry Pi, you want to make sure that your system is up to date.

The easiest way to manage installing, upgrading, and removing software is using APT (Advanced Packaging Tool) from Debian.

Before installing software, you should update your package list with apt update using this command:

sudo apt update

Next, upgrade all your installed packages to their latest versions with the following command:

sudo apt full-upgrade

Note that full-upgrade is used in preference to a simple upgrade, as it also picks up any dependency changes that may have been made.


Congratulations! You now have a headless Raspberry Pi server.

RealVNC (Optional)

If you want to gain remote access to the Raspberry Pi’s Desktop, read a bit further.


Enable VNC Server

To gain graphical remote access to the Raspberry Pi, you have to enable the VNC Server with raspi-config:

sudo raspi-config

Then Navigate to Interfacing Options.
Scroll down and select VNCYes


Installing RealVNC Viewer

Browse to: https://www.realvnc.com/en/connect/download/viewer/
Download and install the version applicable for your system.


Connect over VNC

Launch RealVNC Viewer on your computer and enter the name of your Raspberry Pi server (raspberrypi.local) in to the address bar.

When prompted enter your username and password.


And there you have it, the Raspberry Pi OS Desktop.


Troubleshooting: Creating a Virtual Desktop

If you see a black screen instead of the Raspberry Pi Desktop, it may be because your Raspberry Pi isn’t running a graphical desktop.

VNC Server can create a virtual desktop for you, giving you graphical remote access on demand. This virtual desktop exists only in your Raspberry Pi’s memory.

To create a virtual desktop, run the following command:

vncserver

Make note of the IP address/display number that VNC Server will print to your Terminal (e.g. 192.168.0.200:1).
(Note, the Raspberry Pi’s hostname can also be used together with the display number e.g. raspberrypi.local:1)

On the device you’ll use to take control, enter this information into VNC Viewer.

To destroy a virtual desktop, run the following command:

vncserver -kill :<display-number>

This will also stop any existing connections to this virtual desktop.

Sources

Christophe Knage · 2023-01-27