2 Linux and networking
Linux separates user identity from file ownership and process privileges. root can administer the system. A service should normally run under a dedicated account with only the directories and network capabilities it needs.
Outcome: operate a Linux service and trace a request through DNS, TCP, TLS, and HTTP. Prerequisites: Chapter 1. Environment: a disposable Ubuntu VM, with console access available before changing SSH or firewall rules. Commands here are recipes for that VM; the handbook build does not modify your computer's network configuration.
Users permissions and processes
Linux separates user identity from file ownership and process privileges. root can administer the system. A service should normally run under a dedicated account with only the directories and network capabilities it needs. Mode 600 gives the owner read/write access; 640 adds group read; directory execute permission allows traversal. Permissions on parent directories also matter.
Inspect before changing:
free is a Linux command. df -h reports storage capacity; df -i reports inode availability. A disk can have free bytes and still be unable to create files because inodes are exhausted. Cached memory is not automatically a memory leak. Compare trends and process behavior before making changes.
Use systemctl status SERVICE to inspect a managed service and journalctl -u SERVICE --since '15 minutes ago' to read its recent logs. Look for the first failure, not just the final restart message. Confirm which account the service runs as and where its working directory points.
Remote access without locking yourself out
Generate a separate SSH key for the lab on your laptop, protect it with a passphrase, and register its public half during VM creation. Verify the host fingerprint against the provider console or another trusted channel. A host-key warning may indicate an expected rebuild or a security problem; investigate rather than blindly deleting the remembered key.
Keep the first working SSH session open while testing a second. Verify key login, administrator access, and console recovery before disabling password login or changing access rules. Validate configuration with sudo sshd -t; inspect the effective settings rather than assuming one file overrides all included files. Ubuntu documents its configuration layout and validation procedure in [OpenSSH server](https://ubuntu.com/server/docs/how-to/security/openssh-server/).
For patching, list available updates, read any required reboot notice, schedule interruption, verify backups, apply the update, then verify service behavior. A reboot is an operational event; never treat it as a generic debugging command on a shared machine.
Addresses ports and routing
127.0.0.1 is IPv4 loopback: only the local host can use it. Binding to 0.0.0.0 means all IPv4 interfaces, subject to network controls. It is a listening address, not the address you give a remote user. IPv6 has separate addresses and exposure rules. A hostname can resolve to both A and AAAA records; a stale AAAA record can make some clients fail while others succeed.
A subnet groups addresses under a prefix. A route chooses where packets go next. NAT translates addresses, often at the boundary between a private network and the internet. A firewall filters traffic; a reverse proxy accepts a client connection and forwards an application request. These solve different problems.
Remote Linux inspection:
If the process is listening only on loopback, opening a cloud firewall will not make it reachable directly. If it is listening publicly, a closed firewall may still prevent access. Diagnose both layers.
Follow one HTTPS request
- The client resolves a hostname through DNS.
- It connects to an address and port, typically TCP 443.
- TLS negotiates encryption and validates the certificate for the hostname.
- HTTP carries a method, path, headers, and optional body.
- The proxy selects an upstream application.
- The application authenticates the caller, validates the request, and performs authorized work.
- A response returns through the same chain.
DNS does not issue TLS certificates. TLS encrypts traffic and authenticates the endpoint; it does not grant application permissions. A 200 response proves a request succeeded according to that endpoint, not that every dependency or workflow is healthy.
HTTP you must understand
GET retrieves a representation and should not create business side effects. POST commonly creates work. A 202 response means accepted, not completed. A 400/422 response signals an input problem; 401 signals missing or invalid authentication; 403 means denied permission; 404 can intentionally conceal an inaccessible object; 409 reports a conflict; 429 indicates throttling; 5xx indicates a server-side problem.
Headers carry metadata. Content-Type describes a body representation. Authorization carries credentials and must not be logged. A request ID helps correlate events. A timeout should bound connection establishment and response waiting. Retrying a POST is safe only when the operation's contract makes it safe, for example through a durable idempotency key.
CORS is a browser policy controlling which origins may read certain responses. It is not API authentication and does not prevent a command-line client from calling your server. WebSocket supports ongoing bidirectional communication; SSE supports a server-to-client event stream. Both need reconnection, timeout, and backpressure decisions.
Diagnose in layers
| Symptom | First evidence | Likely layer |
|---|---|---|
| Hostname does not resolve | DNS lookup, authoritative records | DNS |
| Connection refused | Listener and destination port | Process or binding |
| Connection times out | Routes, security groups, firewall, address | Network reachability |
| Certificate mismatch | Requested hostname and certificate names | TLS or DNS |
| 502 response | Proxy upstream address and application logs | Reverse proxy or application |
| 503 readiness response | Database connection and schema | Dependency availability |
| 401/403 | Credential validity and permission policy | Identity/access |
| Requests hang under load | Thread/connection pools, queue depth, latency | Capacity or blocking work |
Use curl -v only with synthetic or unauthenticated requests when sharing output; verbose clients can expose headers. Check the same endpoint from the server and from a separate client. “It works on localhost” narrows the problem; it does not prove an internet deployment works.
Firewall exercise
Draw the allowed flows before adding rules: administrator → SSH; internet → 80/443; proxy → local API; API/worker → database. Keep the database off the public internet. Restrict administration by trusted address or private access where feasible. Preserve recovery access and test from a second session before closing the first.
Docker publishes ports through its own networking rules; host firewall assumptions need verification. The reference Compose file binds the API to 127.0.0.1:18768 and publishes no database port. Inspect the resulting exposure rather than assuming a firewall wrapper handles every container rule. See [Docker packet filtering](https://docs.docker.com/engine/network/packet-filtering-firewalls/).
Exit exercise
Run the local reference API, identify its listener, and retrieve /health/live. Stop only your lab API process and compare connection refusal with an application-generated 404. In a VM, explain an intentionally incorrect DNS entry or proxy target, correct it, and record the evidence. You pass when you can name the failing layer before changing configuration.
Lock in what you just learned (3 quick questions)
curl http://127.0.0.1:8000/health immediately returns "Connection refused", what happened at the TCP layer?