Networking: IP addresses, ports and connections
Your IP address
hostname -I # just the IP(s)
ip addr # all details
ip -br addr # one line per interface
In ip addr, look at wlan0 (Wi-Fi) or eth0 (cable), the line inet 172.16.5.23/16. The number after / is the network size; the address is the part before it. lo with 127.0.0.1 is the Pi talking to itself.
| Interface | What it is |
|---|---|
lo | Loopback: the Pi itself (localhost, 127.0.0.1) |
eth0 | Ethernet cable |
wlan0 | Wi-Fi |
No inet line under wlan0 = not connected to Wi-Fi.
localhost vs the IP address
| Address | Means |
|---|---|
localhost / 127.0.0.1 | This computer. On the Pi it is the Pi; on your laptop it is your laptop. |
172.16.x.x | The Pi on the lab network. Use this from other devices. |
So http://localhost:5000 works in the Pi's Chromium but not on your laptop. From the laptop use http://172.16.x.x:5000.
Ports
One IP address, many services. Each service listens on a port:
| Port | Service |
|---|---|
| 22 | SSH |
| 80 | Apache (http) |
| 3306 | MariaDB |
| 5000 | Flask |
| 5900 | VNC |
See what is listening and on which address:
sudo ss -tlnp
0.0.0.0:80 or *:80 = reachable from the network. 127.0.0.1:5000 = only from the Pi itself.
Testing a connection step by step
ip route # the gateway is the address after "default via"
ping -c 3 GATEWAY-IP # 1. can I reach the router (use the address from ip route)?
ping -c 3 8.8.8.8 # 2. can I reach the internet?
ping -c 3 google.com # 3. do names work (DNS)?
curl -I http://localhost # 4. does my own web server answer?
| 1 | 2 | 3 | Problem |
|---|---|---|---|
| fails | Not on the network: Wi-Fi off, wrong network, cable | ||
| works | fails | Network has no internet, or blocks it | |
| works | works | fails | DNS problem |
ip route shows the gateway (the line starting default via). cat /etc/resolv.conf shows the DNS server.
The lab network may block ping to the internet while web traffic still works. If apt update works, the internet is fine.
Wi-Fi from the terminal
Raspberry Pi OS Bookworm uses NetworkManager:
nmcli device status # interfaces and connections
nmcli device wifi list # networks in range
sudo nmcli device wifi connect "SSID" password "PASSWORD"
nmcli connection show # saved networks
sudo nmcli connection delete "SSID" # forget one
Or use the network icon on the desktop, or sudo nmtui (a text menu).
Hostname
hostname # show it
sudo raspi-config # System Options → Hostname (the safe way)
If you change it by hand, change it in both /etc/hostname and the 127.0.1.1 line of /etc/hosts. Otherwise every sudo prints unable to resolve host.
In a lab where every card was cloned from one image, every Pi is called raspberrypi, so raspberrypi.local is ambiguous. Use IP addresses, or give your Pi a unique hostname.
Common problems
| Problem | Check |
|---|---|
| IP changed since last lab | Normal. hostname -I every lab. |
| Laptop cannot open the Pi's page | Same network? Using the IP, not localhost? Is the service listening on 0.0.0.0 (ss -tlnp)? |
Network is unreachable | Not connected: nmcli device status. |
Temporary failure in name resolution | DNS: test ping 8.8.8.8 vs ping google.com. |
| Wi-Fi keeps dropping | Weak signal or power problems: check vcgencmd get_throttled. |