35

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?

Ladadadada
  • 26,337
  • 7
  • 59
  • 90
jean
  • 385
  • 1
  • 3
  • 4

7 Answers7

45

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.

Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95
  • 7
    Isn't the question asking the reverse of this? – mowwwalker Jun 21 '13 at 17:33
  • 2
    There are two questions. @Sirch already covered the first part pretty good – Mathias R. Jessen Jun 21 '13 at 17:38
  • Oh, I see, I skipped over the description. Thank you for the answer btw! – mowwwalker Jun 21 '13 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? – Tom Marthenal Jun 21 '13 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 – Mathias R. Jessen Jun 21 '13 at 19:20
35

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.

Sirch
  • 5,785
  • 4
  • 20
  • 36
  • 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 '13 at 11:24
  • 2
    RFC 1912 2.4 http://www.ietf.org/rfc/rfc1912.txt – dmourati Jun 21 '13 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 '13 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 '13 at 22:44
  • Hey Sirch, What's the deal with the fullstops? – JonoRR Oct 20 '14 at 10:12
  • If you dont add the fullstops, the parent domain might need to be added to resolve. ie: bar.example.com.example.com. If you dont want to specify the period you can leave off example.com if its in the example.com domain, ie "foo A 192.0.2.23". – Sirch Oct 20 '14 at 12:30
6

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.

Yandong Liu
  • 161
  • 2
2

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.

Keith Wolters
  • 163
  • 1
  • 5
2

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.

scravy
  • 139
  • 3
1

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.
Adrian Maire
  • 145
  • 1
  • 10
0

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.