Enabling Local Name Resolution with hosts Files

You want to set up name resolution on your LAN. Because you are not running any public services, you don’t want to mess with registered domain names; you want to use whatever domain name you feel like inventing. You know that your invented domain name will not be valid outside your LAN, and that’s okay, because you are not running any public services. Your network is stable and rarely changes, so it isn’t worth setting up a DNS server; you just want to use nice, friendly hostnames on your LAN.

Or you are running public services, such as a mail or web server, and you are using a third-party DNS manager for your public services. Your LAN is small, so you don’t want to hassle with setting up a DNS server just for your LAN.

Or you have a registered domain name, and you are already running a DNS server, but you want to enter important machines in hosts files as a backup in case the DNS server goes down.

Use hosts files. hosts files are the same on both Linux and Windows.

On most Linuxes, you need to edit two files: /etc/hostname and /etc/hosts. In /etc/hostname, enter only the hostname of the machine:

windbag

On Red Hat and Fedora, you must edit /etc/sysconfig/network instead of /etc/hostname:

HOSTNAME=windbag

/etc/hosts sets the domain name. You must always have a localhost entry, then add the IP address and the fully qualified domain name on a separate line:

127.0.0.1 localhost.localdomain localhost192.168.1.5 windbag.test.net windbag

Check your configurations:

$ hostnamewindbag$ hostname —fqdn

windbag.test.net

$ dnsdomainname

test.net

You must reboot for hostname changes to take effect.

On Windows, set the hostname by right-clicking My Computer, left-clicking Properties, and entering the name. The location of the hosts file varies; the quickest way to find it is Start -> Search.

Every machine on the LAN must have an identical copy of the hosts file. When this is done, everyone can ping each other by hostname, and applications like Samba that rely on hostnames can be used.

hosts files are the original method for mapping hostnames to IP addresses. DNS was invented because managing more than a few hosts in hosts files quickly becomes difficult.

Using hosts files still has a number of advantages:

  • You’re not dependent on a single server.
  • You can invent any old domain name you like, as long as you only need it for your local network, so you don’t have to hassle with having a registered domain name.
  • You can change domain names whenever you feel like it, which is handy for testing virtual Postfix and Apache domains.
  • If you use /etc/hosts only for servers, which need static IP addresses, you can put the rest of your LAN on a DHCP server and not even bother with hosts entries for them

Here is a sample hosts file. They look just the same on both Linux and Windows:

127.0.0.1        localhost.localdomain localhost192.168.1.5      windbag.test.net  windbag192.168.1.6      powerpc.test.net  powerpc

192.168.1.10     stinkpad.test.net  stinkpad