/etc/hosts confusion

I have been confused by the Treehouse video linked to in the Linode documentation.

My question: why does the video only map the IP address to a hostname and not a fqdn?

I have taken all the steps about using the DNS manager in the Linode GUI and changed the DNS to Linode with my domain name registrar. If I use the dnsdomainname command in my ssh session I get no result.

3 Replies

@oliver-w:

I have been confused by the Treehouse video linked to in the Linode documentation.

My question: why does the video only map the IP address to a hostname and not a fqdn?

I have taken all the steps about using the DNS manager in the Linode GUI and changed the DNS to Linode with my domain name registrar. If I use the dnsdomainname command in my ssh session I get no result.

I've never actually watched the Treehouse videos, so I can't speak to their accuracy. However, from my years of experience administrating systems, I can say your /etc/hosts file should look pretty much like this:

198.51.100.5 host.example.com host
2001:db8::dead:beef host.example.com host
127.0.0.1 host.example.com host localhost
::1 host.example.com host localhost

Ubuntu (and possibly also Debian) put other names in there as well, which you can keep (put the localhost ones at the end; host.example.com should always be first). Remove any 127.0.1.1 entry, though. You can use tabs or spaces to separate fields however you like. In this example, please note the following:

* 198.51.100.5 should be replaced with your Linode's public IPv4 address

  • 2001:db8::dead:beef should be replaced with your Linode's public IPv6 address

  • host should be replaced with your Linode's hostname

  • example.com should be replaced with your domain name

  • If you have additional IPv4 or IPv6 addresses of any type up on your Linode, you should add them as well, following the same format as the public IPv4/IPv6 example

Some people put their hostname and FQDN only on their public IPs; I add it to the localhost addresses, as well as private IP address(es) if any, just in case an application decides to do lookups of the fully qualified domain name based on an IP address instead of the hostname. Putting the FQDN first ensures that anything reading /etc/hosts via glibc gets the FQDN as the canonical name, and the other items on the aliases list.

Thanks for the info :mrgreen:
@dwfreed:

Remove any 127.0.1.1 entry, though.

Why do you remove it?

This might answer your last question: https://askubuntu.com/questions/754213/ … -127-0-1-1">https://askubuntu.com/questions/754213/what-is-difference-between-localhost-address-127-0-0-1-and-127-0-1-1

One trick I use with my /etc/hosts file is to force my Linodes to use their private IPs when talking to each other, since Linode does not count private IP traffic against your bandwidth quota. So in /etc/hosts, I specify the public IPs for the host the file resides on, but specify the private IPs for any of my other Linodes that machine may need to talk to. As dwfreed suggests, you can easily include both the FQD and any additional convenience aliases you want, as long as their on the same line as the IP. Since /etc/hosts takes precedence over DNS, references to those aliases will always covert to the IP in that file, meaning you'll always use the private IP. If that IP ever changes (i.e. you spin up a replacement Linode with a new IP), changing all your scripts and config files that reference your other Linodes is as simple as updating /etc/hosts.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct