Linux command line utility to resolve host names using /etc/hosts first

64,589

Solution 1

This is easily achieved with getent:

getent hosts 127.0.0.1

getent will do lookups for any type of data configured in nsswitch.conf.

Solution 2

One tool that would work is getent. So you could use getent hosts www.google.com, or getent hosts localhost. It will retrieve entries from the databases as specified in your Name Service Switch configuration /etc/nsswitch.conf.

For more modern implementations use getent ahosts www.google.com which will get multiple results.

Solution 3

Use getent ahosts, for instance:

$ getent ahosts www.google.com | sed -n 's/ *STREAM.*//p'
216.58.210.196
2a00:1450:4006:803::2004

You'll get all IPv4 and IPv6 addresses, via the glibc resolver (thus using /etc/hosts first, as usually configured in /etc/nsswitch.conf).

Do not use getent hosts, as it will give you either IPv6 or IPv4 addresses (not both), and the chosen protocol may not be one that does not work. Indeed, IPv6 addresses are generally preferred, but at some places, IPv6 data are filtered (not supported) by the routers.

Solution 4

You can use a gethostbyname() (deprecated) wrapper like:

python -c 'import socket;print socket.gethostbyname("www.google.com")'

Or a getaddrinfo() wrapper like:

python -c 'import socket;print socket.getaddrinfo("www.google.com","http")[0][4][0]'

For python3:

python -c 'import socket;print(socket.getaddrinfo("www.google.com","http")[0][4][0])'

Note that getaddrinfo will return all instances as a list. The last part of the command selects only the first tuple. This can also return IPv6 addresses.

Solution 5

You could use [your favorite language here] to write a script that calls getnameinfo. That is how binaries (like ping) should be doing it, so you're ensured you get the same treatment.

Share:
64,589
medmug
Author by

medmug

Updated on September 18, 2022

Comments

  • medmug
    medmug almost 2 years

    There are several command line utilities to resolve host names (host, dig, nslookup), however they all use nameservers exclusively, while applications in general look in /etc/hosts first (using gethostbyname I believe).

    Is there a command line utility to resolve host names that behaves like a usual application, thus looking in /etc/hosts first and only then asking a nameserver?

    (I am aware that it would probably be like 3 lines of c, but I need it inside of a somewhat portable shell script.)

    • Greg Petersen
      Greg Petersen almost 13 years
      Could you please explain your situation a little more? Does awk '/hostname/ { print $1 }' /etc/hosts help?
    • medmug
      medmug almost 13 years
      @quanta Actually the current solution is grep/sed magic on /etc/hosts. I wanted to make that more general with a fallback.
  • slowpoison
    slowpoison over 12 years
    Yes, but that would not fall back on DNS.
  • cjc
    cjc over 12 years
    No, it resolves it in nsswitch.conf order.
  • Zoredache
    Zoredache over 12 years
    @slowpoison, Take a look at your nsswitch config. My system has files dns for hosts, which means /etc/hosts is consulted and then the DNS resolver. Your config may be different.
  • slowpoison
    slowpoison over 12 years
    @cjc, it does. I don't think I tried it correctly.
  • slowpoison
    slowpoison over 12 years
    @Zoredache, I'm quite impressed with getent. Thanks for the intro to this command.
  • Kyle Smith
    Kyle Smith over 12 years
    This will work, but it's been obsolete for a while. See linux.die.net/man/3/gethostbyname.
  • aseq
    aseq over 12 years
    It's a helpful tool, but why use it to do a lookup in the /etc/hosts file which has been deprecated since about 2 decades, give or take? Thank goodness tools such as ping ignore the hosts file. There are other ways to accomplish these things, such as adding your own custom zone files to your nameserver.
  • Mircea Vutcovici
    Mircea Vutcovici over 12 years
    Thank you, I did not know about it. ;)
  • user239558
    user239558 over 9 years
    Have an upvote. No other semi-portable one-liner has been proposed.
  • craig65535
    craig65535 over 8 years
    It's not broken, it just doesn't use getaddrinfo (which reads /etc/gai.conf). To use getaddrinfo, run getent ahosts.
  • Sri
    Sri over 8 years
    In case this is of value to anyone else, I made a Python 3 version with a few command-line options: github.com/acdha/unix_tools/blob/master/bin/getaddrinfo
  • Bruno Bronosky
    Bruno Bronosky over 6 years
    This is the tool I use in my Alpine docker containers that have no other mechanism like dig or nslookup.
  • starbeamrainbowlabs
    starbeamrainbowlabs about 4 years
    This is the correct answer.