Skip to content
$ lynuxpro
← Linux

LESSON 15 OF 26 · INTERMEDIATE · 21 MIN

DNS, HTTP and TLS from the command line

In one minute

  • Three things behind every website:
    • DNS turns a name (api.acme.com) into an IP address.
    • HTTP is the language of web requests and replies.
    • TLS encrypts the connection (the "S" in HTTPS) and proves the server is who it says it is, using a certificate.
  • Most web outages are one of these three: a wrong DNS record, a misread HTTP error, or an expired certificate. "It's always DNS" is a running joke because it is so often true.
  • First DNS finds the address. Then a TLS connection is opened and the certificate is checked. Then HTTP sends the request and gets back a status code and a page.
  • 👉 "The site is down", "it works for me but not for them", slow pages, and certificate warnings.

Think of it like: making a phone call. DNS = the phone book (name → number). TLS = checking the caller ID and making the line private. HTTP = the actual conversation.

How DNS works

DNS (Domain Name System) is the internet's phone book.

People remember names. Computers need IP addresses.

When an app connects to api.acme.com:

  1. It checks /etc/hosts first (a small local list of names).
  2. It asks the resolver listed in /etc/resolv.conf. In AWS, this is the VPC's resolver at the .2 address.
  3. The resolver asks the root servers, then the .com servers, then acme.com's own authoritative name servers (the ones that hold the real answer).
  4. The answer is kept (cached) for its TTL (time to live), in seconds.
cat /etc/hostscat /etc/resolv.conf
SHELLbashutf-8 · 2L
nameserver 10.0.0.2search ec2.internal
OUTPUTterminalutf-8 · 2L

