Creating a Minimal Server on Raspberry Pi

Creating a Minimal Server on Raspberry Pi
Introduction: Don’t rush to throw away that old Raspberry Pi; this detailed step-by-step guide shows how to make the most of my precious Raspberry Pi system resources with a minimal setup.
Word count: 4981, estimated reading time: 7 minutes
https://linux.cn/article-14405-1.htmlAuthor: Alan Formy-duvalTranslator: hwlife

Recently, my Raspberry Pi🔗 opensource.com‘s microSD card stopped working. It had been running as a server for nearly two years, which provided me with a good opportunity to start exploring and fixing the issue. After the initial installation was completed, it started experiencing some disk-related problems, and the official Raspberry Pi operating system released a significant update (renamed from Raspbian to Raspberry Pi OS(Raspberry Pi OS)). So I bought a new storage card and started reinstalling.

Although the Raspberry Pi 3B is not the latest hardware, it is still sufficient for running a diverse set of services on a minimal server. I believe my previous installation used a full installation image that included a graphical user interface and many other unnecessary packages.

This detailed step-by-step guide shows how to make the most of my precious Raspberry Pi system resources with a minimal setup.

Creating a Minimal Server on Raspberry Pi

Getting Started

First, create a new system drive for the Raspberry Pi. This requires two things: a system image file and a microSD card.

Creating a Minimal Server on Raspberry Pi

Download the Raspberry Pi OS image file

While there are several operating systems to choose from, I stick to the official Raspberry Pi supported system.

The first step is to download the latest system image file from the Raspberry Pi OS🔗 www.raspberrypi.org official website to your computer and then write it to the storage card. They offer three different images, and I chose the Lite version. It is a minimal operating system that only contains the necessary files for the basic system, so it takes up the least disk space and system memory. (When I downloaded the system, the release date was August 20, 2020, but it has certainly been updated since. I don’t think there will be any huge differences, but I recommend reading the release notes.)

Creating a Minimal Server on Raspberry Pi

Write the Raspberry Pi OS image to the storage card

The second step is to write the downloaded system image to the storage card. My card had been used before, and when I inserted it into my Linux desktop computer, it automatically mounted two existing partitions. I couldn’t write the image until I unmounted these two partitions.

To do this, I had to use the following lsblk command to determine their paths, and it turned out that the device path was /dev/mmcblk0:

# lsblk -p

I unmounted the two partitions using the umount command:

# umount /dev/mmcblk0p2
# umount /dev/mmcblk0p1

Once the partitions were unmounted, the image file could be written to the storage card. Although there are many graphical writing tools, I still prefer using the old dd command:

# dd bs=4M if=/home/alan/Downloads/raspios/2020-08-20-raspios-buster-armhf-lite.img of=/dev/mmcblk0 status=progress conv=fsync

Creating a Minimal Server on Raspberry Pi

Booting the Raspberry Pi

You only need a monitor, keyboard, and power adapter to use the Raspberry Pi. I also have an Ethernet cable for network connection; I prefer connecting a dedicated server via cable rather than wireless.

Insert the storage card and turn on the power to the Raspberry Pi. Once it successfully boots, log in using the default credentials: username pi, password raspberry.

Creating a Minimal Server on Raspberry Pi

System Configuration

Follow the steps below to minimize disk space, memory usage, etc. I recommend taking the time to research each configuration to make it as accurate as possible. There are usually several ways to configure applications, and some configuration files and options may be deprecated, so check the product documentation to ensure you are not applying outdated configurations.

Creating a Minimal Server on Raspberry Pi

Run raspi-config

The main configuration program for the Raspberry Pi system is called raspi-config. Run it immediately after logging in:

# raspi-config

Creating a Minimal Server on Raspberry Pi

Raspberry Pi config main window

It presents an option to expand the root filesystem to utilize all available space on the storage card. After selecting this option, restart and log in again.

Use the df command to verify that the total capacity of the storage card is fully utilized:

# df -h

If you need to set other options, run raspi-config again. Some of these options can vary based on your preferences and configurations. Carefully check all these options to ensure nothing is missed. For optimal performance, I recommend making the following adjustments. (I skipped some options that we did not change.)

System Options(System options): Here you can set the hostname; it is best to use a fully qualified domain name (FQDN). You can also change your password here, which is always strongly recommended.
Interface Options(Interface options): Enable the SSH service.
Performance Options(Performance options): Reduce GPU memory to the minimum (16MB).
Localization Options(Localization options): Choose your timezone, location, and keyboard type.
Advanced Options(Advanced options): This option includes the option to expand the root filesystem. If you did not expand it above, make sure to do it here. This way you can access all available space on the storage card.
Update(Update): Entering the update option will immediately check for updates to the raspi-config tool. If updates are available, they will be downloaded and applied, and raspi-config will restart in a few seconds.

