localhost is wrong ip address

6,736

The problem turned out to be missing relevant entries in the /etc/nsswitch configuration file.

While resolving hostnames to IP address, the GNU C library libc uses this file to get the sources and the order in which will be queried. The database name of /etc/nsswitch.conf involved here is hosts. There are also many other databases understood by libc e.g. passwd, protocols etc.

The databases are mentioned at the start of the line followed by the name of the sources to be queried. For example the required configuration for hosts database (used for finding IP address of a host) can take the form :

hosts:          files mdns4 dns
  • files indicates that at first the relevant configuration files in /etc directory to be queried first, hence /etc/hosts is queried first

  • Then mDNS (Multi-cast DNS) is used up to check if the IP can be retrieved

  • Then DNS i.e. the name servers are queried for the IP address.

As your /etc/nsswitch configuration file does not contain anything, i would suggest you to put the default entries to the file :

passwd:         compat
group:          compat
shadow:         compat

hosts: files mdns4 mdns4_minimal [NOTFOUND=return] dns

networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

Check man 5 nsswitch.conf to get more idea.

Share:
6,736

Related videos on Youtube

Trashman
Author by

Trashman

Formerly a Trashman, currently an electrical engineer.

Updated on September 18, 2022

Comments

  • Trashman
    Trashman over 1 year

    I'm having a bizarre problem. It started with postgresql-9.3 not installing. I found it couldn't find localhost.

    I checked /etc/hosts and it has localhost properly as 127.0.0.1

    But when I

    ping localhost
    

    I get

    PING localhost.Home (198.105.244.21) 56(84) bytes of data.
    

    when I

    whois 198.105.244.21
    

    I get "SearchGuide Inc" in Colorado

    How do I get my system to properly recognize localhost?

    I'm running trusty tahr 14.04.2 lts

    Here is my /etc/hosts file:

    127.0.0.1   localhost
    127.0.0.1   ourcomp
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     localhost ip6-localhost ip6-loopback
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    

    I had a typo earlier. It is now corrected.

    Here is the output of nslookup localhost:

    nslookup localhost
    Server:     127.0.1.1
    Address:    127.0.1.1#53
    
    Non-authoritative answer:
    Name:   localhost.Home
    Address: 198.105.244.21
    Name:   localhost.Home
    Address: 198.105.254.21
    

    Output of iptables -L:

    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination         
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination  
    

    ufw status is inactive

    /etc/nsswitch is blank

    contents of /etc/resolv.conf:

    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    nameserver 127.0.1.1
    search Home
    

    ifconfig:

    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:46904 errors:0 dropped:0 overruns:0 frame:0
              TX packets:46904 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:2538766 (2.5 MB)  TX bytes:2538766 (2.5 MB)
    
    p5p1      Link encap:Ethernet  HWaddr f0:4d:a2:9b:3b:b9  
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    wlan0     Link encap:Ethernet  HWaddr 00:1b:b1:81:06:3d  
              inet addr:192.168.0.14  Bcast:192.168.0.255  Mask:255.255.255.0
              inet6 addr: fe80::21b:b1ff:fe81:63d/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:8968 errors:0 dropped:0 overruns:0 frame:0
              TX packets:7779 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:4212369 (4.2 MB)  TX bytes:1142565 (1.1 MB)
    
    • heemayl
      heemayl almost 9 years
      127.0.0.0 is the network address, localhost should be 127.0.0.1 (other IPs of this subnet is allowed although not prefarable)..could you please edit your question and add the output of cat /etc/hosts ?
    • heemayl
      heemayl almost 9 years
      Do you have iptables or ufw or any other means of forwarding mechanisms configured ?
    • Trashman
      Trashman almost 9 years
      Not that I know of. That is, I didn't purposely configure them, but I don't know if something else I installed did.
    • steeldriver
      steeldriver almost 9 years
      localhost.Home looks like an mDNS hostname: please add the contents of your /etc/nsswitch.conf and /etc/resolv.conf files, and output of ifconfig
    • heemayl
      heemayl almost 9 years
      Run the command echo 'hosts: files mdns4 mdns4_minimal [NOTFOUND=return] dns' | sudo tee -a /etc/nsswitch.conf and then check..
    • Trashman
      Trashman almost 9 years
      Not sure what you were going for, heemayl. That just added that literal string to nsswitch.conf. Should I not use the single quotes?
    • heemayl
      heemayl almost 9 years
      no quoting, just add the string to /etc/nsswitch.conf..
    • steeldriver
      steeldriver almost 9 years
      @heemayl I think the networking service may need to be restarted (or a reboot) for changes in nsswitch.conf to take effect?
    • Trashman
      Trashman almost 9 years
      @heemayl, thank you for that. After doing that and restarting, my system is behaving correctly. Two things are still bugging me, though. I still have these localhost.Home entries of 198.105.244.21 when I run nslookup. Why is that? What bugs me is this seems like a "band-aid" that may be masking something else. What exactly did that change do?
    • heemayl
      heemayl almost 9 years
      ok..remove dns (the last word) from the line we just added and then check..this is temporary for the purpose of checking..now use nslookup ..i think this is being resolved by your DNS, this will confirm us..
    • Trashman
      Trashman almost 9 years
      @heemayl removing dns broke my internet connection, but did not change the output of nslookup localhost, it still shows the localhost.home entries with the same IPs
    • heemayl
      heemayl almost 9 years
      @Trashman Well then..it was due to mdns....remove the mdns4 entries and check to be sure of it..