DNS, DHCP, HTTP and HTTPS, TCP and UDP, and Ports

DevOps and Cloud Engineering: Complete Course from Beginner to Advanced · Networking Fundamentals · 8 min read · Updated

From name to page

  1. A client wants example.com. It does not know the IP address yet.
  2. DNS answers with an A record, and the answer is cached for the TTL.
  3. The client connects to the web port, 443, over TCP, and TLS secures the connection.
  4. The application answers with a status code. 200 is success, 5xx means the server failed.

In the last two lessons you learned addresses and networks. Now come the services that make a network usable for people: DNS turns names into addresses, DHCP hands out addresses automatically, HTTP and HTTPS carry web pages, and TCP, UDP and ports decide which program on a machine receives which data. Each one shows up in daily DevOps work, especially when something fails and you must find out why.

Why do we need it?

People use names, computers use numbers. DNS and DHCP make networks easy to use, HTTP carries the web, and ports let many programs share one machine. Most real outages show up as a DNS, port or HTTP status problem.

A real-life example

DNS is the phone book: you look up a name and get a number. DHCP is a hotel reception that hands you a room number when you arrive. A port is a door number inside the building, so the pizza arrives at the right flat.

DNS: the phone book of the internet

People remember names such as example.com. Computers use IP addresses. DNS (Domain Name System) translates one into the other. When you ask for a name, your computer asks a resolver, usually run by your internet provider or a public service. The resolver asks the system of servers that hold the answer, and keeps the reply for a time (the TTL, time to live) so it does not have to ask again. The records you will meet most often are these.

  • A: a name to an IPv4 address. AAAA: a name to an IPv6 address.
  • CNAME: a name that is an alias of another name.
  • MX: which servers receive email for the domain.
  • TXT: free text, used for domain verification and email security settings.
  • NS: which servers are in charge of the domain.

When you change a DNS record, old answers stay in caches until the TTL runs out, so changes can take minutes or hours to be seen everywhere. That is called propagation, and it is a common reason for the answer: I changed it, but I still see the old site. Before a planned move, lower the TTL a day ahead. Tools such as dig example.com and nslookup example.com show what DNS answers. They are not installed in every container image, so you may need to install them.

DHCP: addresses on request

When your phone joins Wi-Fi, nobody types an address in. DHCP (Dynamic Host Configuration Protocol) on the router gives the device an address, the subnet mask, the gateway (the way out to other networks) and the DNS server, for a limited time called a lease. Servers usually need a fixed address instead, so they get a static address or a reservation, and cloud providers handle this for you. If a device shows an address starting with 169.254, it did not get an answer from a DHCP server.

HTTP and HTTPS

HTTP is the language of the web. The client sends a request with a method, a path and headers, and the server sends back a status code, headers and a body. HTTPS is the same conversation inside TLS, which encrypts it and proves the server’s identity with a certificate.

  • Methods: GET reads, POST creates, PUT or PATCH changes, DELETE removes.
  • 2xx success: 200 OK, 201 Created.
  • 3xx redirect: 301 moved for good, 302 moved for now.
  • 4xx the client did something wrong: 400 bad request, 401 not logged in, 403 not allowed, 404 not found, 429 too many requests.
  • 5xx the server failed: 500 internal error, 502 bad gateway, 503 unavailable, 504 gateway timeout.

In DevOps you will see 502, 503 and 504 often. They usually mean a proxy or load balancer could not get a good answer from the application behind it: the app is down, overloaded or slow. Try curl -I https://example.com to see the status line and headers, and curl -v for the full conversation.

TCP and UDP

  • TCP sets up a connection first (the three-way handshake), numbers every piece of data, resends lost pieces and delivers in order. It is reliable and a little slower. Web, email, SSH and databases use it.
  • UDP just sends. There is no connection and no resend. It is fast and light, and fine when old data is useless anyway: video calls, games, DNS lookups.

Ports

