pi-dabbler
Posts: 8
Joined: Tue May 26, 2015 9:08 am

hosts file not being used?

Tue May 26, 2015 1:58 pm

We have an RPi running a Midori browser against a web server located on the other side of a low-bandwidth WAN in a data centre. The RPi is on 'this side of the WAN' on a subnet of 10.14.9.0/24, and the webserver is in the datacentre on 'the other side of the WAN' on an IP of 10.10.17.15. The URL in the browser references a 'friendly name' webserver.abc.local of the webserver, which exists on the nameservers in the data centre, and which we have entered into the RPi's /etc/hosts file:

...
127.0.1.1 raspberrypi
10.10.17.15 webserver.abc.local

We have a resolv.conf file on the RPi that references these nameservers, but this causes undue traffic over the WAN to resolve 'webserver.abc.local' to its IP address of 10.10.17.15 several times every 60 seconds. So we renamed the resolv.conf file and added the abovementioned entry into the /etc/hosts file .

However it appears that the browser does not utilise the hosts file entry to resolve the IP address of webserver.abc.local, because the browser does not update. We also do not see any http traffic to 10.10.17.15 through the firewall that is located in the datacentre, between the WAN and the web server. We can also see countless attempts finding a nameserver on the WWW to do the DNS lookup, to no avail. When we restore resolv.conf, the browser works as expected, and we see http traffic through the firewall.

In Windows one needs to enable lmhosts file to cause the OS to first look at lmhosts before it uses a nameserver. Is it necessary to do the same in Raspbian, so that the browser is forced to look in /etc/hosts file to resolve an IP address, before it attempts to use a nameserver? Or is there some other root cause for the browser failing to first check the hosts file for IP resolution? Is there a way to test access into the hosts file, to confirm whether the entry inside is correct? It appears nslookup does an external DNS lookup into the WWW, i.e. it does not reference the hosts file.

Thank you.

pi-dabbler
Posts: 8
Joined: Tue May 26, 2015 9:08 am

Re: hosts file not being used?

Mon Jun 15, 2015 9:49 am

After some further Google research and testing on my RPi, it appears that the hosts file is indeed being used before a nameserver in the resolv.conf file is used. Example:

resolv.conf contains router IP address 192.168.1.1

Enter 11.22.33.44 as IP address of cnn.com into hosts file and do a ping. Ping attempts to reach 11.22.33.44, instead of its correct IP address 157.166.226.25. Remove entry for cnn.com from hosts file and repeat: ping now tries to reach 157.166.226.25.

Case closed.

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: hosts file not being used?

Mon Jun 15, 2015 10:41 am

It's all controlled by /etc/nsswitch.conf

Code: Select all

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

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
so that means we search /etc/hosts first, then multicast DNS (minimal), then DNS (defined by /etc/resolv.conf), finally mulitcast DNS (full)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pi-dabbler
Posts: 8
Joined: Tue May 26, 2015 9:08 am

Re: hosts file not being used?

Tue Jun 16, 2015 11:34 am

Thank you Dougie.

Return to “Raspberry Pi OS”