Linux command line utility to resolve host names using /etc/hosts first
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.
medmug
Updated on September 18, 2022Comments
-
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 almost 13 yearsCould you please explain your situation a little more? Does
awk '/hostname/ { print $1 }' /etc/hosts
help? -
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 over 12 yearsYes, but that would not fall back on DNS.
-
cjc over 12 yearsNo, it resolves it in nsswitch.conf order.
-
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 over 12 years@cjc, it does. I don't think I tried it correctly.
-
slowpoison over 12 years@Zoredache, I'm quite impressed with
getent
. Thanks for the intro to this command. -
Kyle Smith over 12 yearsThis will work, but it's been obsolete for a while. See linux.die.net/man/3/gethostbyname.
-
aseq over 12 yearsIt'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 over 12 yearsThank you, I did not know about it. ;)
-
user239558 over 9 yearsHave an upvote. No other semi-portable one-liner has been proposed.
-
craig65535 over 8 yearsIt's not broken, it just doesn't use getaddrinfo (which reads /etc/gai.conf). To use getaddrinfo, run
getent ahosts
. -
Sri over 8 yearsIn 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 over 6 yearsThis is the tool I use in my Alpine docker containers that have no other mechanism like
dig
ornslookup
. -
starbeamrainbowlabs about 4 yearsThis is the correct answer.