hosts file

CentOS uses the hosts file to resolve hostnames locally before trying to resolve them from a dns server in /etc/resolv.conf.

Traditionally, I preferred to not setup much in /etc/hosts and primarily use DNS for name resolution. But with the growing popularity of configuration management tools like puppet and chef, it isn’t difficult to maintain a consistent hosts file across an environment which helps lighten the load on DNS servers.

The format is simply ip address, followed by hostnames per line.

For example:

# we want the localhost ip address to resolve as localhost localhost
# we also want this server to resolve to its RFC1918 address websrv01

With websrv01 entered into hosts, we can resolve it a few different ways.

ping websrv01

Both of these will resolve