Networking, Ports & curl Beginner 35 min
Lab overview

Networking, Ports & curl

35 min 4 checked steps Beginner
The situation

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.

By the end of this lab you will be able to
  • 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.

IdeaExampleRemember
IP address127.0.0.1127.0.0.1 (localhost) always means "this machine"
Port80, 443, 22, 8080Only one program can listen on a given IP and port
Host nameshop.internalTurned into an IP by /etc/hosts or DNS
ProtocolHTTP over TCPcurl 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.

The steps
1
Interfaces and listening portsFind which port the unknown debug-server process is listening on.
2
Talk to services with curlSave the API's health response and record the status code of its admin page.
3
Names and /etc/hostsMake shop.internal resolve to this machine so the site opens by name.
4
Fix "address already in use"Free port 9999 and start the new release on it.
How this lab works
  1. Press Start lab. You get your own Ubuntu machine on the right, just for you.
  2. Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
  3. 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.
  4. Stuck? Open the hint. Nothing you do here can break anything real.
Step 1 of 4

Interfaces and listening ports

Objective

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

bash
ip addr
ip -br addr

ip 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

bash
ip route

The 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.

ifconfig and netstat are the old tools. They still appear in interviews, but ip and ss replace them on modern Linux.

3. Which ports are listening?

bash
ss -tlnp
FlagMeaning
-tTCP sockets
-lonly listening sockets
-nshow numbers (8080) instead of names (http-alt)
-pshow 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

bash
lsof -i -P -n
lsof -i :8080

lsof 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 debug keeps the debug-server line.
Step 2 of 4

Talk to services with curl

Objective

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

bash
curl http://localhost:8080

curl prints the response body: the HTML of the shop's site.

2. See the headers too

bash
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:

CodeMeaning
200OK
301 / 302Redirect (add -L to follow it)
401 / 403Not logged in / not allowed
404Not found
500 / 502 / 503Server error / bad gateway / service unavailable

3. Watch the whole conversation

bash
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

bash
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
OptionMeaning
-ssilent: no progress bar
-o FILEsave the body to a file (/dev/null throws it away)
-w FORMATprint details such as the status code or the time taken
-ffail with a non-zero exit code on HTTP errors, handy in CI

5. Is the port open at all?

bash
nc -zv localhost 8081
nc -zv localhost 5000

nc -z only checks whether a TCP connection succeeds. "Connection refused" means nothing listens there. A timeout usually means a firewall is dropping packets.

  1. Save the body returned by http://localhost:8081/health into /root/health.json.
  2. Write only the HTTP status code of http://localhost:8081/admin into /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
text
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.txt

What each part does

  • curl -s: fetch a URL silently (no progress bar). -o FILE saves 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 (like 403).
  • > /root/admin-status.txt saves that code.
Step 3 of 4

Names and /etc/hosts

Objective

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:

bash
grep hosts /etc/nsswitch.conf

files 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.

bash
cat /etc/hosts
cat /etc/resolv.conf

2. Look a name up

bash
getent hosts localhost
getent hosts shop.internal

getent hosts resolves a name exactly the way programs do, /etc/hosts included. The second command prints nothing because the name doesn't exist yet.

dig and nslookup (also installed) ask DNS servers directly and skip /etc/hosts. Use them to debug DNS records, and getent to 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.

text
127.0.0.1   shop.internal

Add the line with an editor (nano /etc/hosts) or append it:

bash
echo "127.0.0.1   shop.internal" >> /etc/hosts

Be 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:

bash
curl http://shop.internal:8080

Expected result: getent hosts shop.internal prints 127.0.0.1 and the curl command returns the shop's HTML page.

Hint: the commands, explained
text
echo "127.0.0.1 shop.internal" >> /etc/hosts
getent hosts shop.internal

What each part does

  • echo "127.0.0.1 shop.internal" >> /etc/hosts: append a line mapping the name shop.internal to this machine's own address. /etc/hosts is checked before DNS.
  • getent hosts shop.internal: look the name up the same way programs do. It prints 127.0.0.1 shop.internal.
Step 4 of 4

Fix "address already in use"

Objective

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:

bash
cat /root/README-release.txt

Try to start release 2.0:

bash
python3 -m http.server 9999 --directory /opt/shop/release

It 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

bash
ss -tlnp 'sport = :9999'
lsof -i :9999
fuser 9999/tcp

All 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:

bash
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 ps shows it).

4. Start the release in the background

bash
nohup python3 -m http.server 9999 --directory /opt/shop/release > /tmp/release.log 2>&1 &

Then test it:

bash
curl http://localhost:9999

Stop 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
text
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/null hides 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.
Lab complete

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 addr and ip route show addresses and the default gateway. ss -tlnp (or lsof -i :PORT) shows who listens where.
  • 0.0.0.0:PORT accepts connections on every interface, 127.0.0.1:PORT only from the same machine.
  • curl -i, -v, -o, -w '%{http_code}' and -f cover almost every API and health-check test. nc -zv host port checks whether a port is open at all.
  • Names resolve through /etc/nsswitch.conf: /etc/hosts first, then DNS from /etc/resolv.conf. getent hosts shows what applications see, dig asks DNS directly.
  • "Address already in use": find the PID with ss -tlnp, lsof or fuser, then stop it or change your port.

Next: the Archives, Compression & Backups lab.

root@devopsshack offline

Your Linux machine is ready to launch

Starting gives you a private Ubuntu server for up to 45 minutes. It is deleted when you finish.

Feedback