Is it possible to give two IP addresses for the same host name?

I want to give two ip addresses for same host name.
Like in /etc/hosts file:

xx.x.x.x example # if system is connected to dhcp
y.y.yy.y example # if system is connected to VPN 

So is it possible?

2 Answers

It's not really possible as in this specific example. Your hosts file essentially forms part of your system's ability to map a hostname into an IP address. So when your system wants to know the IP address of "example", it'll get a single IP address in reply. The hosts file allows no way of distinguishing different "record types" for its resolution. So for hostname to IP address mapping, you only get one IP address per hostname (but multiple hostnames could resolve to the same IP address, which is the opposite).

Depending on what you're doing, might it be possible to use an external DNS service like dyndns.org to map a constant hostname, that will work on either network, to your IP address?

Strictly, the above answer is incorrect.

In older code, using gethostbyname() would indeed return only one result.

In newer code, using getaddrinfo() will return all results, and the /etc/hosts file in the question will work as wanted.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like