Is it possible for two hostnames share the same IP address?

Solution 1:

Assigning more than one IP address to one hostname is also possible:

rr.example.com.        A      192.0.2.12
rr.example.com.        A      192.0.2.23
rr.example.com.        A      192.0.2.34
rr.example.com.        A      192.0.2.45

When you query a DNS server for rr.example.com you'll get back a list of IP addresses back. You can then choose to connect to one of them. Should the first attempt to connect get actively refused, just try the next.

Most browser will follow this flow, as long as the endpoints actively refuse TCP connectivity. Should an endpoint timeout, the ressource will be treated as unreachable even though not all IP's has been tried

Since most applications (browsers included) are often only interested in 1 IP endpoint at a time and just choose the first available answer, you risk skewing the load between the target servers so that the first server gets all the traffic while the others might be idle.

To circumvent this, most DNS servers offer what is known as a Round Robin configuration, making the server alternate the order in which equally matching records are returned. Before load balancers were commonplace, this was an efficient way to load balance and somewhat implement fault-tolerance on network systems.

Solution 2:

Yes, it is possible for multiple hostnames to use the same ip address, the best practise is to use a CNAME record to point to the A record

bar.example.com.        CNAME  foo.example.com.
foo.example.com.        A      192.0.2.23

Take note of all the full stops.

Having one hostname to represent multiple ip addresses is a little more complicated. If we are talking about MX records, this solution already exists in DNS using priority numbers, if you want it to represent multiple A records, youd better to use a load balancer, HAProxy for example.


Solution 3:

In addition to CNAME change as other answers suggested, you also have to handle the logic on your hosting server. I use Apache and I configured it as:

<VirtualHost 1.2.3.4:80>
    ServerName  www.abc.com
    ServerAlias abc.com
    ...
</VirtualHost>

<VirtualHost 1.2.3.4:80>
    ServerName  www.xyz.com
    ServerAlias xyz.com
    ...
</VirtualHost>

I'm sure other http server softwares have similar things.


Solution 4:

You need to be clear on what you mean by two hostnames. If you mean two physical boxes with the same IP address the answer is typically no. A case where you would is if serverA and serverB are working as an active-passive cluster then you would have each server have two addresses a piece. One would be an ip address dedicated to that server and the second ip address would be one which was shared between the servers but only the active server will be listening to that shared address. The passive server only starts listening in the shared address when the active server goes down.


Solution 5:

It is also worth noting that in IPv6 you can assign the same IP to two or more hosts and the network will do the load balancing and failover for you (if one is not reachable, try the other). Both are regarded as one logical endpoint and there is no DNS involved whatsoever.

This feature is known as Anycast.