learn/phase-1/p1-w3/lesson 02
Week 3 · lesson 2

DNS on Linux, localhost & URL Anatomy

Hands-on. Where names map to IPs on your own machine.

What you'll learn

  • Know the two files Linux uses to resolve names: /etc/hosts and /etc/resolv.conf.
  • Explain what 127.0.0.1 (localhost / loopback) means.
  • Break a URL into its parts: scheme, host, port, path, query.

DNS on Linux, localhost & URL Anatomy

You already know DNS turns names into IPs. Now let's see where that happens on your own machine.

DNS mapping in Linux

Two files do most of the work:

  • /etc/hosts, a manual, local list of name → IP mappings. Your computer checks this first, before asking any DNS server. Great for testing; also a classic place malware adds fake entries.
  • /etc/resolv.conf, tells your system which DNS servers to ask when a name is not in /etc/hosts.
# /etc/hosts
127.0.0.1   localhost
10.10.10.5  target-01

The vocabulary that ties it together: a client sends a request, a server sends a response; DNS maps a domain name to an IP address.

Loopback / localhost

127.0.0.1  →  always points back to your own computer

127.0.0.1 (name: localhost) is the loopback address. Traffic to it never touches the network card, it loops straight back inside your machine. That is why developers run a local web server and visit http://127.0.0.1:8080 to test before anyone else can reach it.

Anatomy of a URL

A URL packs several of the things you have learned into one string:

https :// example.com : 443 / login ? next=/home
  │         │           │     │        │
scheme     host        port  path    query
  • scheme, the protocol (https)
  • host, the domain name (resolved by DNS) or an IP
  • port, which service (often hidden; defaults to 443 for https)
  • path, which resource on the server
  • query, extra parameters after ?

Reading a URL well is a security skill: the difference between saarathiacademy.com and saarathiacademy.com.np (or a sneaky @ in the host) is exactly what phishing relies on, which is the next lesson.

Try it

In the terminal, cat hosts.txt to read a sample hosts file, and grep target hosts.txt to search it. Then dig example.com to see the DNS path that /etc/resolv.conf would send you down.

Check your understanding

3 questions

Type an answer and press Check. Grading is keyword-based and forgiving, so short answers are fine.

  1. 1

    Given the /etc/hosts line 10.10.10.5 target-01, what IP does target-01 resolve to on this machine?

  2. 2

    From the URL https://shop.example.com:8443/cart?id=7, what are the port and the path?

  3. 3

    ping 127.0.0.1 replies instantly without touching the network. Which interface handled it?