# /etc/hosts needs host, host.domain, and host.domain.



## scotia (Apr 30, 2019)

Hi all.

My servers all point sendmail to a mailhub, let's call it `mailhub`, with an FQDN of `mailhub.domain`.

In my sendmail file I put a trailing dot, thusly: `mailhub.domain.`.

To my surprise, in /etc/hosts I needed the exact entry - having just the uQDN or omitting the trailing dot wouldn't work.

It appears also that /etc/resolv.conf isn't consulted (it doesn't add the search domain and then query /etc/hosts).  Which leads me to think that the matching against /etc/hosts is a simple word-boundary delimited text search.  Does that sound right?

Now I'm tempted to edit all of my /etc/hosts files and add new entries for each host with a trailing dot. (Just in case there's a program somewhere referring to it).

Thanks,
Scott


----------



## D-FENS (Apr 30, 2019)

The trailing dot makes the domain name absolute. If you omit it, it might get overridden similarly to when you use an absolute path /... or a relative one .../....
Do you have a "search" clause in your /etc/resolv.conf?

Normally the domain name resolution (including /etc/hosts) should handle domain names without a trailing dot just fine (it's a shortcut according to RFC 1034, https://tools.ietf.org/html/rfc1034 , page 7).

It is not clear from your question exactly what "wouldn't work". If you ask a precise question and post the commands and an error message you're getting it would be easier to help.


----------



## SirDice (Apr 30, 2019)

scotia said:


> It appears also that /etc/resolv.conf isn't consulted (it doesn't add the search domain and then query /etc/hosts).


It uses DNS _or_ the hosts file, not a combination of both. And the hosts file is typically checked first. See nsswitch.conf(5). 

```
hosts: files dns
```


----------



## scotia (Apr 30, 2019)

Hi,

precise question: is it expected that an entry in /etc/hosts like:


```
127.0.0.1 host.domain
```

NOT resolve `host.domain.`? (NB: the trailing dot).

eg:

```
# grep test /etc/hosts
127.0.0.1               test.domain
# ping test.domain.
ping: cannot resolve test.domain.: Unknown host
# ping test.domain
PING test.domain (127.0.0.1): 56 data bytes
```

Also:


```
# grep google /etc/hosts
127.0.0.1               google.com
# ping -c 1 google.com
PING google.com (127.0.0.1): 56 data bytes
...
# ping -c 1 google.com.
PING google.com (216.58.199.78): 56 data bytes
```


----------



## SirDice (Apr 30, 2019)

You shouldn't use the trailing dot on URLs. The trailing dot should only be used inside DNS zone records.


----------



## scotia (Apr 30, 2019)

Not sure from where you're getting URLs...  let's get back to the described behaviour.

In any case, a trailing dot in some configurations is important:

From: https://www.sendmail.org/~ca/email/doc8.12/cf/m4/masquerading_relaying.html



`For duplicate suppression to work properly, the host name is best specified with a terminal dot:
define(`MAIL_HUB', `host.domain.')
      note the trailing dot ---^`


----------



## SirDice (Apr 30, 2019)

scotia said:


> Not sure from where you're getting URLs...





scotia said:


> In my sendmail file I put a trailing dot, thusly: `mailhub.domain.`.


URL, URI, whatever it's called. You shouldn't put the trailing dot here.


----------



## scotia (Apr 30, 2019)

Diversions aside , are you saying the question in post #4 is so flawed that it can't be answered?



SirDice said:


> You shouldn't use the trailing dot on URLs. The trailing dot should only be used inside DNS zone records.



Given the above sentence directly conflicts with the Sendmail documentation, can you cite a reference or elucidate on why trailing dots are to be avoided (when specifying hosts, not URLs - which I assume break requests because of the Host: header)?

Thanks.


----------



## SirDice (Apr 30, 2019)

scotia said:


> Given the above sentence directly conflicts with the Sendmail documentation


It assumes you're using DNS to resolve. Which begs the question, why aren't you using DNS? Managing hosts files is a royal pain in the posterior. Especially if you have hundreds of hosts to maintain.


----------



## Chris236 (May 2, 2019)

SirDice said:


> It assumes you're using DNS to resolve. Which begs the question, why aren't you using DNS? Managing hosts files is a royal pain in the posterior. Especially if you have hundreds of hosts to maintain.



And that is the point. As soon as you have working DNS, use it. /etc/hosts is only there for the few cases where you need that information but do not (yet) have DNS.

And if you ever have something to add to /etc/hosts, the full proper and ugly way is (and has been for more than a decade):

```
192.0.2.3        plumpudding  plumpudding.example.com
192.0.2.3        plumpudding.example.com.
```
And the same again for v6


----------



## scotia (May 4, 2019)

I really wish I started this thread with post 4.  No-one has really had a swing at it yet.  I would expect a "yes" or "no" or "that's odd".

To the other points raised - there are good reasons to use /etc/hosts.  One of my hosts is a mail relay (for authenticated users) which has a tunnel to my internal network over which I want things like syslog.  It's memory-constrained VM and so I don't want to run a DNS server just to resolve my syslog hostname.  This is but one reason to use a static file for hostname resolution.



Chris236 said:


> And if you ever have something to add to /etc/hosts, the full proper and ugly way is (and has been for more than a decade):



I've never seen/used the same host IP across different lines.  Just for anyone's reference reverse lookups resolve to the first hostname listed on the first matching line.


----------

