Linux – How is priority determined in /etc/hosts

centoshostslinuxlinux-networkingnetworking

For reasons too specific to detail here… I have tried to add two entries for a domain to the /etc/hosts file on a CentOS-7 server. In order for one piece of software to run happily, there needs to be an entry like:

192.168.1.z  otherserver

however, I actually want that domain to resolve to a different IP:

10.w.x.y     otherserver

My understanding (backed up by similar questions eg Assign multiple IPs to 1 Entry in hosts file) is that the first matching entry will be used.

So this file:

192.168.1.z  otherserver
10.w.x.y     otherserver

should lead to the domain resolving as 192.168.1.z; whereas this file:

10.w.x.y     otherserver
192.168.1.z  otherserver

should lead to it resolving as 10.w.x.y.

However, no matter what ordering I use (and however many duplicate entries I add) the domain always resolves as 192.168.1.z unless I remove that entry completely.

So is there any way to include both lines but ensure that 10.w.x.y takes precedence?

Best Answer

Top to bottom. That's usually it.

You may want to also examine your /etc/nsswitch.conf

Other services may use a different method of DNS lookup. What application is this?

Also, check for caching daemons. Is nscd running?