Raspberry PI OS networking

Per default DHCP is configured. However if the raspberry works as server and has to be reached via the network a static address is desired.

Networking with Debian 12 and newer

Debian 12 Bookworm and Debian 13 Trixie use Networkmanager to configure.

sudo ip a to see what is active

sudo nmtui to get the text user interface of Networkmanager. Edit a connection, select IP4 to manual and enter a manual IP4 address

Address 192.168.1.<n>/24

Gateway 192.168.1.1

DNS 8.8.8.8

disable IP6 and save it and sudo systemctl restart NetworkManager to restart it

sudo ip a to see what happened, if connected with ssh then the address remains an a sudo reboot is required

Advanced setup using nmcli

nmtui is limited, for more settings use nmcli

nmcli connection show

In the following use case the raspberry is connected via eth0 to mobile router having low band width access to the Internet but therefore physically portable and cheap. However when moved to the office for development low bandwidth is an obstacle. Turning on to fast wlan0 would be a booster but the Raspberry would still use eth0 since it has a lower metric value. It would however switch to wlan0 when the eth0 cable gets removed. nmtui can not do this but nmcli can!

To give priority to wlan0 its metric must have a lower value.

nmcli connection show to see the names of the connections

sudo nmcli connection modify "<wlan SSID>" ipv4.route-metric 50 set a low metric and therefore high priority

sudo nmcli connection modify "Wired connection 1" ipv4.route -metric 200 set a high metric and therefore low priority

sudo nmcli connection up "<wlan SSID>" takes the modification

sudo nmcli connection up "Wired connection 1" takes the modification

To see the result: ip route show

default via 192.168.1.1 dev wlan0 proto dhcp src 192.168.1.161 metric 50 default via 192.168.20.1 dev eth0 proto dhcp src 192.168.20.5 metric 200

curl -s https://ifconfig.me will show the wan Internet address of the wlan0 router and uses high the speed connection.

sudo ping -c3 192.168.20.1 will still find the eth0 router

Observe the files in /etc/NetworkManager/system-connections, they contain now the new metric values.

Disable wlan0 and curl -s https://ifconfig.me will show the wan Internet address of the eth0 router and use the low speed mobile connection.

Networking with Debian 11

For Debian 11 Bullseye to have a static address edit sudo nano /etc/dhcpcd.conf or sudo mc

Important

Give a static address if the raspberry works as server and will get accessed over the network. It is also possible to turn on networkmanager instead of dhcpcd and then the steps as for Debian 12 can be done.

Acting as access point

Connecting a Raspberry to a local Ethernet might not always be possible or allowed.

Accessing it by devices as Smart phones and Laptops can be done when the Raspberry acts as WLAN access point.

Note

If the Raspberry is connected to the Internet as thorough eth0 then the WLAN devices can make use of it. However be aware that nftable or wireguard setups can prevent this or must be adapted.

Raspberry Pi OS (Trixie) come with NetworkManager that allows such a setup.

/etc/NetworkManager/system-connections/ is the directory where NetworkManager store setups as profiles.

sudo nmcli con add type wifi ifname wlan0 mode ap con-name Hotspot ssid RaspberryAP

this creates the file /etc/NetworkManager/system-connections/Hotspot.nmconnection

sudo nmcli con modify Hotspot wifi-sec.key-mgmt wpa-psk

sudo nmcli con modify Hotspot wifi-sec.psk "<Password>"

Adds the password to this file

sudo nmcli con modify Hotspot ipv4.method shared ipv4.addresses 192.168.4.1/24

Creates a DHCP server in the raspberry to get IP addresses for the attached devices

sudo nmcli con modify Hotspot connection.interface-name wlan0 tells to use wlan0 for it

Check it wlan0 appears on nmcli device status if not, turn make it available sudo nmcli radio wifi on

sudo nmcli con up Hotspot starts the access point

sudo nmcli con down Hotspot stops it

nmcli device status shows the status

The raspberry can now be accessed using its WLAN SSID and Password under 192.168.4.1

sudo nmcli con modify Hotspot connection.autoconnect yes

sudo nmcli con modify Hotspot connection.autoconnect-priority 10

Will make that it starts automatically next boot

sudo nmcli con modify Hotspot connection.autoconnect no will prevent it from stopping automatically

sudo cat /var/lib/NetworkManager/dnsmasq-wlan0.leases or ip neighbor show dev wlan0 will show who is attached to the AP

Changing Hostname

Having meaningful hostnames are vital in a network. There are many ways to change the hostname, but after reboot and Raspberry PI OS Trixie the old hostname comes back. The reason is that after boot it takes the hostname from /boot/firmware/user-data

hosts file

The /etc/hosts file gets recreated every boot by the cloud-init service. To turn this off read the comments in the re-written /etc/hosts file or create the file: /etc/cloud/cloud.cfg.d/99_custom_hosts.cfg with:

manage_etc_hosts: false

Linurs startpage