35
votes

Can anyone explain whether it is possible for two hostnames to share the same IP address?

And what about if one hostname represents more than one IP address, is that possible too? Why?

2
  • 18
    Want a real world example where it happens? Ping serverfault.com and superuser.com and look at the IP address that is returned for both. Jun 21, 2013 at 16:32
  • 1
    All hostnames already share the 127.0.0.1 address, sort of ...
    – jlliagre
    Jun 24, 2013 at 23:01

7 Answers 7

45
votes

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.

5
  • 7
    Isn't the question asking the reverse of this?
    – mowwwalker
    Jun 21, 2013 at 17:33
  • 2
    There are two questions. @Sirch already covered the first part pretty good Jun 21, 2013 at 17:38
  • Oh, I see, I skipped over the description. Thank you for the answer btw!
    – mowwwalker
    Jun 21, 2013 at 17:49
  • 1
    Do browsers/network stacks typically try the first address received? Or will they try a random one? Would it make sense to randomize the order IPs are returned by your DNS server to try to balance the load? Jun 21, 2013 at 19:12
  • Depends on the specific client implementation. Most servers will randomize the order of returned IP addresses (this is the behavior referred to as "round-robin"). Interesting questions actually, I think I'll expand on the answer a bit Jun 21, 2013 at 19:20
35
votes

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.

6
  • 6
    It is worth noting, and I've seen on the real Internet, chains of CNAMEs that broke name resolvers. Because a CNAME can point to another CNAME. Even CNAME loops are possible.
    – PP.
    Jun 21, 2013 at 11:24
  • 2
    RFC 1912 2.4 ietf.org/rfc/rfc1912.txt
    – dmourati
    Jun 21, 2013 at 17:08
  • It's also worth pointing out that A records containing the same IP can appear in different zones, e.g., x.foo.com. A 1.2.3.4 and y.bar.com. A 1.2.3.4.
    – Blrfl
    Jun 23, 2013 at 12:58
  • This answer forgets to tell about wildcard A records, which also lead to multiple hostnames on the same IP.
    – Izzy
    Jun 24, 2013 at 22:44
  • Hey Sirch, What's the deal with the fullstops?
    – JonoRR
    Oct 20, 2014 at 10:12
6
votes

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.

2
votes

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.

2
votes

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.

1
vote

Multiple IP for a same Domain:

  • Yes, it is possible and very common: If a server (with ip A) fall down, you may connect to the next IP of the DNS register and access an another server (with ip B) to get the service.

Multiple Domain for a same IP:

You need to answer the next question: Are all Domain providing the same service?

  • If yes: it is a very common configuration also: lot of companies buy lot of domain with different country top domain: xxx.com, xxx.net, xxx.org, etc. And all of them point to the same service, that is, to the same list of IP.
  • If no: It is possible but not common and not recomended. Some hosting companies that only allow HTTP service use NAT/Proxy to spare IP. But of course, that mean a Proxy that "understand" HTTP to identivy the destination service.
0
votes

Many servers in telecom(like HSS and PCRF) use SCTP transport and so these servers have multiple IP addresses. These addresses are used for multi-homing(which SCTP supports) providing the connection with redundancy and failover.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .