Skip to main content

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.

InterfaceWhat it is
loLoopback: the Pi itself (localhost, 127.0.0.1)
eth0Ethernet cable
wlan0Wi-Fi

No inet line under wlan0 = not connected to Wi-Fi.

localhost vs the IP address​

AddressMeans
localhost / 127.0.0.1This computer. On the Pi it is the Pi; on your laptop it is your laptop.
172.16.x.xThe 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:

PortService
22SSH
80Apache (http)
3306MariaDB
5000Flask
5900VNC

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?
123Problem
failsNot on the network: Wi-Fi off, wrong network, cable
worksfailsNetwork has no internet, or blocks it
worksworksfailsDNS 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​

ProblemCheck
IP changed since last labNormal. hostname -I every lab.
Laptop cannot open the Pi's pageSame network? Using the IP, not localhost? Is the service listening on 0.0.0.0 (ss -tlnp)?
Network is unreachableNot connected: nmcli device status.
Temporary failure in name resolutionDNS: test ping 8.8.8.8 vs ping google.com.
Wi-Fi keeps droppingWeak signal or power problems: check vcgencmd get_throttled.