How network manager is resolving localhost without /etc/hosts

Bruce Ferrell bferrell at
Fri Dec 25 22:33:29 UTC 2015

On 12/25/2015 11:22 AM, Tom H wrote:
> On Mon, Dec 21, 2015 at 12:30 AM, Liam Proven <lproven at> wrote:
>> On 20 December 2015 at 21:57, Shashwat Kumar <shashwatkmr.001 at> wrote:
>>> I tried to make a new mapping in /etc/hosts which didn't work. After
>>> searching I found that network-manager does not use /etc/hosts but dnsmasq
>>> is used as local dns resolver.
>>> To confirm this design, I commented every line in /etc/hosts and flush
>>> dnsmasq cache using
>>> sudo /etc/init.d/nscd restart
>>> and restarted network-manager
>>> sudo service network-manager restart
>>> But to my surprise ping localhost was still resolving to As per
>>> my knowledge localhost mapping is only maintained in /etc/hosts. So how
>>> localhost was still being resolved? Is there any information that I am
>>> missing?
> I don't have access to Ubuntu+NM at the moment but, IIRC, it won't
> resolve localhost to if you set "dns=none" in
> "/etc/NetworkManager/NetworkManager.conf" and if "/etc/hosts" doesn't
> have a " localhost" line (even though this isn't really what
> "man NetworkManager.conf" says about "dns=none").
>> Localhost is *always* on any computer with IPv4. You can't
>> change this to anything else in any way at all, as far as I know; it's
>> part of the definition of IPv4.
> It's always because it's what's set in "/etc/hosts". If it
> isn't set there:
> # cat /etc/hosts
> rr
> # ping localhost
> ping: unknown host localhost
> # getent hosts localhost
> # getent ahosts localhost
> #
>> You do *not* set localhost to your network IP address! That will break
>> TCP/IP very badly, I think.
> Most definitely +1

Name resolution at it lowest level is a function of a thing called the resolver library... A part of glibc... Very low level systems library.  The resolver library is is configured
by a file, /etc/nsswitch.conf (name switch configuration).  This row means:

hosts:          files mdns_minimal [NOTFOUND=return] dns

Check for the known file, /etc/hosts.  If it's not there, look at the minimal mdns resolver library (on linux known as avahi usually, OS X bonjour).   If you don't find it there,
go ask DNS.

NetworkManager relies on this mechanism and ordering

More information about the ubuntu-users mailing list