Skip to content
Visual explainer 5h to learn 5 steps

Networking: TCP/IP, DNS, HTTP, SSH

Explain how a request reaches a server and debug it with curl, dig, ss and ssh.

By Invicly Tech, , in Full DevOps with Python. About 10 minutes to read.

Topic 4 of 34

In short

A web request travels through DNS to find an IP, TCP to open a connection, TLS to encrypt it and HTTP to ask for data. Knowing each layer tells you which tool to use when something breaks.

Explained simply

A network is computers passing small packets of data to each other. Each machine has an IP address, like a street address, for example 10.0.1.25. Each program on that machine listens on a port, like an apartment number: web servers use 443, SSH uses 22, Postgres uses 5432.

People remember names, computers need numbers. DNS is the phone book that turns api.example.com into an IP address. Your computer asks a DNS resolver, which looks the name up and remembers the answer for a while, so the next lookup is instant.

Once your computer knows the IP, it opens a TCP connection, which is like a phone call: both sides agree to talk and every packet is checked and resent if lost. On top of that, TLS encrypts the call, and HTTP is the language spoken in it.

When something breaks, the error message usually tells you which step failed. "Could not resolve host" is DNS. "Connection refused" means nothing listens on that port. A long hang ending in a timeout usually means a firewall is silently dropping packets. Learning to read these saves hours.

Press Play on a diagram to walk through it one step at a time.

LayersTCP/IP model (top to bottom)
  1. 1
    Application

    HTTP, DNS, SSH. Debug with curl, dig, ssh -v.

  2. 2
    Transport

    TCP or UDP with port numbers like 22, 80, 443.

  3. 3
    Internet

    IP addresses, subnets like 10.0.1.0/24, and routing.

  4. 4
    Link

    Ethernet or Wi-Fi frames on the local network.

SequenceWhat curl https://api.example.com does
curl
DNS resolver
Server
  1. 1Where is api.example.com?
    curl to DNS resolver: Where is api.example.com?
  2. 2A record: 203.0.113.10
    DNS resolver to curl: A record: 203.0.113.10
  3. 3TCP SYN to port 443
    curl to Server: TCP SYN to port 443
  4. 4SYN-ACK, connection open
    Server to curl: SYN-ACK, connection open
  5. 5TLS handshake
    curl to Server: TLS handshake
  6. 6GET /health HTTP/1.1
    curl to Server: GET /health HTTP/1.1
  7. 7200 OK with body
    Server to curl: 200 OK with body
SequenceResolving api.example.com from scratch
Your laptop
Recursive resolver
Root and .com servers
example.com DNS
  1. 1Where is api.example.com?
    Your laptop to Recursive resolver: Where is api.example.com?
  2. 2Who handles example.com?
    Recursive resolver to Root and .com servers: Who handles example.com?
  3. 3Ask ns1.example.com
    Root and .com servers to Recursive resolver: Ask ns1.example.com
  4. 4A record for api?
    Recursive resolver to example.com DNS: A record for api?
  5. 5203.0.113.10, TTL 300
    example.com DNS to Recursive resolver: 203.0.113.10, TTL 300
  6. 6203.0.113.10 (cached 5 min)
    Recursive resolver to Your laptop: 203.0.113.10 (cached 5 min)
CompareReading the error message
Could not resolve host
  • DNS failed
  • Typo or missing record
  • Check with dig
  • Look at /etc/resolv.conf
Connection refused
  • Host answered fast
  • Nothing on that port
  • App down or on 127.0.0.1
  • Check with ss -tlnp
Timed out
  • No answer at all
  • Firewall or security group
  • Wrong route or IP
  • Check rules, then nc -vz

In depth

How a DNS lookup really travels

Your program first checks /etc/hosts, then asks the system resolver, which on many distros is systemd-resolved listening on 127.0.0.53. That forwards to a recursive resolver, such as your cloud's resolver or 1.1.1.1. If the recursive resolver has no cached answer, it asks a root server where .com lives, asks the .com servers where example.com lives, then asks example.com's own name servers for the final record.

Every answer has a TTL (time to live) in seconds, saying how long it may be cached. That is why a DNS change can take minutes or hours to reach everyone.

  • A record: name to IPv4 address
  • AAAA: name to IPv6 address
  • CNAME: name is an alias for another name
  • dig +trace example.com shows every hop

Refused, reset or timed out

Errors tell you where to look. Connection refused means the machine answered but nothing is listening on that port: the app is down or bound to the wrong address. A timeout means no answer came back at all: a security group, firewall or wrong route is dropping packets. Connection reset means something accepted the connection and then cut it, often a proxy or a crashed app.

A very common trap: an app listening on 127.0.0.1:8000 only accepts connections from the same machine. To be reachable from outside or from another container, it must listen on 0.0.0.0. ss -tlnp shows which address each port is bound to.

Subnets and CIDR without the headache

An address like 10.0.1.0/24 describes a range. The /24 means the first 24 bits are the network part, leaving 8 bits for hosts, so 256 addresses from 10.0.1.0 to 10.0.1.255. Each step smaller in the number doubles the size: /23 is 512 addresses and /16 is 65,536.

Private ranges that never appear on the public internet are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Cloud VPCs use them. AWS reserves 5 addresses in every subnet, so a /24 gives you 251 usable hosts there.

  • /32 = 1 address, /24 = 256, /16 = 65,536
  • Hosts = 2 to the power of (32 minus the prefix)
  • python3 -c "import ipaddress as i; print(i.ip_network('10.0.1.0/24').num_addresses)"
LayersPrefix length and range size
  1. 1
    /16

    65,536 addresses

  2. 2
    /23

    512 addresses

  3. 3
    /24

    256 addresses; 251 usable in an AWS subnet

  4. 4
    /32

    One single address

