Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Networking and Servers

Network Setup: Wi-Fi, Ethernet and Static IPs

Connect a Raspberry Pi to Wi-Fi or Ethernet, find its IP address, set a static address with NetworkManager, and understand subnets, gateways, DNS and mDNS names.

A Pi on a network is a tiny server you can reach from your laptop. Most projects (a dashboard, an SSH session, a sensor that publishes data) depend on this. The trouble is that a Pi without a screen is invisible until you know how to find it, and an address that changes every day breaks every bookmark and script.

This lesson covers the layers you need: the interface, the IP address, the gateway, DNS and the friendly .local name. Commands run on a real Pi, so they are read-only here; runnable blocks use Python's standard ipaddress module to practice the arithmetic.

Interfaces and addresses

Linux names network cards by role. eth0 (or an enx... name) is wired Ethernet, wlan0 is Wi-Fi, lo is the loopback to yourself.

Terminal
ip -br addr            # one line per interface with its IP
ip route               # default gateway and local subnets
hostname -I            # all IPv4/IPv6 addresses of this Pi
nmcli device status    # NetworkManager's view
iwconfig wlan0         # Wi-Fi signal (or: iw dev wlan0 link)

A typical output line reads wlan0 UP 192.168.1.42/24. The /24 is the prefix length: the first 24 bits are the network part, so this Pi lives in the subnet 192.168.1.0/24 and its neighbours are 192.168.1.1 to 192.168.1.254.

NetworkManager on current Pi OS

Since Bookworm, Raspberry Pi OS uses NetworkManager. Older releases used dhcpcd and wpa_supplicant.conf; if a tutorial mentions those files, it is for an older image. Connect to Wi-Fi from the command line, with no desktop:

Terminal
sudo nmcli radio wifi on
nmcli device wifi list                         # visible networks
sudo nmcli device wifi connect "HomeNet" password "correct-horse"
nmcli connection show                          # saved profiles
sudo nmcli connection down  "HomeNet"
sudo nmcli connection up    "HomeNet"

You can also preset Wi-Fi and a hostname in Raspberry Pi Imager (the settings gear) before the first boot, which is the easiest route for a headless Pi. Set the Wi-Fi country too: it selects the legal channels, and without it the radio may stay blocked (rfkill list shows that).

DHCP, leases and why addresses change

By default the Pi asks the router for an address with DHCP. The router lends one for a while (a lease) and the Pi may get a different one later. There are two ways to make it stable:

  1. DHCP reservation in the router: it always hands the same address to the Pi's MAC address. This is the cleaner choice because the Pi itself stays on automatic settings.
  2. Static address configured on the Pi: faster to type, but you must pick an address outside the router's DHCP range or you may clash with another device.
Terminal
sudo nmcli connection modify "HomeNet" \
  ipv4.method manual \
  ipv4.addresses 192.168.1.50/24 \
  ipv4.gateway 192.168.1.1 \
  ipv4.dns "1.1.1.1 9.9.9.9"
sudo nmcli connection up "HomeNet"

Three numbers must be right: the address with its prefix, the gateway (your router, where traffic to other networks goes) and DNS servers (which turn names into addresses). A wrong gateway means local ping works but the internet does not; wrong DNS means pinging 8.8.8.8 works but example.com fails.

Checking subnet math in Python

The standard library can answer "is this address in my subnet?" Use it to validate a static address before applying it:

Python Check a static address with the ipaddress module

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Names: hostname and mDNS

Remembering 192.168.1.50 is not needed. Every Pi announces its hostname with mDNS (Avahi), so raspberrypi.local or whatever hostname you chose works on most home networks. Pick a descriptive name like greenhouse, then reach it with ssh [email protected].

Terminal
hostnamectl set-hostname greenhouse    # change the name (then reboot)
ping greenhouse.local
avahi-browse -art | head               # who announces services

mDNS is limited to the local network and sometimes disabled on corporate or guest Wi-Fi. When it fails, use the IP address from the router's device list.

Finding a headless Pi

flowchart TD
    A["Pi is on, no screen"] --> B{"Router device list shows it?"}
    B -- yes --> C["Use that IP"]
    B -- no --> D{"ping hostname.local works?"}
    D -- yes --> E["Use the .local name"]
    D -- no --> F["Scan the subnet or use Ethernet"]

Look at the router first, then try mDNS, then scan.

A scan with nmap -sn 192.168.1.0/24 lists live hosts. The next stand-in imitates a scan result: filter devices by the vendor prefix of the MAC address (Raspberry Pi Ltd uses prefixes such as b8:27:eb, dc:a6:32, e4:5f:01 and d8:3a:dd).

Python Stand-in: spot Pis in a device list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Troubleshooting order

  1. Is the interface up (ip -br addr)? No address means DHCP failed or Wi-Fi did not connect.
  2. Can you ping the gateway (ping -c 3 192.168.1.1)? If not, the local link is the problem.
  3. Can you ping 1.1.1.1? If not, the router has no internet.
  4. Can you ping example.com? If not, check DNS.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tool manages Wi-Fi from the command line on Raspberry Pi OS Bookworm?

    Bookworm switched to NetworkManager, controlled with nmcli.
  2. 2. You can ping 8.8.8.8 but not example.com. What is likely wrong?

    Reaching an address but not a name points at name resolution.
  3. 3. In 192.168.1.42/24, what does /24 mean?

    The prefix length counts the network bits; /24 is netmask 255.255.255.0.
  4. 4. Why is a DHCP reservation often better than a static IP on the Pi?

    The router keeps one list of addresses, so two devices cannot be given the same one.

Summary

  • ip -br addr, ip route and nmcli show and change network settings.
  • Current Pi OS uses NetworkManager; set Wi-Fi country and credentials in Imager for headless setups.
  • A stable address comes from a DHCP reservation or a static address with correct gateway and DNS.
  • hostname.local (mDNS) saves you from remembering IPs.
  • Troubleshoot in order: interface, gateway, internet by IP, DNS.

Next: SSH, VNC and Remote Access