-2

From https://serverfault.com/a/953384/16981

Host names do not correspond to an {ipaddress,port} tuple. A host name is only the name of a server, which should be resolvable to one or more IP addresses. Ports have nothing to do with host names at all.

My questions are:

(1) I only know (and never assume I actually know) a hostname is resolved to no more than one IP address. So what is it like that a host name is resolvable to more than one IP addresses?

(2) Is a host name specified with different ports resolved to the same IP address? Can a host name specified with different ports invoke different servers?

Thanks.

Rhangaun
  • 189
  • 1
  • 15
Tim
  • 1,487
  • 6
  • 28
  • 43

3 Answers3

9

1) A hostname can be resolved to more than one IP address; this is called DNS round robin and can be used for load distribution, load balancing, or fault tolerance.

2) Host names are not in any way related to ports, they only map to one or more IP addresses.

Massimo
  • 70,200
  • 57
  • 200
  • 323
  • Thanks. Can multiple hostnames be resolved to the same IP address? Is name based virtual hosting such an example? – Tim Feb 11 '19 at 21:47
  • "A hostname can be resolved to more than one IP address". Do you mean a host name can be resolved to more than one IP addresses at the same time? – Tim Feb 11 '19 at 22:16
  • @Tim yes to both. Multiple IP to one Host is a basic form of loadbalancing called [Round Robin DNS](https://en.wikipedia.org/wiki/Round-robin_DNS). Mutiple Host to one IP is incredibly common and underpins [name-based vitual hosting](https://en.wikipedia.org/wiki/Virtual_hosting#Name-based). – Boris the Spider Feb 12 '19 at 07:17
  • @Boris By "Round Robin DNS", do you mean a host name can be resolved to more than one IP addresses at the **same** time? – Tim Feb 12 '19 at 14:04
  • @Tim Yes, a single hostname may resolve to many IP addresses. The site you're on has four when I do a `dig serverfault.com`. – ceejayoz Feb 12 '19 at 16:25
4

An example is the DNS name service provided by Cloudfire.

$ host one.one.one.one
one.one.one.one has address 1.1.1.1
one.one.one.one has address 1.0.0.1
one.one.one.one has IPv6 address 2606:4700:4700::1111
one.one.one.one has IPv6 address 2606:4700:4700::1001

If you try connecting to the DNS host name, you will connect to one or the other of the IPv4 or IPv6 addresses, generally alternating between the two addresses.

Ports are completely unrelated to IP addresses. You can set up something, say in iptables in Linux, to forward a specific port onto a different server but this is unusual.

doneal24
  • 851
  • 6
  • 14
  • Thanks. So a hostname can be resolved to more than one IP address Does that mean a host name can be resolved to more than one IP addresses at the same time? – Tim Feb 11 '19 at 22:23
  • @Tim Yes, and that is exactly what was demonstrated here. – Michael Hampton Feb 12 '19 at 16:22
  • @MichaelHampton Round robin is not at the same time. Consider CPU scheduling processes in round robin fashion, no two processes can run on the CPU at the same time, and the assignment is dynamically changed but always just one process is assigned to the CPU at a time, and that is called "concurrency". – Tim Feb 12 '19 at 16:39
  • 1
    @Tim You seem to be conflating the resolution of the hostname to IP address with actually making a connection to an IP address. These are separate operations. – Michael Hampton Feb 12 '19 at 16:41
  • @MichaelHampton I am not. No hostname can be resolved to two IP addresses at the same time. The resolution is changed dynamically though. – Tim Feb 12 '19 at 16:41
  • @Tim Eh? Now I'm certain that one of us is confused. We can see here (and in practice all the time) that this hostname resolved to four IP addresses at the same time. What are you talking about? – Michael Hampton Feb 12 '19 at 16:42
  • 1
    @Tim obviously a hostname can resolve to multiple IPs, that's literally how round robin DNS works. Depending on the application protocol, the client will typically choose only one of those IPs to connect to, although I'm sure there's some oddball protocol that could connect to multiple IPs. – mfinni Feb 12 '19 at 17:50
  • 1
    @Tim The example I gave shows that the host has two A records and two AAAA records. The `host` command (or nslookup, dig, etc.) will show that the DNS name resolves to four IP addresses at the same time. There is no preference between one A record and the other A record. It's up to the application to choose how it's going to connect. – doneal24 Feb 12 '19 at 18:14
3

One way to look at DNS is as a large distributed key-value store.

  • The key is made up of a domain name (e.g. www.google.com) and a record type (e.g. A for "Address").
  • The value is basically a string, whose meaning depends on the record type.
  • Each key can have multiple values. Again, what this means depends on the record type.

When you want to make an HTTP request to serverfault.com, the first thing you need to do is look up the A record for that domain. So you query the DNS system to find out the value of that key, and get back an address, like 216.58.206.132.

Then, you can make a connection to that address, on whatever port you like. This might be a port that the user has specified (e.g. 8080 for the URL http://www.google.com:8080/) or a default that you expect to work (80 for HTTP, 443 for HTTPS).

In the case of A records, and HTTP, there is no facility for DNS to tell you which port to use. Other record types (e.g. SRV) may specify more information, including both an address and a port number, or even a bunch of configuration values you need to use to successfully talk to the server.

If you actually query the A record for serverfault.com, what you'll actually get back is a list of answers:

;; QUESTION SECTION:
;serverfault.com.               IN      A

;; ANSWER SECTION:
serverfault.com.        174     IN      A       151.101.129.69
serverfault.com.        174     IN      A       151.101.193.69
serverfault.com.        174     IN      A       151.101.1.69
serverfault.com.        174     IN      A       151.101.65.69

Now, we only need one address to connect to a web server, so we interpret this to mean "pick any of these addresses". Generally, everybody will pick the first one, so the DNS server will change the order each time you ask; that way, load will be spread between the different addresses. This is what is meant by "round-robin DNS".

An important disclaimer here is that DNS isn't actually like a database: when we query a DNS server, it can make up an answer however it likes, just like when we query a web server for a URL. For instance, if I look up the address for google.com, I get this:

;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             25      IN      A       216.58.206.78

That doesn't mean that there's only one IP address that serves everyone who wants to connect to google.com; instead, it means that the server in charge of that domain has decided that that's the IP address it's going to give me, which is different from the address it will give you.

IMSoP
  • 490
  • 2
  • 10