SSH beyond logging in

SSH is also a secure tunnel. Servers in private subnets are usually reached through a bastion (jump) host: ssh -J bastion.example.com app-01 hops through it in one command. A local port forward lets you reach a private service from your laptop: ssh -L 5433:db.internal:5432 bastion makes localhost:5433 on your laptop talk to the database.

Put these in ~/.ssh/config with ProxyJump and LocalForward so you never retype them. ssh -v prints every step of the connection, which shows whether the problem is the network, the host key or your key.

Sequencessh -L 5433:db.internal:5432 bastion
App on laptop
ssh client
Bastion
Database
  1. 1Connect to localhost:5433
    App on laptop to ssh client: Connect to localhost:5433
  2. 2Carried inside the SSH tunnel
    ssh client to Bastion: Carried inside the SSH tunnel
  3. 3TCP to db.internal:5432
    Bastion to Database: TCP to db.internal:5432
  4. 4Query result
    Database to Bastion: Query result
  5. 5Back through the tunnel
    Bastion to ssh client: Back through the tunnel
  6. 6Reply on localhost:5433
    ssh client to App on laptop: Reply on localhost:5433

Learn it in 5 short steps

About 1h each. Track them with a timer in your plan.

  1. 1
    Learn TCP/IP layers, ports and subnets~1h
  2. 2
    Trace DNS lookups with dig~1h
  3. 3
    Inspect HTTP requests with curl -v~1h
  4. 4
    List listening sockets with ss~1h
  5. 5
    Set up SSH keys and config~1h

Key points

  • Debug layer by layer: name resolves, port is open, service answers, response is right.
  • dig shows DNS answers; curl -v shows the TCP, TLS and HTTP exchange.
  • ss -tlnp lists listening TCP ports and which process owns each one.
  • SSH keys beat passwords; ~/.ssh/config saves hosts, users and key paths.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
dig +short api.example.com
curl -v https://api.example.com/health
ss -tlnp | grep 8000
ssh-keygen -t ed25519 -C "me@laptop"
ssh-copy-id deploy@203.0.113.10
ssh -v deploy@203.0.113.10

Resolve, request, check listeners and set up key-based SSH.

"The site is down": debug it layer by layer

Work from the bottom up and stop at the first failure. Step 1: does the name resolve? Step 2: is the port reachable? nc -vz tests a TCP connection without sending data. Step 3: does the service answer, and with what status? Step 4: on the server, is the app listening, and on which address? Each step either passes or tells you exactly which team or setting to check next.

bash
dig +short api.example.com                # 1. DNS
nc -vz api.example.com 443                # 2. TCP port
curl -sS -o /dev/null -w '%{http_code} %{time_total}s\n' https://api.example.com/health
# 3. HTTP status and timing

# 4. on the server itself
ss -tlnp | grep -E ':443|:8000'
curl -sS localhost:8000/health
sudo journalctl -u app -n 50

Reach a private database through a bastion

Your Postgres server sits in a private subnet with no public IP. Only the bastion can reach it. Step 1: describe both hosts in ~/.ssh/config. Step 2: open a tunnel with ssh -N db-tunnel (-N means no remote shell, just the tunnel). Step 3: in another terminal, connect to localhost:5433 as if the database were local. Close the tunnel with Ctrl+C when done.

ssh-config
Host bastion
  HostName bastion.example.com
  User ubuntu
  IdentityFile ~/.ssh/id_ed25519

Host db-tunnel
  HostName bastion.example.com
  User ubuntu
  LocalForward 5433 db.internal:5432

# terminal 1: ssh -N db-tunnel
# terminal 2: psql -h localhost -p 5433 -U app appdb

Quick reference

  • dig +trace name shows the whole DNS path; dig @1.1.1.1 name asks a specific resolver
  • nc -vz host 443 tests whether a TCP port accepts connections
  • curl -w '%{http_code} %{time_total}' prints status and total time
  • Refused = nothing listening; timeout = something dropping packets
  • Bind to 0.0.0.0 to accept outside traffic; 127.0.0.1 is local only
  • Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
  • ssh -J bastion host jumps through a bastion; ssh -L 5433:db:5432 makes a tunnel
  • ip addr shows your addresses; ip route shows where packets go

Common mistakes

  • App bound to 127.0.0.1 is unreachable from other machines; bind 0.0.0.0 when needed.
  • Private key readable by others makes SSH refuse it; use chmod 600.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1curl to port 8000 fails instantly with "Connection refused". What is most likely?

Question 2

2How many addresses are in 10.0.0.0/22?

Question 3

3You changed a DNS record, but some users still reach the old IP. Why?

Question 4

4Your app works with curl localhost:8000 on the server but not from your laptop. ss shows 127.0.0.1:8000. What should you change?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Find the IP address, TTL and name servers for a domain you use, using dig.

  2. Exercise 2medium

    Start a web server on port 8000 bound to 127.0.0.1, prove it is unreachable from another machine or container, then rebind it to 0.0.0.0 and prove it works.

  3. Exercise 3hard

    Write a Python script check.py HOST PORT that reports which layer fails: DNS, TCP or neither, with a 3-second timeout.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between TCP and UDP?
TCP sets up a connection and guarantees ordered, complete delivery, which suits web and database traffic. UDP just sends packets with no guarantee, which is faster and used for DNS, video calls and games.
Why does ping work but my website doesn't?
ping uses ICMP, not TCP, and only proves the machine is reachable. The web port can still be blocked or have nothing listening on it.
Is it safe to use 8.8.8.8 or 1.1.1.1 as my DNS server?
Yes, they are large public resolvers from Google and Cloudflare. Inside a cloud VPC, though, keep the provider's resolver so private names still resolve.

Go deeper

ShareXLinkedInWhatsApp