reosoftproductions.com
RODNEY AND ARLYN'S WEB SITE

Linux - hosts File

Linux Linux - hosts File

hosts File

The hosts file is a simple text file that associates IP addresses with hostnames, one line per IP address.

Modifying your hosts file enables you to override the DNS for a domain, on that particular machine. This is useful when you want to test your site without the test link, prior to going live with SSL; verify that an alias site works, prior to DNS changes; and for other DNS-related reasons. Modifying your hosts file causes your local machine to look directly at the IP address specified.

The hosts file can be edited using any editor of choice.

For each host, a single line should be present with the following information:

IP_address canonical_hostname [aliases...]/

Fields of the entry are separated by any number of blanks and/or tab characters. Text from a "#" character until the end of the line is a comment, and is ignored. Host names may contain only alphanumeric characters, minus signs ("-"), and periods ("."). They must begin with an alphabetic character and end with an alphanumeric character. Optional aliases provide for name changes, alternate spellings, shorter hostnames, or generic hostnames (for example, localhost).

Example

# The following lines are desirable for IPv4 capable hosts
127.0.0.1       localhost

# 127.0.1.1 is often used for the FQDN of the machine
127.0.1.1       thishost.mydomain.org  thishost
192.168.1.10    foo.mydomain.org       foo
192.168.1.13    bar.mydomain.org       bar
146.82.138.7    master.debian.org      master
209.237.226.90  www.opensource.org

# The following lines are desirable for IPv6 capable hosts
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

This file is located in /etc/hosts.

Modifications to this file normally take effect immediately, except in cases where the file is cached by applications.

Understanding Name Service Switch in Linux

Before moving any further, you should understand a few things about another important file that is /etc/nsswitch.conf. It provides Name Service Switch functionality which controls the order in which services are queried for name service lookups.

The configuration is based on order; if files is before dns it means the system will query the /etc/hosts file before checking DNS for name service requests. But if DNS is before files then the domain lookup process will consult DNS first before any other appropriate services or files.

In this scenario, we want to query the "files" service. To check the order, type:

$ cat /etc/nsswitch.conf

OR

$ grep hosts /etc/nsswitch.conf