Skip to content

IP addresses and ports: ip and ss

AdvancedLesson 45 of 669 min

Find your IP address and gateway with ip, start a small web server, then see which programs listen on which ports with ss, on your laptop and on a server.

Every machine on a network has an IP address, like a street address, and every program that accepts connections listens on a port, like a door number. "The website" is really "the program on port 80 at 10.0.0.10". The machines here (your laptop, web, db and the router) are pretend ones inside your browser, so nothing you type reaches a real network.

Your address

ip addr
ip -br a

lo is the loopback: 127.0.0.1, which always means this machine itself. eth0 is the network card, with your address on the network, 10.0.0.5/24. The /24 says the first three numbers are the network's part: every machine from 10.0.0.1 to 10.0.0.254 is a neighbour.

ip route

The default route is where everything else goes: the router, 10.0.0.1, which passes it on to the internet. hostname -I prints just your addresses.

Ports

Some ports are known by heart: 22 ssh, 53 DNS, 80 http, 443 https, 5432 PostgreSQL. Ports below 1024 need root to listen on.

ss lists the sockets: the open doors. -t TCP, -l listening, -n numbers instead of names, -p the program (other users' programs only with sudo):

ss -tln

0.0.0.0 or 127.0.0.1

The address a program listens on matters. 0.0.0.0:80 accepts connections from anywhere. 127.0.0.1:3000 accepts them only from the same machine, which is how you keep an app private behind a web server.

To see a port open up, start a little web server in the background with &. Python's http.server serves the folder you're in:

cd site && python3 -m http.server 8000 &

Then ss -tln shows it, and curl localhost:8000 gets the page. kill %1 stops it.

Your turn

(1) Find your laptop’s IP address and its default gateway. (2) Serve the site folder on port 8000 in the background and find it in ss. (3) On web, find which program listens on port 3000, then (4) try curl http://web:3000/ from your laptop and see that it can’t connect.

Next