Networking Basics
The Layered Model
Networks are built in layers, each relying on the one below. When something fails, the layers give you an order in which to look: there is no point debugging HTTP headers if packets never reach the host.
TCP/IP Layers
| Layer | Responsibility | Examples | Check with |
|---|---|---|---|
| Link | Frames between directly connected devices. | Ethernet, Wi-Fi, MAC addresses | ip link |
| Internet | Addressing and routing packets between networks. | IPv4, IPv6, ICMP | ip addr, ping, traceroute |
| Transport | Connections and ports between processes. | TCP, UDP | ss, nc |
| Application | The protocol the program speaks. | DNS, HTTP, TLS, SSH | dig, curl, openssl |
The seven-layer OSI model splits these further and is common in vendor documentation; “layer 4” (transport) and “layer 7” (application) load balancers take their names from it.
Encapsulation
Each layer wraps the data from the layer above in its own header: an HTTP request becomes the payload of a TCP segment, which becomes the payload of an IP packet, which travels inside an Ethernet frame. The maximum frame payload, the MTU, is typically 1500 bytes on Ethernet; tunnels and VPNs add headers and reduce it, a frequent cause of connections that open but then hang on large transfers.
Addressing and Ports
An IP address identifies a network interface; a port identifies a process on that host. Together they form an endpoint, and a TCP connection is identified by both endpoints.
IP Addresses and CIDR
CIDR notation writes a network as an address and a prefix length: 10.0.20.0/24 means the first 24 bits are the network and the remaining 8 bits number up to 256 addresses (254 usable hosts). Private IPv4 ranges, which are not routed on the public internet, are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. IPv6 addresses are 128 bits written in hexadecimal groups (2001:db8::1), and hosts often have both kinds.
# Interfaces and their addresses, briefly.
ip -brief address
# Which route, interface and source address would be used to reach a host.
ip route get 1.1.1.1
TCP, UDP and Ports
TCP provides an ordered, reliable byte stream, opened with a three-way handshake (SYN, SYN-ACK, ACK). UDP sends independent datagrams with no delivery guarantee and no handshake; DNS queries and most real-time media use it. Well-known ports include 22 (SSH), 53 (DNS), 80 (HTTP), 443 (HTTPS) and 5432 (PostgreSQL).
# Listening TCP and UDP sockets, numeric, with the owning process (needs root for -p).
sudo ss -tulpn
# Is anything listening on 5432, and on which address? 127.0.0.1 means local only.
ss -tln 'sport = :5432'
A service bound to 127.0.0.1 is reachable only from the same host, one bound to 0.0.0.0 or :: on every interface. “Works locally, unreachable remotely” is very often this, or a firewall.
DNS
The Domain Name System translates names into addresses and other records. Because almost every connection starts with a lookup, DNS failures look like everything failing at once — hence the operator's saying, it's always DNS.
How Resolution Works
A client asks a recursive resolver (configured in /etc/resolv.conf or by systemd-resolved). On a cache miss, the resolver asks a root server, then the top-level domain servers, then the domain's authoritative servers, and caches the answer for the record's TTL. Common record types are A (IPv4), AAAA (IPv6), CNAME (alias), MX (mail), TXT (verification and policy) and NS (delegation).
# Short answer from the configured resolver.
dig +short www.example.org A
# Full answer including TTL, then ask a specific public resolver for comparison.
dig www.example.org
dig @1.1.1.1 www.example.org
# Follow the delegation chain from the root, bypassing caches.
dig +trace www.example.org
DNS in Operations
Lower a record's TTL a day before a planned migration, so clients pick up the new address quickly, and raise it again afterwards. Remember that resolvers and applications cache answers, sometimes ignoring TTLs, and that /etc/hosts entries override DNS on that host — a forgotten entry is a classic source of “only this server is broken”.
HTTP and TLS
Most service traffic is HTTP, almost always wrapped in TLS. Knowing how to inspect both from the command line turns vague reports into precise diagnoses.
Inspect HTTP
An HTTP exchange is a request (method, path, headers, optional body) and a response (status code, headers, body). Status classes tell you who to look at: 2xx success, 3xx redirect, 4xx the client's request was rejected, 5xx the server or something behind it failed — 502 and 504 usually mean a proxy could not get a good answer from the upstream service.
# Verbose request: shows DNS result, TLS handshake, request and response headers.
curl -v https://www.example.org/ -o /dev/null
# Where does the time go? Timings for each phase of one request.
curl -s -o /dev/null -w 'dns %{time_namelookup}s connect %{time_connect}s tls %{time_appconnect}s first byte %{time_starttransfer}s total %{time_total}s\n' https://www.example.org/
# Test a specific backend while keeping the real host name (and certificate check).
curl -v --resolve www.example.org:443:10.0.20.15 https://www.example.org/health
TLS and Certificates
TLS encrypts the connection and authenticates the server with a certificate that a trusted authority signed for the host name. Most TLS incidents are operational: an expired certificate, a missing intermediate certificate in the chain, or a name mismatch. Automate renewal (for example, with ACME and Let's Encrypt), and monitor expiry dates with an alert weeks in advance.
# Show the certificate chain the server presents, with SNI for the host name.
openssl s_client -connect www.example.org:443 -servername www.example.org </dev/null
# Just the subject, issuer and validity dates of the leaf certificate.
openssl s_client -connect www.example.org:443 -servername www.example.org </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates
Routing, NAT and Firewalls
Between two hosts lie routers, address translation and filters. Each can silently drop traffic.
Routes and Gateways
A host sends packets for its own subnet directly and everything else to its default gateway. Routers forward packets hop by hop using their routing tables. traceroute (or mtr, which repeats it continuously and shows loss per hop) reveals the path and where it stops. Note that many routers deprioritize or block the probe replies, so a hop showing loss is only significant if the loss continues to the destination.
ip route # the host's routing table and default gateway
traceroute -n 10.0.30.7 # each hop, numeric (no reverse DNS delays)
mtr -n -c 50 --report 10.0.30.7
NAT and Firewalls
NAT rewrites addresses so many private hosts can share one public address; it is why a server sees a load balancer's address instead of the client's, unless the client address is passed in a header such as X-Forwarded-For. Firewalls — host firewalls (nftables, iptables, firewalld, ufw) and network or cloud security groups — allow or drop traffic by address, port and protocol. A dropped packet causes a timeout; a rejected one, or a closed port, causes an immediate connection refused. That difference alone often tells you which layer to inspect.
A Troubleshooting Method
Work from the bottom of the stack upward, and from the client toward the server, changing one thing at a time.
Bottom-Up Checklist
- Name: does the name resolve, to the expected address, from this host? (
dig,getent hosts name) - Route and reachability: is there a route, and do packets arrive? (
ip route get,ping,mtr) - Port: does a TCP connection open? (
nc -vz host 443; timeout points to a firewall, refused to no listener) - Listener: on the server, is the process listening on the right address and port? (
ss -tlpn) - TLS: does the handshake succeed with a valid certificate for the name? (
openssl s_client) - Application: what does the protocol return, and what do the server logs say? (
curl -v, logs)
Capture Packets When Needed
When the tools above disagree with what the application claims, look at the packets themselves. tcpdump shows exactly what crosses an interface; save captures to a file and open them in Wireshark for detailed analysis. Captures can contain sensitive data, so limit them with filters and delete them after use.
# TCP handshakes and resets to or from one host on port 443, without name lookups.
sudo tcpdump -ni any 'host 10.0.20.15 and tcp port 443 and (tcp[tcpflags] & (tcp-syn|tcp-rst) != 0)'
Practice Lab
Use two Linux machines or containers on the same network, one running a small web server.
Inspect a Connection
- Start a web server bound to
127.0.0.1:8080, try to reach it from the second machine, and explain the error. Rebind it to0.0.0.0and retry. - Use
curl -wto time DNS, connect, TLS and first byte for three public sites, and compare. - Inspect one site's certificate chain with
openssl s_clientand note its expiry date and issuer.
Diagnose Broken Paths
- Ask a partner to break the path secretly: a wrong
/etc/hostsentry, a firewall drop rule, or a stopped service. - Find the fault using only the bottom-up checklist, writing down each command and what it ruled out.
- Repeat with a firewall reject rule instead of drop, and explain why the symptom changed.