Headless setup of a Raspberry Pi
Headless Setup for a Raspberry Pi 3B/4B¶
A generic from-scratch setup guide for the Raspberry Pi can be found in the internet:
"Headless" setup guide, this is what we need.
- Raspberrypi.org: https://www.raspberrypi.com/documentation/computers/configuration.html#setting-up-a-headless-raspberry-pi
- Hackster: https://www.hackster.io/435738/how-to-setup-your-raspberry-pi-headless-8a905f
- Medium: https://medium.com/hackernoon/how-to-setup-a-headless-raspberry-pi-7f2b8b00c790
The most relevant part of those guides is the network setup.
- Official setup guide: https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up
This last one is not specific enough, but it is worth linking to.
OS setup¶
See:
Connection over LAN¶
Choose a connection method¶
Wireless / WiFi: available on RPi 3B or 4B, or by adding a WiFi dongle to an earlier Pi.
- This is only needed if you did not configure the WiFi connection in previous steps (e.g. when loading the OS to the SD card) or if the original WiFi network is no longer available.
- See the update wifi connection section below.
Wired / Ethernet: available on all Raspberry Pi B models.
- Find a suitable ethernet cable.
- Connect the Raspberry Pi to your router.
- You may need to enable ethernet in
cloud-init
if using Ubuntu on the Raspberry Pi.
Find the Pi's IP address¶
Find the Pi's IP address using a network scanning tool:
- First, try connecting to the "raspberrypi.local" address. If your network supports it, this will work out of the box, and is the easiest method. Otherwise consider the following options.
- Use the
Fing
app (Android). - Use your router's admin interface.
- Use the
nmap
tool (linux): example:sudo nmap -sn 192.168.0.0/24
. sudo
is required for getting the full output fromnmap
.- An example of the expected output is shown below.
Example output from sudo nmap 192.168.0.0/16 -p 3333
:
Nmap scan report for 192.168.13.52
Host is up (0.093s latency).
PORT STATE SERVICE
3333/tcp open dec-notes
MAC Address: DC:A6:32:50:03:7D (Raspberry Pi Trading)
Once you find the Pi's IP address (e.g. 192.168.0.13
) write it down.
Test the connection¶
Test a connection to the Pi:
- Through SSH (u:
pi
p:raspberry
) on Linux: - Using a known IP (example):
ssh pi@192.168.0.13
- Using mDNS:
ssh pi@raspberrypi.local
- You may add the
-p 3333
option, to scan only for the default GUI port.
- You may add the
- Using the browser, write the Pi's IP followed by the GUI's port (3333), and the Pipetting GUI should open. This option is only valid if you have flashed our custom OS image instead of a "stock" OS.
- Known IP:
http://192.168.0.13:3333
- Using mDNS:
http://raspberrypi.local:3333
- Known IP:
Tip
The Pi's IP address might expire and change. Repeat the steps above if this seems to have happened. You may also try configuring the Pi or your router to fix it's IP (i.e. setting a "Static IP" or "DHCP Reservation", respectively), or use the Pi's hostname to connect to it (i.e. through mDNS/zeroconf to a a raspberrypi.local address). Guides for this are available on the internet, specific to your internet router and operating system.
Continue with the rest of the setup¶
Go back to the Software Setup Guide to update and install the required software.
Utilities¶
Change the hostname¶
To change the hostname in Ubuntu temporarily, you can use raspi-config
or the hostnamectl
command.
The procedures are different, depending on the OS:
- On RaspberryPi OS, use the
raspi-config
tool. Just use the menus. - On Ubuntu, things are a bit messed up (instructions follow).
To change it in ubuntu, run:
sudo hostnamectl set-hostname NEW_HOSTNAME --static # This is only temporary.
sudo hostname NEW_HOSTNAME
Now, to make it persist across reboots, you need to edit some files:
- Edit
/etc/hosts
: - Replace the old hostname here.
- You can use
vi
,nano
, or use sed:sudo sed -i 's/OLD_HOSTNAME/NEW_HOSTNAME/g'
. - Check with
cat /etc/hosts
. - Edit
/etc/cloud/cloud.cfg
: - Change
preserve_hostname: false
topreserve_hostname: true
(i.e. comment it out). - Add
hostname: NEW_HOSTNAME
(just belowpreserve_hostname
). - Change
- update_hostname
to#- update_hostname
(i.e. comment it out). - Check with
cat /etc/cloud/cloud.cfg
.
Reboot to check if it worked.
Sources:
- https://www.redswitches.com/blog/ubuntu-change-hostname/
- https://www.cyberciti.biz/faq/ubuntu-20-04-lts-change-hostname-permanently/
- https://stackoverflow.com/a/61064656
- https://linuxconfig.org/how-to-set-hostname-on-raspberry-pi
Update WiFi connection¶
Cloud init networks¶
For a cloud-init
setup, as seen in Ubuntu for Raspberry Pi, things are straightforward. See the sections below
- Open the
/etc/netplan/50-cloud-init.yaml
file for editing. - If you do not have access to the pi, either mount the SD card on your PC (instructions here), or login with display and keyboard.
- Make it look familiar (sample below), save, and exit.
- Put the SD card back and/or reboot.
network:
version: 2
# Configure your wifis here.
wifis:
renderer: networkd
wlan0:
access-points:
# First network.
YOUR_SSID_HERE:
password: YOUR_PASSWORD_HERE
# Second network.
ANOTHER_SSID_HERE:
password: ANOTHER_PASSWORD_HERE
# And so on...
dhcp4: true
optional: true
# The following enables ethernet.
ethernets:
eth0:
dhcp4: true
optional: true
Change WiFi in raspi-config¶
You can also connect to a different WiFi with the raspi-config
and nmcli
tools. This is the official method, which works on modern Raspbian.
If you don't have network access, you'll need to connect a display and keyboard to the Pi to access a terminal.
Tip
If on an older version of RPi OS, try editing the wpa_supplicant
file from the SD card (details below).
- Open a terminal on the Pi (see nots above).
- Follow the tutorial here: https://www.raspberrypi.com/documentation/computers/configuration.html#wireless-networking-command-line
WPA Supplicant¶
For WPA-supplicant setups, which work on older Raspbian versions.
Warning
This may not work at all on newer Raspbian versions (>=Bookworm). The wpa_supplicant
"trick" only works at the very first boot.
For details and discussion, see: https://github.com/raspberrypi/bookworm-feedback/issues/72
Alternative method: https://forums.raspberrypi.com/viewtopic.php?t=352958
- Unplug the power supply from the Rapsberry Pi, and remove the SD card.
- Mount the micro SD card in you computer.
- Edit the
/etc/wpa_supplicant.conf
file, and add your wireless network. - We recommend also adding your mobile hotspot network to this file, in case your network blocks
nmap
, or does not support mDNS/zeroconf. - A
wpa_supplicant.conf
file can be created in the/boot
partition, and may also work. - Safely unmount and eject the micro SD card. Then insert it into the Raspberry Pi, and plug it to the power supply.
Check port usage¶
This can be handy to see what apps are using which ports:
sudo lsof -i -P -n | grep LISTEN
Source: https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/