hosts file seems to be ignored
Solution 1
With this configuration, most applications will happily work with your entry from /etc/hosts
.
However host
doesn't look at /etc/nsswitch.conf
. That is by design, not by accident, since host
is specifically a DNS lookup program. /etc/hosts
is not DNS, it's (mostly) what we used before we had DNS.
The same is also true for dig
and nslookup
- they're DNS specific too.
Solution 2
The host
command (along with dig
and nslookup
) is part of the bind
DNS utilities. As a DNS resolver utility, it does DNS resolution alone.
If you're interested in fetching an entry from any libnss
-driven data store, you can use the getent
program. To get a hosts entry, for example, use it like this:
getent hosts google.com
This follows the resolution order set out under hosts:
in /etc/nsswitch.conf
in order, which includes /etc/hosts
if "files" is lised as one of the options.
Solution 3
For me this issue occurred due to incorrect file permissions. Only root could read /etc/hosts. The file should be world-readable.
Solution 4
These days (2021) Firefox is using DNS over HTTPS which you have to disable for it to respect your hosts file. I think this has some privacy implications, so get informed before you do it.
Go to Firefox settings, search DNS. Click settings
next to Configure how firefox connects to the internet
. At the bottom, uncheck Enable DNS over HTTPS
Related videos on Youtube
z4y4ts
Updated on September 17, 2022Comments
-
z4y4ts almost 2 years
I have almost fresh Ubuntu desktop box. OS was installed two weeks ago and updated from karmic repositories. Last week I had no problems with DNS. But this week something had changed. I'm not sure what and when, and not sure whether I changed any configs.
So now I have some really weird situation. According to logs name resolving should work normally.
/etc/hosts
127.0.0.1 localhost test 127.0.1.1 desktop
/etc/host.conf
order hosts,bind multi on
/etc/resolv.conf
# Generated by NetworkManager search search servers obtained via DHCP nameserver 192.168.0.3
/etc/nsswitch.conf
passwd: compat group: compat shadow: compat hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 networks: files protocols: db files services: db files ethers: db files rpc: db files netgroup: nis
But if fact it is not.
user@test ~>ping test
PING localhost (127.0.0.1) 56(84) bytes of data. [skip]
Pinging is ok.
user@test ~>host test
test.mydomain.com has address xx.xxx.161.201
I suspect that NetworkManager might cause this misbehavior, but don't know where to start to check it. Any thoughts, suggestions?
-
z4y4ts over 14 yearsSo, you're saying,
host
utility doesn't use/etc/hosts
? And there's nothing to fix in my case? -
Alnitak over 14 yearsyup, pretty much - nothing to see here :)
-
mveroone over 8 yearsWhat I don't get is that
host
>used to< be the "regular" name-querying command, obeying/etc/nsswitch.conf
-
Alnitak over 8 years@Kwaio I can find no evidence in the ISC sources that
host
ever did -
mveroone over 8 yearsWell I might have used unsual distribution implementations, then, my bad.
-
Emmanuel almost 8 yearsThank you for reminding me the
getent
command. -
Mikko Rantalainen over 6 yearsI believe that nowadays one should be using
getent ahosts
instead ofgetent hosts
becausegetent hosts
usesgethostbyaddr()
orgethostbyname*()
which are obsolete. If I have understood correctly,getent hosts
emulates how old UNIX C programs used to work andgetent ahosts
emulates the way moderm programs should work.