小武哥
小武哥

Reputation: 446

Can /etc/hosts config reverse resolution?

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!

Upvotes: 19

Views: 44579

Answers (4)

Pcgomes
Pcgomes

Reputation: 302

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

Upvotes: 3

user3881379
user3881379

Reputation: 1

Yes you can. If you use dnsmasq, you can interfere in a number of ways to get a forward lookup going to 127.0.0.1 and the reverse lookup from 127.0.0.1 going to your host. For example, if your hostname is host1.mydomain.com with a real IP address of 192.168.1.12, then you can get 127.0.0.1 to resolve to it by doing the following in the dnsmasq configuration file: host-record=host1.mydomain.com,127.0.0.1

The forward interference can be done in many ways, here is one: alias=192.168.1.12,127.0.0.1

Obviously you need to set up the rest of dnsmasq properly to forward to your real DNS server ... but that is simple enough

Upvotes: 0

Tor Magnus
Tor Magnus

Reputation: 341

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.

Upvotes: 34

mwangi
mwangi

Reputation: 1626

No. That can only be done on a DNS server.

Upvotes: 2

Related Questions