Networking, Ports & curl
Monitoring says the shop's new release can't start because "the port is already in use", and the developers also want an easy name for the internal site. You will map out what is listening on the machine, check each service with curl, add a host name and free the port so the release can start.
- Read network interfaces and routes with ip, and list listening ports with ss and lsof
- Test HTTP services with curl: headers, status codes, saving bodies and timing
- Explain how a name becomes an IP address and add an entry to /etc/hosts
- Find and stop the process holding a port, then start a service on it
How programs talk over a network. Every machine has one or more network interfaces, each with an IP address. A program that offers a service (a web server, a database) listens on a port, a number from 1 to 65535. To reach it, a client needs the IP address and the port, for example 127.0.0.1:8080.
| Idea | Example | Remember |
|---|---|---|
| IP address | 127.0.0.1 | 127.0.0.1 (localhost) always means "this machine" |
| Port | 80, 443, 22, 8080 | Only one program can listen on a given IP and port |
| Host name | shop.internal | Turned into an IP by /etc/hosts or DNS |
| Protocol | HTTP over TCP | curl speaks HTTP, nc just opens TCP connections |
Well-known ports worth memorising for interviews: 22 SSH, 53 DNS, 80 HTTP, 443 HTTPS, 3306 MySQL, 5432 PostgreSQL, 6379 Redis, 6443 Kubernetes API.
This lab machine has no internet access, so everything happens on localhost, but every command works the same way against a remote server.
- Press Start lab. You get your own Ubuntu machine on the right, just for you.
- Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
- Do the Your task part, then press Check my work. The check looks at the machine and tells you what is right or still missing.
- Stuck? Open the hint. Nothing you do here can break anything real.
Interfaces and listening ports
Find which port the unknown debug-server process is listening on.
Why we do this: before debugging any connection problem we answer two questions: what addresses does this machine have, and what is listening on which port. Half of all "it doesn't connect" tickets end right here.
1. Network interfaces and addresses
ip addr
ip -br addrip addr lists every interface with its addresses. -br (brief) prints one line per interface. This lab machine is isolated, so you will only see lo, the loopback interface with 127.0.0.1. On a real server you would also see something like eth0 or ens5 with its private IP.
2. Routes
ip routeThe routing table says where packets go. On a normal server the default via ... line is the gateway to the rest of the network. Here it is empty, which is exactly why this machine can't reach the internet.
ifconfigandnetstatare the old tools. They still appear in interviews, butipandssreplace them on modern Linux.
3. Which ports are listening?
ss -tlnp| Flag | Meaning |
|---|---|
-t | TCP sockets |
-l | only listening sockets |
-n | show numbers (8080) instead of names (http-alt) |
-p | show the process that owns each socket |
Read the Local Address:Port column. 0.0.0.0:8080 means "port 8080 on every interface", while 127.0.0.1:8081 means "only reachable from this machine". That difference matters: a database bound to 127.0.0.1 can't be reached from other servers.
4. The same view from lsof
lsof -i -P -n
lsof -i :8080lsof lists open files, and network sockets count as files. lsof -i :PORT answers "who is using this port?".
Three services are running. One of them, called debug-server, nobody remembers starting. Find the port it listens on and write just the number into /root/port.txt.
Expected result: cat /root/port.txt prints the debug-server's port.
Hint: the commands, explained
Look for debug-server in the last column of ss -tlnp, or run lsof -i -P -n | grep debug. Then for example: echo 1234 > /root/port.txt
What each part does
ss -tlnp: list sockets: tCP only, listening only, numeric ports (no names), and the process using each. The last column shows the program name and PID.lsof -i -P -n: list open network connections with numeric ports and addresses;grep debugkeeps the debug-server line.
Talk to services with curl
Save the API's health response and record the status code of its admin page.
Why we do this: curl is the DevOps engineer's Swiss army knife. Health checks, testing an API after a deploy, checking a load balancer, debugging TLS: they all start with a curl command, and you will use it in CI pipelines and Kubernetes probes too.
1. A simple request
curl http://localhost:8080curl prints the response body: the HTML of the shop's site.
2. See the headers too
curl -i http://localhost:8081/health
curl -I http://localhost:8080-i includes the response headers before the body. -I sends a HEAD request and shows only the headers. The first line holds the status code:
| Code | Meaning |
|---|---|
200 | OK |
301 / 302 | Redirect (add -L to follow it) |
401 / 403 | Not logged in / not allowed |
404 | Not found |
500 / 502 / 503 | Server error / bad gateway / service unavailable |
3. Watch the whole conversation
curl -v http://localhost:8081/health-v (verbose) shows the connection, the request curl sends (lines starting with >) and the response (lines starting with <). This is the first thing to try when a request behaves strangely.
4. Options you will use in scripts
curl -s http://localhost:8081/health | jq .
curl -s -o /tmp/page.html http://localhost:8080
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8081/nothing
curl -s -o /dev/null -w 'total: %{time_total}s\n' http://localhost:8080| Option | Meaning |
|---|---|
-s | silent: no progress bar |
-o FILE | save the body to a file (/dev/null throws it away) |
-w FORMAT | print details such as the status code or the time taken |
-f | fail with a non-zero exit code on HTTP errors, handy in CI |
5. Is the port open at all?
nc -zv localhost 8081
nc -zv localhost 5000nc -z only checks whether a TCP connection succeeds. "Connection refused" means nothing listens there. A timeout usually means a firewall is dropping packets.
- Save the body returned by
http://localhost:8081/healthinto/root/health.json. - Write only the HTTP status code of
http://localhost:8081/admininto/root/admin-status.txt.
Expected result: jq .status /root/health.json prints "ok", and /root/admin-status.txt holds a three-digit code.
Hint: the commands, explained
curl -s -o /root/health.json http://localhost:8081/health
curl -s -o /dev/null -w '%{http_code}' http://localhost:8081/admin > /root/admin-status.txtWhat each part does
curl -s: fetch a URL silently (no progress bar).-o FILEsaves the response body to a file instead of printing it.-o /dev/null: throw the body away.-w '%{http_code}': after the request, write only the HTTP status code (like403).> /root/admin-status.txtsaves that code.
Names and /etc/hosts
Make shop.internal resolve to this machine so the site opens by name.
Why we do this: people and configs use names, but connections need IP addresses. When a name resolves to the wrong address, or not at all, everything breaks in confusing ways. Knowing the order in which Linux looks names up saves hours.
1. How Linux resolves a name
When a program asks for shop.internal, Linux checks the sources listed on the hosts: line of /etc/nsswitch.conf:
grep hosts /etc/nsswitch.conffiles means /etc/hosts is checked first, then dns means the DNS servers listed in /etc/resolv.conf. That is why an entry in /etc/hosts overrides DNS, which is handy for testing a new server before the real DNS record exists.
cat /etc/hosts
cat /etc/resolv.conf2. Look a name up
getent hosts localhost
getent hosts shop.internalgetent hosts resolves a name exactly the way programs do, /etc/hosts included. The second command prints nothing because the name doesn't exist yet.
digandnslookup(also installed) ask DNS servers directly and skip/etc/hosts. Use them to debug DNS records, andgetentto see what applications will actually get.
3. Add your own name
/etc/hosts has one line per address: the IP, then one or more names.
127.0.0.1 shop.internalAdd the line with an editor (nano /etc/hosts) or append it:
echo "127.0.0.1 shop.internal" >> /etc/hostsBe careful to use >> (append). A single > would wipe the file.
Make the name shop.internal resolve to 127.0.0.1, then open the shop's site by name:
curl http://shop.internal:8080Expected result: getent hosts shop.internal prints 127.0.0.1 and the curl command returns the shop's HTML page.
Hint: the commands, explained
echo "127.0.0.1 shop.internal" >> /etc/hosts
getent hosts shop.internalWhat each part does
echo "127.0.0.1 shop.internal" >> /etc/hosts: append a line mapping the nameshop.internalto this machine's own address./etc/hostsis checked before DNS.getent hosts shop.internal: look the name up the same way programs do. It prints127.0.0.1 shop.internal.
Fix "address already in use"
Free port 9999 and start the new release on it.
Why we do this: "bind: address already in use" is one of the most common reasons a service or container fails to start. The fix is always the same: find who holds the port, decide whether it should, then stop it or pick another port.
1. See the error for yourself
The release notes say:
cat /root/README-release.txtTry to start release 2.0:
python3 -m http.server 9999 --directory /opt/shop/releaseIt fails at once with OSError: [Errno 98] Address already in use. Only one program can listen on a port.
2. Find the owner of the port
ss -tlnp 'sport = :9999'
lsof -i :9999
fuser 9999/tcpAll three give you the PID of the process holding the port. fuser prints just the PID, which is easy to use in scripts.
3. Stop it
You found in step 1 that this is the forgotten debug-server. Stop it gracefully:
kill <PID>Check that the port is free: ss -tln | grep 9999 should now print nothing.
In real life, check what the process is before killing it. If it is a legitimate service, change your app's port instead. In containers the same error usually means another container already publishes that host port (
docker psshows it).
4. Start the release in the background
nohup python3 -m http.server 9999 --directory /opt/shop/release > /tmp/release.log 2>&1 &Then test it:
curl http://localhost:9999Stop the debug-server and start release 2.0 on port 9999, serving /opt/shop/release, in the background.
Expected result: curl http://localhost:9999 returns "Shop release 2.0" and pgrep -f debug-server prints nothing.
Hint: the commands, explained
kill $(fuser 9999/tcp 2>/dev/null)
nohup python3 -m http.server 9999 --directory /opt/shop/release > /tmp/release.log 2>&1 &What each part does
fuser 9999/tcp: print the PID of whatever is listening on TCP port 9999;kill $( )stops it, freeing the port.2>/dev/nullhides fuser's extra text.nohup ... &: start a program in the background so it keeps running after you close the terminal.python3 -m http.server 9999 --directory /opt/shop/release: a simple web server on port 9999 serving that folder.> /tmp/release.log 2>&1: its output and errors go to a log file.
Brilliant work!
You mapped every service on the machine, tested them like a monitoring system would, gave the site a name and fixed a real "address already in use" failure.
Interview-ready takeaways:
ip -br addrandip routeshow addresses and the default gateway.ss -tlnp(orlsof -i :PORT) shows who listens where.0.0.0.0:PORTaccepts connections on every interface,127.0.0.1:PORTonly from the same machine.curl -i,-v,-o,-w '%{http_code}'and-fcover almost every API and health-check test.nc -zv host portchecks whether a port is open at all.- Names resolve through
/etc/nsswitch.conf:/etc/hostsfirst, then DNS from/etc/resolv.conf.getent hostsshows what applications see,digasks DNS directly. - "Address already in use": find the PID with
ss -tlnp,lsoforfuser, then stop it or change your port.
Next: the Archives, Compression & Backups lab.
See which services listen on which ports, talk to them with curl, make names resolve with /etc/hosts and fix the classic "port already in use" error.
Monitoring says the shop's new release can't start because "the port is already in use", and the developers also want an easy name for the internal site. You will map out what is listening on the machine, check each service with curl, add a host name and free the port so the release can start.