Setting up and configuring Raspberry Pi OS can be a streamlined process by using chroot on a different machine. This guide will walk through the steps to prepare your Raspberry Pi SD card on a laptop, eliminating the need for direct access to the Raspberry Pi during the initial setup. By the end of this guide, you'll have a fully configured Raspberry Pi OS with network settings, a working SSH connection, and optional proxy configurations (also for users in Mainland China).
Why Chroot?
Chrooting is a very easy way to configure an sdcard, and I just don't like the idea of a desktop environment on Raspbery Pi, which complicates things.
Chroot is very powerful, and there are certain limitations(for example, not able to sudo
or start a systemd
service inside chroot)
Install Qemu
This is necessary for chrooting the sdcard on my laptop
sudo apt install qemu-user-static
Copy Necessary Files
Before configuring the Raspberry Pi, we need to copy some essential files to the target file system. This allows for chrooting(since the laptop is usually x86 and the rasp pi is usually arm
, and we need the resolve conf to have network connection. These files include qemu-aarch64-static
and resolv.conf
.
# Copy the QEMU executable to simulate the ARM processor in the Chroot environment
sudo cp /usr/bin/qemu-aarch64-static /media/user/rootfs/usr/bin/
# Copy the DNS configuration file to ensure proper network connection
sudo cp /etc/resolv.conf /media/user/rootfs/etc/resolv.conf
Mount File Systems and Enter Chroot Environment
Mounting the file systems and entering the Chroot environment allows us to perform configuration operations in the target system.
Mount the /dev /proc /sys
directories to access devices, processes, and system information in the Chroot environment.
# Mount the /dev directory
sudo mount --bind /dev /media/user/rootfs/dev
# Mount the /proc directory
sudo mount --bind /proc /media/user/rootfs/proc
# Mount the /sys directory
sudo mount --bind /sys /media/user/rootfs/sys
# Enter the Chroot environment
sudo chroot /media/user/rootfs /bin/bash
Modify Source List
To speed up package downloads, change the default software sources to local mirrors.
# Replace the default Raspberry Pi software sources with USTC mirrors
sudo sed \
-e 's|http://archive.raspberrypi.org|http://mirrors.ustc.edu.cn/raspberrypi|g' \
-e 's|http://archive.raspberrypi.com|http://mirrors.ustc.edu.cn/raspberrypi|g' \
-i.bak \
/etc/apt/sources.list.d/raspi.list
# Replace Debian software sources with USTC mirrors
sudo sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# Update and upgrade packages
apt update && apt upgrade
Unmount File Systems
After completing the configuration, unmount the previously mounted file systems.
# Unmount /dev, /proc, and /sys directories
sudo umount /media/user/rootfs/dev /media/user/rootfs/proc /media/user/rootfs/sys
# Unmount the root file system
sudo umount /media/user/rootfs
Adding the Wifi Service
After chrooting, I can run commands now.
I add the wifi service to automatically connect to Berkeley Eduroam. So
I add a service /etc/systemd/system/connectwifi.service
[Unit]
Description=Connect to BerkeleyEduroam
After=network.target
[Service]
Type=oneshot
ExecStartPre=/bin/sleep 30
ExecStart=/usr/local/bin/connectwifi.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Where the connect wifi script(add the appropriate wifi)
nmcli connection delete ''
nmcli con add con-name '' \
ifname wlan0 \
type wifi \
ssid '' \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk ''
nmcli con up ''
Adding a Sanity Check
I need to get the ip address for ssh, because when I tried using nmap to find the ip to ssh into it returned dozens of
addresses and I couldn't find the appropriate one.
To make a sanity check to see if Rasp Pi is connected to wifi, I added
another service, I post the ip address online every minute
[Unit]
Description=Send notification to webhook.site
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/send_webhook.sh
[Install]
WantedBy=multi-user.target
I used webhook, and the script
#!/bin/bash
IP_ADDRESS=$(ip addr show wlan0 | grep inet\b | awk '{print $2}' | cut -d/ -f1)
curl -X POST https://webhook.site/da5810be-d59c-4746-962e-bccd2a778cc3 -d ip=$IP_ADDRESS
Configure Passwords
Then configure the password of user pi
.
ssh
After setting up the network to start default at boot, I could ssh into
it from the same network. ssh pi@10.40.88.225
Using Clash
This is only needed in Mainland China to bypass the firewall.
Download the Clash executable file and transfer it to the Raspberry Pi. Clash is a multi-platform proxy client.
Download the Arm-64 Linux Version, then use SCP to transfer it to the Raspberry Pi.
Initiate Clash
To start Clash, run the following command. This will initiate Clash using the specified configuration file.
clash -d . -f $config_file
Switch Between Different Proxies
To switch between different proxies, we will utilize the Clash API. Follow these steps:
First, create a script to list the available proxy servers from the configuration file. This script will extract the server names using awk.
servers=$(awk '/^ - name:/{print $3}' $config_file | tr -d '')
After listing the servers, you can switch the proxy server by making an API call to the Clash server(assuming port 9090)
curl -X PUT -H Content-Type: application/json -d {\name\:\$server\} http://localhost:9090/proxies/GLOBAL
Set Proxy
Save this as a file set_proxy.sh
then do source set_proxy.sh
(using bash set_proxy.sh
doesn't work)
export http_proxy=http://localhost:7890
export https_proxy=http://localhost:7890
export all_proxy=http://localhost:7890
Trying basic commands
echo 0 | sudo tee /sys/class/leds/ACT/brightness # Turn off LED
echo 1 | sudo tee /sys/class/leds/ACT/brightness # Turn on LED
echo $(($(cat /sys/class/thermal/thermal_zone0/temp)/1000))°C # Show temperatures
Transfering Files
scp <filename> pi@10.40.88.225:/home/pi/
Playing Music
I can move mp3 files into the Rasp Pi OS then use cvlc, then listen with a earphone for example
cvlc yt1s.com - Би2 Молитва OST Метро.mp3
I can adjust volumes with alsamixer