Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can /etc/hosts config reverse resolution?

Tags:

dns

hosts

As we all known, we can add 'ip host' item in /etc/hosts to mock a DNS's name resolution, now comes the question, can I use /etc/hosts to do inverse resolution, form ip to hostname? Or is there any other handy way to do this? Thanks!

like image 446
小武哥 Avatar asked Nov 13 '12 06:11

小武哥


People also ask

Does hosts file override DNS?

The hosts file on your computer allows you to override DNS and manually map hostnames (domains) to IP addresses. This can come in handy during migrations as you might want to see how your website looks on a different server, but perhaps the DNS hasn't propagated yet.

How do I bypass DNS resolution?

You can bypass DNS lookups by adding an entry to the hosts file. Where 192.0. 2.6 and www.example.com should be replaced by the new server's IP address and the hostname respectively.

Does order matter in etc hosts?

order doesn't matter except to the humans reading it. Sometimes I order mine in numerical order of the IPs and sometimes I sort it alphabetically by host name. The system doesn't care. The only time order matters is if you have conflicting information in the file.

What is the difference between resolv conf and etc hosts file?

resolv. conf specifies nameservers in order of search preference. hosts overrides all nameservers by mapping urls/shortnames to IPs.


2 Answers

Maybe. It will depend on the tool you use to do the lookup and the configuration of resolving on your computer.

For example gethostbyaddr() will check /etc/hosts if "files" is in the hosts section of your /etc/nsswitch.conf

Note however that not all tools will do a local resolve, such as the "host" command for example, so it depends entirely on how you are attempting to do the lookup.

like image 136
Tor Magnus Avatar answered Oct 14 '22 09:10

Tor Magnus


Yes. It does that automatically if the application uses Name Service Switch libraries (most applications do), and if /etc/nsswitch.conf is configured to resolve IPs from /etc/hosts with a line such as this:

hosts:          files dns

You can test the reverse name resolution with either of the options below:

getent hosts 127.0.0.1

or

resolveip 127.0.0.1
like image 31
Pcgomes Avatar answered Oct 14 '22 09:10

Pcgomes