
Recently, my Raspberry Pi’s microSD card stopped working. It has been running as a server for nearly two years, which gave me a great opportunity to start exploring and fixing the problem. After the initial installation was completed, it began to show some disk-related issues, and the official Raspberry Pi operating system released a significant update (renamed from Raspbian to 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 minimal server with diverse services. I believe my previous installation used a full installation image, which included a graphical user interface and many other unnecessary packages.
This detailed step-by-step guide shows how I made the most of my precious Raspberry Pi system resources with a minimal setup.
Getting Started
First, create a new system drive for the Raspberry Pi. This requires two things: a system image file and a microSD card.
Download the Raspberry Pi system image file
While there are several operating systems available, I stick to the officially supported Raspberry Pi system.
The first step is to download the latest system image file from the Raspberry Pi OS official website to your computer and then write it to the storage card. They provide three different images, and I chose the lite version. It is a minimal operating system that only contains the essential files for the basic system, so it takes up the least amount of disk space and system memory. (When I downloaded the system, the release date was August 20, 2020, but it has definitely been updated since then. I donβt think there would be any major differences, but I recommend reading the release notes.)
Writing the Raspberry Pi system 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 loaded two existing partitions. Before I could write the image, I had to unmount these two partitions.
To do this, I had to use the lsblk command to determine their paths, and it was confirmed 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 to use 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
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 to connect a dedicated server via cable rather than wireless.
Insert the storage card and power on the Raspberry Pi. Once it successfully boots, log in with the default credentials: username pi
, password raspberry
.
System Configuration
Follow the steps below to minimize disk space, memory usage, etc. I recommend spending as much time as possible researching each configuration to get it as correct as possible. There are usually several methods to configure applications, and some configuration files and options may be discarded, so check the product documentation to ensure you are not applying outdated configurations.
Running raspi-config
The main configuration program for the Raspberry Pi system is called raspi-config
. Run it immediately after logging in:
# raspi-config
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, reboot 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 may vary based on your preferences and configurations. Carefully check all these options to ensure nothing is overlooked. For optimal performance, I recommend making the following adjustments. (I skipped some options that we didnβt change.)
raspi-config
tool has updates available. If updates are available, they will be downloaded and applied, and raspi-config
will reboot in a few seconds.Once you have completed these configurations in raspi-config
, select βFinishβ to exit the tool.
Manual Configuration
I also recommend several other changes, all of which require editing some configuration files to manually change the settings.
Setting a Static IP Address
Generally, it is better to set a server with a static IP address. Use the ip command to verify the network interface and set the IP address, default gateway (router), and domain name service (DNS) address:
# 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
Disabling IPv6
Unless you have a specific need to use IPv6, you may prefer to disable it. To do this, you can create two new files that include a single line directive instructing the Linux kernel not to use IPv6.
First, create the /etc/sysctl.d/disable-ipv6.conf
file containing 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
Disabling Wi-Fi, Bluetooth, and Audio
The specific purpose of 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 accomplish this:
# cd /boot
# echo "dtoverlay=disable-bt" >> config.txt
# echo "dtoverlay=disable-wifi" >> config.txt
To disable audio, change the parameter of dtparam=audio
to off
. You can accomplish 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:
The Final Step
Check your memory usage:
# free -h
I was shocked: my system only used 30MB of memory.
Create a personal account: It is recommended to create user accounts for individuals logging into this server. You can assign them to the sudo
group allowing them to run administrative commands. For example, create an account with the username George.
# adduser george
# usermod -a -G adm,sudo,users george
Update: This is an important step. Apply updates to get the latest fixes for the Raspberry Pi operating system.
# apt update
# apt full-upgrade
Reboot: Rebooting your new server is a good idea:
# systemctl reboot
Install Cockpit: You can install the famous Linux web console Cockpit 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. Use this command to install it:
# apt install cockpit
Now my Raspberry Pi server is ready to host services, and I can use it as a web server, VPN server, Minetest game server, or as I did, a Pi-Hole based ad blocker.
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 also enhances 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 continuing to use it.
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 proudly presented by Linux China