IP addresses and ports: ip and ss
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 addrip -br alo 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 routeThe 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 -tln0.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.
Your task
(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.
- Your address (10.0.0.5) and the gateway (10.0.0.1) (not done yet)
- A web server on port 8000, seen by ss (not done yet)
- On web, the program on port 3000 (node) (not done yet)
- From the laptop, web’s port 3000 is closed (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
ip- Show network addresses and routes More on ip
ss- Show network sockets: who is listening on which port More on ss
hostname- Show the machine’s name or address
Every everyday command on one page: the Linux commands cheat sheet.