>_ IzzyLab

Hands-on task

This task is step 8 of the course “Networking: first steps”

Give a site a name

easy~5 min

A new shop is already running on this machine on port 8080, but nobody has created a DNS record for its name yet. The team wants to open the storefront by name right now. Make the name shop.test lead to the address 127.0.0.1 on this machine — then curl http://shop.test:8080/ will show the shop's page.

Don't touch the lines that are already in /etc/hosts: other programs depend on them.

What you'll practise:
  • /etc/hosts
  • getent hosts
  • curl

Topic breakdown

The system can turn a name into an address without a DNS server too — using the /etc/hosts file. It's usually checked first, before DNS, so an entry in it takes effect at once and only on this machine. That's what people do when DNS isn't set up yet or when they need to point a name at another server for a while.

The format: on a line, first the IP address, then one or more names separated by spaces. Lines with # are comments.

127.0.0.1    localhost
10.1.0.7     db.example.lan  db

You can edit the file with an editor (nano /etc/hosts) or append a line from the command line: >> adds text to the end of the file, while > replaces all of its contents.

To check that the entry works, use getent hosts name: the command asks the system the same way programs do.

$ getent hosts db.example.lan
10.1.0.7        db.example.lan db

Ports aren't written in /etc/hosts: the file only answers the question "what address does this name have". The port goes into the request address itself — http://name:port/.

What's checked

  1. The name shop.test resolves
  2. The name shop.test leads to 127.0.0.1
  3. The old lines of /etc/hosts are still there
  4. curl http://shop.test:8080/ shows the shop's page

What it looks like