You usually reach a machine by its name, like web. But the network only delivers to IP addresses, like 10.0.0.10, so a machine has to look up the address that goes with a name. The simplest place it looks is the file /etc/hosts, a list of addresses and their names:
learner@ops:~$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 ops
10.0.0.10 web ← ping web goes to 10.0.0.10
10.0.0.20 db
192.168.50.20 scanner
…Each line holds one address followed by its name. When a name has no line, ping can't find an address for it:
learner@ops:~$ ping -c 3 printer
ping: printer: Name or service not known/etc/hosts belongs to root, so you need sudo to change it. sudo tee -a adds a line to the end of the file. Leave out -a and tee replaces the whole file instead.
echo "10.0.0.30 printer" | sudo tee -a /etc/hostsA line in /etc/hosts only works on the machine it's in: web and db each have their own /etc/hosts. Chapter 4 covers DNS, which lets every machine look up the same names.
Objectives
- Which address does
/etc/hostsgivescanner? - Add the printer, at
10.0.0.30, to/etc/hostsasprinter, then ping it by name.