Mapping Names to IP Addresses with the etchosts File


Mapping Names to IP Addresses with the /etc/hosts File

You need a way for hostnames to map to IP addresses. Normally, this is done with DNS, but you don't have DNS set up yet. In the meantime, you can provide mappings for hosts you know aboutsuch as hosts on our local networkwith the /etc/hosts file.

/etc/hosts allows you to build a table in which each entry lists an IP address, its most common (or "official") hostname, and any aliases (additional names that map to the same IP address). Each of these items is separated by spaces or tabs. This table is consulted before any DNS queries are made, so /etc/hosts acts both as a backup to DNS (if it's not available) and an override (if it's not serving correct information). The aliases can even be used as a shorthand for favorite hosts whose names you don't want to have to remember, as with shell aliases (which we discussed in the "Shell Initialization Files" section of Chapter 9, "Customizing the Shell").

Here are a few sample lines from /etc/hosts:

64.41.131.132         ns              ns.example.com lion.example.com 64.41.131.133         www2            www2.example.com 64.41.132.165         www3            www3.example.com 114.235.123.11        www.foobar.com  fred


With this table in place, you can connect to the listed hosts by nameeither the "official" name in the second column or any of the aliases listed in the third columneven if no DNS servers can be reached.

Caution

You should remove host entries from your /etc/hosts file once they're reachable properly with DNS. The reason for this is to aid in support and troubleshooting later; if you have trouble connecting to a certain host, you might waste hours in fruitless tinkering before realizing that you had hard-coded an IP address into /etc/hosts rather than relying on DNS.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net