Menu
Coddy logo textTech

The Hosts File

Lesson 6 of 47 in Coddy's Networking with the Terminal course.

Before your machine asks DNS anything, it checks a file on disk: /etc/hosts. Look inside it:

cat /etc/hosts

Each line is an address followed by one or more names for it. The line you will always find is this one:

127.0.0.1    localhost

That is why localhost works with no network and no DNS server at all. The answer is sitting in a file.

The hosts file wins over DNS. Whatever it says is used, and DNS is never consulted for that name. That makes it genuinely useful, for pointing a real domain at a test machine while you work, and it also makes it the first place to look when a name resolves somewhere that makes no sense.

Your file has other lines too, including ones for IPv6 and usually one for the machine's own name, so do not expect it to look identical to anyone else's.

challenge icon

Challenge

Medium

Find out what localhost resolves to, straight from the file that answers it.

Print only the address on the first line of /etc/hosts that mentions localhost. The expected output is exactly:

127.0.0.1

Do not print the whole file. Your hosts file has several lines, including IPv6 ones and your machine's own name.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Networking with the Terminal

Practice on your own: Terminal playground