/etc/hosts ignored

I just installed 14.2 on a spare machine. I have used FreeBSD off and on for 20 years or more, and used v14.0 for quite some time on my primary machine until the 14.1 "upgrade" broke the AMD graphics driver. At that point, due to an accumulation of other problems (one with UFS!), which I discussed on this forum, I installed Arch Linux.

That was six months ago and 14.2 is now out, so I decided to try it on a spare machine. After doing just the basic install and creating my account, I scp-ed the /etc/hosts from my primary machine, tweaked it for the machine I was working on and installed it /etc. I've done this countless times in the past. What I found now is that the system appears to be ignoring the hosts file. I can ping my primary machine if I enter its ip address. But if I attempt to ping it by name, the ip address is completely bogus. In fact, if I try to ping the 14.2 from itself by name (its correct ip address is in the hosts file), the same bogus ip address gets used.
Removing the 'search' line from /etc/resolv.conf results in ping reporting that the hostname cannot be resolved. It is obvious that 'ping <hostname>' was ignoring /etc/hosts and using dns despite <hostname> being present in the /etc/hosts file.

Completely on a hunch, I took a look at the hosts file and noted that each of the lines describing a local host began with a tab character. I deleted them, and remarkably, it now works correctly on 14.2. This file with the tabs works fine on Linux, and I've used this file on FreeBSD in the past, so I think this problem was introduced in 14.2, though I can't prove it. From the 'hosts' man page: "Items are separated by any number of blanks and/or tab characters."

I checked /var/log for any indication of an error in processing /etc/hosts and there is none that I could find.

For the presence of white space in a configuration file, undocumented, to break such a basic system function as this is pretty incredible to me.
 
I have tested the problem you describe on FreeBSD 13.4 and FreeBSD 14.2 and can not reproduce the problem.

Specifically, I modified /etc/nsswitch.conf to use "files" only for resolving host names:
Code:
[gunsynd.161] $ grep "^hosts" /etc/nsswitch.conf
hosts: files
When I deleted the test host name (farlap) from /etc/hosts, ping failed, confirming that the (failed) resolution was from /etc/hosts:
Code:
[gunsynd.162] # ping farlap                 
ping: cannot resolve farlap: Name does not resolve
When I restored the test host name to /etc/hosts, and replaced spaces with tabs between the IP address and the host name:
Code:
[gunsynd.163] $ grep farlap /etc/hosts
192.168.1.34        farlap.my.domain.name        farlap
ping worked, confirming that resolution from /etc/hosts, with tabs present, worked.

I observed exactly the same expected behaviour on both FreeBSD 13.4 and 14.2.
 
I'm not sure you're testing exactly what Don is saying? I'm reading this as lines starting with whitespace are ignored and they are. Possible it has been this way since 1996? Testing on 14.1, 13.4, and 9.3. So nothing new to 14.2.


(ins)$ uname -r
14.1-RELEASE-p7
(ins)$ fgrep example /etc/hosts
10.0.0.1 example
(ins)$ ping example
ping: cannot resolve example: Name does not resolve
(ins)$ getent hosts example
(ins)$
...
(ins)$ fgrep example /etc/hosts
10.0.0.1 example
(cmd)$ getent hosts example
10.0.0.1 example
(ins)$ ping example
PING example (10.0.0.1): 56 data bytes
...

I would have expected more people to complain about this however the best I could find is.


Nothing in Bugzilla either.


Looping over cp for spaces/tabs before the strpbrk for spaces/tabs and updating p is I'm guessing what David had in mind 20 years ago?

But given there's less complaints about this maybe it's accepted to just not begin lines with whitespace?
 
Derek -- you are are correct. gpw928 did not test the problem I encountered and you describe the issue correctly. It is the initial whitespace on a line
in /etc/hosts that is the problem.

I consider this a bug and will file a bug report. At the very least, this behavior should be documented, though I think it should be fixed.

I have a history of trying to use FreeBSD over many years, because its development model and other aspects of the system make more sense to me than
the Linux approach. But I've encountered serious problems with FreeBSD each time and had to fall back to Linux. So variants of this hosts file have been passed back and forth
during that time between the systems. I *thought* the tabs were there during previous uses of FreeBSD, most recently last year, but your testing tells me otherwise. Thanks for clearing this up.
 
Apologies for mis-reading the question.

RFC 952 refers to the DoD Internet host table specification for hosts.txt, which is (somewhat) different to /etc/hosts on Unix systems.

I am unable to find an RFC defining what goes into /etc/hosts.

The canonical TCP/IP implementation is that from the University of California, at Berkeley, i.e. BSD Unix, of which FreeBSD is a direct descendant.

The FreeBSD hosts(5) manpage says that "Items are separated by any number of blanks and/or tab characters", suggesting that leading white space may imply a null first item. This is true of all BSD releases, going back to 4.1cBSD.

It's somewhat illogical to suggest that the implementation details of a Unix clone like Linux (or any other Unix clone) should be the arbiter of "what's correct".
 
Back
Top