An IP address finds the machine. A port number finds the program on that machine, so one server can run a web server and a database at the same time. Ports go from 0 to 65535. A program listens on a port, and a client connects to it. These are the ones worth knowing by heart.

  • 22 SSH
  • 25 / 587 email sending (SMTP)
  • 53 DNS
  • 80 HTTP and 443 HTTPS
  • 3306 MySQL, 5432 PostgreSQL, 27017 MongoDB, 6379 Redis
  • 3000, 5000, 8080 common for development web apps

To see what is listening on a Linux machine, use ss -tlnp (or netstat -tlnp on older systems), which shows the open TCP ports and the program for each. If your app says address already in use, another program holds the port. To test whether you can reach a port from another machine, use curl for web services, or the check in the health script of the Linux course.

Architecture diagram

Here is the idea as a simple picture. You can use the same diagram in your own notes.

sequenceDiagram
  participant B as Browser
  participant D as DNS
  participant S as Web server
  B->>D: Address of example.com?
  D-->>B: 203.0.113.10
  B->>S: TCP connect on port 443
  B->>S: GET /
  S-->>B: 200 OK and the page

How to check it worked

  • You can explain what a 502 and a 404 mean and who is at fault in each case.
  • You can say why a DNS change is not seen at once.
  • You can match each of the ports above to its service.

How companies use it

Teams manage DNS records for domains, load balancers and email, and use TTLs when moving a service. They read HTTP status codes in monitoring and logs, and check ports when services cannot connect.

Common errors and how to fix them

  • I changed DNS but still see the old site. Caching. Wait for the TTL or flush local caches, and lower the TTL before planned changes.
  • 502 Bad Gateway. The proxy could not get a good answer from the app. Check that the app is running and healthy.
  • 504 Gateway Timeout. The app answered too slowly. Check load, database and timeouts.
  • Address already in use. Another program holds the port. Find it with ss -tlnp.
  • Service reachable locally but not from outside. It listens on 127.0.0.1 or a firewall blocks the port.

Best practices

  • Lower TTL before moves.
  • Use health endpoints returning 200.
  • Do not expose databases to the internet.
  • Use HTTPS everywhere.
  • Know the common ports.

Interview questions with simple answers

  1. What does DNS do? Translates names to IP addresses.
  2. What is a CNAME? An alias from one name to another.
  3. What is TTL? How long an answer may be cached.
  4. What does DHCP do? Gives devices addresses and settings automatically.
  5. GET vs POST? GET reads data. POST sends data to create something.
  6. What does 404 mean? The page was not found.
  7. What does 502 mean? Bad gateway: the proxy got a bad answer from the app behind it.
  8. TCP vs UDP? TCP is reliable and ordered. UDP is fast with no guarantee.
  9. Which port is SSH? 22.
  10. What is a port? A number that identifies a program on a machine.

Practical exercises

Try each one yourself before you look at the hint. Struggling for a few minutes is how the skill sticks.

  1. Run curl -I on three sites and note the status. Hint: Look at the first line.
  2. Find what is listening on your machine. Hint: ss -tlnp.
  3. Start a web server on port 8000 and open it with curl. Hint: python3 -m http.server 8000.
  4. Look up an A record and its TTL. Hint: dig or nslookup.
  5. Explain why 127.0.0.1 is not reachable from another machine. Hint: It is the loopback address.

Mini project: Website detective

Pick a public site. Record its DNS A record, TTL, HTTP status, server header and TLS certificate expiry using command-line tools. Present the findings as a one-page table.

Knowledge test

Answer the ten questions in the quiz at the end of this lesson. You pass with 60 percent. If an answer is wrong, read the explanation, then reread the part of the lesson it points to.

Frequently asked questions

Why do I still see the old site after I changed DNS?

DNS answers are cached until their TTL expires, on resolvers and sometimes on your own computer or browser. Wait for the TTL, or lower the TTL before the change next time.

What is the difference between 502 and 504?

A 502 means the proxy got a bad answer from the application behind it. A 504 means the proxy waited and got no answer in time. Both point to a problem behind the proxy.

Why is DNS usually UDP and not TCP?

DNS questions and answers are small, so a fast UDP message is enough. DNS falls back to TCP for large answers.

Practise with interview questions