Linux – proper relationship between /etc/hosts and DNS A records for a Linux server

domain-name-systemhostnamelinux

I have an Ubuntu server. It is going to be a web server with a URI of www.example.com. I have a DNS A record pointing www.example.com to the server's IP address.

Let's say I pick "trinity" as the hostname for this server.

I want to set up the DNS records correctly. I need reverse DNS to www.example.com, so a CNAME for www.example.com doesn't seem appropriate. Here's my question:

Is it considered best practice to set up two DNS records (which in my case would likely be two A records), one for www.example.com and one for trinity.example.com, both pointing to this server's IP address? (Or, even if it is not accepted as a best practice, is it a good idea?)

If so, would the following be a proper /etc/hosts file?

$ cat /etc/hosts
127.0.1.1       trinity.local          trinity
99.100.101.102  trinity.example.com    trinity        www.example.com

This server is a Linode and Linode's docs seem to imply that the above approach is best (if I am reading them correctly). Here's the relevant section. I bolded the line that seems to apply here.

Update /etc/hosts

Next, edit your /etc/hosts file to resemble the following example,
replacing "plato" with your chosen hostname, "example.com" with your
system's domain name, and "12.34.56.78" with your system's IP address.
As with the hostname, the domain name part of your FQDN does not
necesarily need to have any relationship to websites or other services
hosted on the server (although it may if you wish). As an example, you
might host "www.something.com" on your server, but the system's FQDN
might be "mars.somethingelse.com."

File:/etc/hosts

127.0.0.1        localhost.localdomain    localhost
12.34.56.78      plato.example.com        plato

The value you assign as your system's FQDN should have an "A" record
in DNS pointing to your Linode's IP address
. For more information on
configuring DNS, please see our guide on configuring DNS with the
Linode Manager.

Best Answer

The proper relationship between /etc/hosts entries and DNS is that if you have working DNS /etc/hosts should only contain entries for localhost (pointing to 127.0.0.1 and ::1). Mucking about with a hosts file when you have working DNS is just a way to create strange behavior and trouble later.1

If you need reverse DNS contact your ISP (Linode) and ask them to set an appropriate PTR record for your IP address.
There is no requirement for the PTR record to match an A record (or indeed for any host with that name to exist at all: 69.18.136.215 reverses to cl136-215.invision.net, which does not have a forward address), so you can set the PTR name to match an A record, a CNAME, or to any valid DNS name you would like.


1As with all rules, exceptions exist. Your case does not sound like one of them.