search lets you use short names: ping db1 tries db1.ec2.internal. In Kubernetes pods, /etc/resolv.conf points to CoreDNS (the cluster's DNS), with search domains like default.svc.cluster.local.

Remember it: TTL = how long people may remember the old answer.

Record types

TypeMapsExample
AName → IPv4 addressapi.acme.com → 203.0.113.10
AAAAName → IPv6 addressapi.acme.com → 2001:db8::10
CNAMEName → another name (a nickname)www → acme.com
MXWhere to deliver emailacme.com → mail.acme.com
TXTFree text (domain proofs, email rules)"v=spf1 include:..."
NSWhich servers hold the real answersacme.com → ns-1.awsdns.com
SRVA service's host and portUsed by Kubernetes, LDAP
PTRIP → name (reverse lookup)10 → web1.acme.com

AWS also has alias records. They can point the bare domain (acme.com) at a load balancer, which a normal CNAME cannot do.

Asking DNS: dig

dig asks a DNS server a question and shows the full answer.

👉 Any time you suspect DNS: "is this name pointing where I think?"

dig example.comdig +short example.com
SHELLbashutf-8 · 2L
;; ANSWER SECTION:example.com.     300   IN   A   93.184.216.34;; Query time: 12 msec;; SERVER: 10.0.0.2#53
OUTPUTterminalutf-8 · 4L
PartMeaning
300TTL: may be cached for 300 seconds
IN AAn internet A record (name → IPv4)
SERVERWhich resolver answered

Useful versions:

dig +short api.acme.com             # just the answerdig api.acme.com AAAA               # a specific record typedig @8.8.8.8 api.acme.com           # ask a specific resolverdig +trace api.acme.com             # follow the chain from the rootdig -x 10.0.1.25                    # reverse lookupnslookup api.acme.com               # older tool, often availablegetent hosts api.acme.com           # what the system resolver (incl. /etc/hosts) returns
OUTPUTterminalutf-8 · 7L

dig asks DNS directly and ignores /etc/hosts. getent hosts shows what apps really get, including /etc/hosts.

Fixing DNS problems

What you seeLikely causeCheck
Works on your laptop, not on the serverDifferent resolvers, or a private zonedig @resolver name on both
New record not showing yetThe old answer is still cached (TTL)Compare dig @8.8.8.8 with the local resolver
NXDOMAINThe name does not existTypo, wrong zone, record never created
SERVFAILThe resolver could not get an answerDNSSEC problems, broken name servers
Only one machine is wrongAn old line in /etc/hostsgrep name /etc/hosts

Before a migration, lower the TTL (for example to 60 seconds) a day ahead. Then when you switch the record, everyone picks up the change quickly.

HTTP from the command line

HTTP is how browsers and apps talk to web servers.

A request has a method (GET, POST...), a path, headers and sometimes a body. The response has a status code, headers and a body.

👉 Testing health checks, APIs and websites.

curl -I https://example.comcurl -v http://localhost/health
SHELLbashutf-8 · 2L
HTTP/2 200content-type: text/html; charset=UTF-8cache-control: max-age=604800
OUTPUTterminalutf-8 · 3L

Status codes you must know

CodeMeaningUsual cause in operations
200OK
201 / 204Created / no content
301 / 302RedirectHTTP to HTTPS, or a moved path (curl -L follows it)
304Not modifiedCaching is working
400Bad requestThe client sent invalid data
401UnauthorizedMissing or wrong login details
403ForbiddenLogged in, but not allowed; a WAF; an S3 bucket policy
404Not foundWrong path, ingress rule or app route
429Too many requestsRate limiting
500Internal server errorA bug or crash in the app: check the app logs
502Bad gatewayThe proxy reached the app but got a bad answer or none: app crashed or wrong port
503Service unavailableNo healthy app behind the proxy, overloaded, or maintenance
504Gateway timeoutThe app was too slow, so the proxy gave up

Remember it: 2xx = good. 3xx = go elsewhere. 4xx = you (the client) did something wrong. 5xx = the server broke.

502, 503 and 504 from a load balancer almost always mean the problem is behind it (the app), not in the load balancer itself.

Useful curl recipes

curl -s -o /dev/null -w "%{http_code}\n" https://api.acme.com/healthcurl -w "dns:%{time_namelookup} connect:%{time_connect} tls:%{time_appconnect} total:%{time_total}\n" -o /dev/null -s https://api.acme.comcurl -H "Host: api.acme.com" http://10.0.1.25/        # test a backend directlycurl --resolve api.acme.com:443:10.0.1.25 https://api.acme.com/   # pin DNS for a testcurl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' https://api.acme.com/items
OUTPUTterminalutf-8 · 5L
  1. Print only the status code (great for scripts).
  2. Time each step. This tells you whether slowness is in DNS, the connection, TLS or the app.
  3. Send a request straight to one server, skipping DNS.
  4. Pretend DNS points to a certain IP, just for this one test.
  5. Send JSON data with POST.

TLS and certificates

HTTPS is HTTP inside an encrypted TLS connection. The server proves who it is with a certificate, signed by a trusted certificate authority (CA).

Without it, anyone on the network could read or change the traffic, or pretend to be your site.

Think of it like: a passport. It says who you are (the name), it is issued by a trusted government (the CA), and it has an expiry date.

What a certificate contains:

FieldMeaning
Subject / SANWhich names it is valid for (api.acme.com, *.acme.com)
IssuerThe CA that signed it
Not Before / Not AfterWhen it starts and stops being valid
ChainMiddle certificates that link it back to a trusted root

Look at a live certificate:

openssl s_client -connect example.com:443
SHELLbashutf-8 · 1L
echo | openssl s_client -connect api.acme.com:443 -servername api.acme.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates# subject=CN = api.acme.com# issuer=C = US, O = Let's Encrypt, CN = R11# notBefore=Oct  1 00:00:00 2026 GMT# notAfter=Dec 30 23:59:59 2026 GMT
OUTPUTterminalutf-8 · 5L

Common TLS errors

ErrorMeaningFix
certificate has expiredPast its notAfter dateRenew it; automate with cert-manager or ACM
hostname mismatchThe name is not in the certificate's listGet a certificate that covers that name
unable to get local issuer certificateA middle certificate is missing, or a private CA is not trustedServe the full chain; add the CA to the trust store
self-signed certificateNot signed by a trusted CAUse a real CA, or trust the internal CA on purpose

curl -k skips the certificate check. It is fine for a quick test, but never put it in scripts or real code. It switches off the very protection TLS gives you.

Expired certificates are one of the most common avoidable outages. Automate renewal (Let's Encrypt with cert-manager in Kubernetes, AWS Certificate Manager on load balancers) and set an alert 30 days before expiry.

Real-world scenario: "the site is down" after a DNS change

dig +short example.comdig @8.8.8.8 example.comcurl -I https://example.com
SHELLbashutf-8 · 3L
  1. dig +short on the server returns the old load balancer IP. dig @8.8.8.8 returns the new one. The TTL was 86400 (one day), so resolvers still remember the old answer.
  2. The old load balancer was already deleted, so anyone with the cached answer fails.
  3. Fix: bring back the old endpoint for now. Next time, lower the TTL before migrating.

Common mistakes

  • Testing DNS with ping. It hides whether the name lookup or the connection failed. Use dig and getent.
  • Forgetting TTLs before a migration.
  • curl -k in scripts, which quietly accepts any certificate.
  • Blaming the load balancer for 502s when the app behind it is crashing.

Practice

  1. Find the IP of example.com with dig +short, then ask Google's resolver for the same name.
  2. Read the TTL of the answer.
  3. Get only the status code of http://localhost/health.
  4. Check when the certificate on example.com expires.
  5. A user reports 504 errors. Where would you look first, and why?

Cheat sheet

CommandWhat it does
dig +short nameQuick DNS answer
dig @server name TYPEAsk a specific resolver for a record type
dig +trace nameFollow the lookup from the root
getent hosts nameWhat apps really get
curl -I url / curl -v urlHeaders / the full conversation
curl -w "%{http_code}"Status code only
openssl s_client -connect h:443Look at TLS and the certificate

Key takeaways

  • DNS is the phone book: it turns names into IP addresses.
  • DNS answers are remembered for a while (the TTL). Lower it before you change a record.
  • HTTP codes: 2xx good, 4xx client mistake, 5xx server broke.
  • 502, 503 and 504 usually mean the app behind the load balancer has a problem.
  • Certificates expire. Renew them automatically and never use curl -k in real code.