Once you have completed these configurations in raspi-config, select “Finish(Finish)” to exit the tool.

Creating a Minimal Server on Raspberry Pi

Manual Configuration

I also suggest a few other changes that all require editing some configuration files to manually change settings.

Creating a Minimal Server on Raspberry Pi

Set a Static IP Address

Generally, it is better to set a server with a static IP address. Verify the network interfaces with the ip command and set the IP address, your default gateway (router), and domain name server (DNS) addresses:

# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether b8:27:eb:48:3f:46 brd ff:ff:ff:ff:ff:ff

You will also need to know your default gateway and one or more DNS server addresses. Add this information to the /etc/dhcpcd.conf configuration file (I strongly recommend making a backup of this file before making changes):

# cd /etc
# cp -a dhcpcd.conf dhcpcd.conf.original

Edit the file as follows:

# vi dhcpcd.conf

# static IP configuration:
interface eth0
static ip_address=192.168.1.5/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.3 192.168.1.4

Creating a Minimal Server on Raspberry Pi

Disable IPv6 Protocol

Unless you have a specific need to use IPv6, you may prefer to disable it. To do this, you can create two new files, each containing a single line directive instructing the Linux kernel not to use IPv6.

First, create the /etc/sysctl.d/disable-ipv6.conf file with a single line directive:

# cd /etc/sysctl.d
# echo "net.ipv6.conf.all.disable_ipv6 = 1" > disable-ipv6.conf

Then create the /etc/modprobe.d/blacklist-ipv6.conf file containing a single line directive:

# cd /etc/modprobe.d
# echo "blacklist ipv6" > blacklist-ipv6.conf

Creating a Minimal Server on Raspberry Pi

Disable Wi-Fi, Bluetooth, and Audio

The specific use case for my server does not require Bluetooth and audio, and it connects via Ethernet, not wireless (Wi-Fi). Unless you plan to use them, follow the steps below to disable them.

Make the following changes to the /boot/config.txt file (again, I recommend making a backup of this file):

# cd /boot
# cp -a config.txt config.txt.original

Add the following two directives to the bottom of the file to disable Bluetooth and Wi-Fi:

dtoverlay=disable-bt
dtoverlay=disable-wifi

These echo commands can complete the task:

# cd /boot
# echo "dtoverlay=disable-bt" >> config.txt
# echo "dtoverlay=disable-wifi" >> config.txt

To disable audio, change the dtparam=audio parameter to off. You can do this with a short sed command:

# sed -i '/dtparam=audio/c dtparam=audio=off' config.txt

The final step is to disable the Wi-Fi service using the systemctl mask command:

systemctl mask wpa_supplicant.service

If you don’t need other services, you can disable them as well:

◈ Disable the modem service: systemctl disable hciuart
◈ Disable the Avahi daemon: systemctl disable avahi-daemon.service

Creating a Minimal Server on Raspberry Pi

Final Steps

Check your memory usage:

# free -h

I was shocked: my system was only using 30MB of memory.

Create personal accounts: It is advisable to create user accounts for individuals logging into this server. You can assign them to the sudo group to allow them to run administrative commands. For example, create an account with the username George:

# adduser george
# usermod -a -G adm,sudo,users george

Perform updates: This is an important step. Apply updates to get the latest fixes for the Raspberry Pi operating system.

# apt update
# apt full-upgrade

Reboot: Restarting your new server is a good idea:

# systemctl reboot

Install Cockpit: You can install the renowned Linux web console Cockpit🔗 cockpit-project.org on the Raspberry Pi system, which provides an HTML-based interface for remotely managing and monitoring your server. I recently wrote an article on Getting Started with Cockpit🔗 opensource.com. Use this command to install it:

# apt install cockpit

Now my Raspberry Pi server is ready to host services, and I can use it for web server🔗 opensource.com, VPN server🔗 opensource.com, Minetest🔗 github.com game servers, or as I did, a Pi-Hole based ad blocker🔗 opensource.com.

Creating a Minimal Server on Raspberry Pi

Keeping Old Hardware Alive

No matter what hardware you have, carefully streamlining and controlling your operating system and packages can keep your system resource usage low, allowing you to get the most out of it. This can also improve security by reducing the number of services and packages available for potential malicious actors to exploit.

So before you discard old hardware, consider the various possibilities for continued use.

via: https://opensource.com/article/21/1/minimal-server-raspberry-pi

Author: Alan Formy-Duval Topic: lujun9972 Translator: hwlife Proofreader: wxy

This article is originally compiled by LCTT and honorably presented by Linux China

Creating a Minimal Server on Raspberry Pi
Welcome to reprint in accordance with the CC-BY-SA agreement,
If you need to reprint, please leave a comment below the article “Reprint: Public Account Name“,
We will add you to the whitelist and authorize “Reprint articles can be modified“.

Leave a Comment

Your email address will not be published. Required fields are marked *