Learn › Linux Foundations › Important Files and Paths
cat - a hands-on Linux lab on a real virtual machine.
Read the local name table with cat /etc/hosts. What the file is for, the address-whitespace-name line shape, the two loopback lines that both mean this machine, and why a line here is checked before DNS and wins. /etc/hosts is structure-only: your hostname and aliases vary, so the drills teach the format and the moves.
Type ping google.com and your machine somehow turns the word google.com into a numeric address it can actually reach. There is a whole network service for that lookup, called DNS, a kind of internet-wide phone book. But before your machine ever asks that phone book, it checks a tiny file on your own disk first. A file you can read, and edit, by hand.
That file is /etc/hosts. It is a private, local list of names and the addresses they point to, and because the machine reads it FIRST, whatever you write in it wins. This lesson opens that file and teaches you how to read every line in it.
The black boxes below are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. /etc/hosts is a real system file, and its exact contents change from machine to machine: a different hostname, different aliases, maybe extra lines. The file shown here is one real example. On the live machine at the end of the module, you read your own /etc/hosts and see your own lines.
/etc/hosts is a plain text file that maps names to IP addresses on this one machine. An IP address is the numeric location of a computer on a network, like 192.168.1.50. A hostname is a friendly name for it, like webserver. The file's whole job is to say: when someone asks for this name, hand back this address.
Think of it as a sticky note on your own desk. Before you look a number up in the big shared phone book (DNS), you glance at the sticky note. If the name is written there, you use that number and never open the phone book at all. That ordering, local file first, is the single most important fact about this file.
The file lives at /etc/hosts on every Linux machine, and you read it the same way you read any text file, with cat. Open it now:
cat /etc/hosts
prompt: student@linuxcamp:~$ answer: cat /etc/hosts output: 127.0.0.1 localhost 127.0.1.1 debian ::1 localhost ip6-localhost ip6-loopback hint: Use cat, a space, then the full path: cat /etc/hosts
Those exact lines belong to the machine this was captured on. On YOUR machine the second line will carry a different hostname (you might see raspberrypi or web01 where this shows debian), and some machines add or drop the IPv6 lines. What never changes is the SHAPE of a line, and that shape is the whole lesson.
Every line in /etc/hosts is built the same way, left to right: an IP address, then some whitespace (one or more spaces or tabs), then one or more names that all resolve to that address. That is it. Read the first line again:
When a line lists several names after the address, as the ::1 line does, every one of those names resolves to that same address. So on the captured machine, localhost, ip6-localhost, and ip6-loopback all point at ::1. Multiple names for one address is normal and useful: it is how one machine answers to several aliases.
A # at the start of a line makes it a comment: the machine ignores it. Many distributions ship /etc/hosts with a comment line or two at the top explaining the file. Those are notes for humans, not name mappings.
Two of those lines appear on essentially every Linux machine, and both point back at the machine itself. That is called loopback: an address that means "right here, this same computer."
127.0.0.1 is the IPv4 loopback. Any traffic sent to it never leaves the machine; it loops straight back. The name localhost is mapped to it so localhost always means "me." The ::1 line is the same idea in IPv6, the newer address format. There, ::1 is the loopback, and it too resolves to localhost.
Read just the loopback line and confirm the name it carries:
cat /etc/hosts
prompt: student@linuxcamp:~$ answer: cat /etc/hosts output: 127.0.0.1 localhost 127.0.1.1 debian ::1 localhost ip6-localhost ip6-loopback hint: Same command as before, then read the 127.0.0.1 line: cat /etc/hosts
Leave the loopback lines alone. 127.0.0.1 localhost and the ::1 line are load-bearing: countless programs on the machine expect localhost to resolve to the machine itself. Deleting or changing them breaks things quietly. When people edit /etc/hosts, they ADD new lines below these, they do not touch these.
Now the payoff. Because the machine reads /etc/hosts before it ever asks DNS, adding a line here PINS a name to an address, no phone book involved. Suppose a server on your network lives at 192.168.1.50. Add this line to the file:
192.168.1.50 webserver
From then on, ping webserver resolves locally to 192.168.1.50, instantly, with no DNS lookup at all. You invented a name and made it real, just for this machine. That same first-checked behaviour drives the three everyday uses:
That webserver line is an EXAMPLE, not something already in the captured file. The addresses and names you would add are yours to choose; what stays fixed is the line shape you learned, address, whitespace, name. Adding lines is exactly what the VM lab at the end of the module has you do for real.
Scaffolding off. No command is printed this time. You have every piece you need.
You have just sat down at an unfamiliar Linux machine and you want to see its local name table: the file that maps names to addresses and gets checked before DNS. Print the whole file to the screen so you can read every mapping in it.
prompt: student@linuxcamp:~$ answer: cat /etc/hosts output: 127.0.0.1 localhost 127.0.1.1 debian ::1 localhost ip6-localhost ip6-loopback hint: Reach for the read-a-file command from earlier lessons and hand it the full path. Think cat, then /etc/hosts.
That is the move: cat /etc/hosts prints the whole local name table. You recalled the command from memory and pointed it at the path, which is exactly what the real machine will ask of you. Your own machine's rows will read differently: a different hostname on the second line, perhaps extra entries. The shape of every line is the same one you can now read, an address, whitespace, then one or more names.
You earned this cheat sheet. It is the whole file in five facts:
Two things bite people here. First, editing /etc/hosts needs root (administrator) rights; a normal user can read it but not save changes to it. Second, because this file is checked first, a wrong entry silently overrides real DNS: point a real domain at the wrong address and the machine will believe you and fail to reach it. Read freely; edit carefully.
The practice terminal has shown you the shape of /etc/hosts: what it is for, the address-whitespace-name line, the two loopback lines that mean "this machine", and why a line you add here beats DNS. Every one of those you read yourself.
The Important Files module ends with one real Linux machine, the Important Files capstone mission. That is where you read /etc/hosts for real. A VM boots just for you, with its own live system files. It hands you objectives that use exactly what you practiced across this module: reading the accounts, groups, names, and settings that describe the machine. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.
Finish the other Important Files lessons, then go read a real machine's name table for yourself.
Practice /etc/hosts in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.