DNS Hostname-to-IP Mapping: Can Multiple Hostnames Resolve to Same IP and Vice Versa?


1 views

In DNS architecture, hostnames and IP addresses don't always maintain a one-to-one relationship. The Domain Name System allows flexible mappings through various record types.

This is absolutely possible and commonly implemented using:

# Example Apache VirtualHost configuration

    ServerName example1.com
    ServerAlias example2.com
    DocumentRoot /var/www/shared

Practical use cases include:

  • Web hosting services running multiple websites on single server
  • Microservices architecture with multiple service names
  • Load balancers serving identical content under different domains

DNS A/AAAA records can contain multiple IP addresses:

; Example DNS zone file entry
www    IN    A    192.0.2.1
www    IN    A    192.0.2.2
www    IN    A    192.0.2.3

Implementation scenarios:

  • Basic load distribution without dedicated load balancer
  • Geographical DNS resolution (GeoDNS)
  • High availability failover systems

When implementing these configurations:

// Node.js example checking DNS resolution
const dns = require('dns');

dns.resolve4('example.com', (err, addresses) => {
    if (err) throw err;
    console.log(Resolved addresses: ${addresses});
});

Key considerations:

  • SSL/TLS certificates must cover all hostnames (use SAN certificates)
  • Session persistence challenges in load-balanced environments
  • DNS TTL values significantly impact change propagation

For cloud deployments, understand these record types:

# AWS Route53 example with ALIAS
resource "aws_route53_record" "www" {
    zone_id = aws_route53_zone.primary.zone_id
    name    = "www.example.com"
    type    = "A"
    alias {
        name                   = "d123.cloudfront.net"
        zone_id                = "Z2FDTNDATAQYW2"
        evaluate_target_health = true
    }
}

In DNS (Domain Name System), it's absolutely possible for multiple hostnames to resolve to the same IP address. This is a common practice known as DNS aliasing or virtual hosting. For example:

# Example DNS records
www.example.com.    IN  A     192.0.2.1
shop.example.com.   IN  A     192.0.2.1
blog.example.com.   IN  A     192.0.2.1

When multiple hostnames point to the same IP, web servers use the Host header to distinguish between sites. Here's a basic Nginx configuration example:

server {
    listen 80;
    server_name www.example.com;
    root /var/www/example;
}

server {
    listen 80;
    server_name shop.example.com;
    root /var/www/shop;
}

The reverse scenario is also possible - a single hostname resolving to multiple IP addresses. This is often used for load balancing:

# Round Robin DNS example
example.com.    IN  A     192.0.2.1
example.com.    IN  A     192.0.2.2
example.com.    IN  A     192.0.2.3

When coding applications that deal with hostnames and IPs, consider these scenarios:

// Python example to resolve hostnames
import socket

hostnames = ["www.example.com", "shop.example.com"]
for host in hostnames:
    print(f"{host}: {socket.gethostbyname(host)}")

# Output might show same IP for different hosts

Remember that DNS responses have TTL (Time To Live) values affecting how long clients cache the IP mappings. This impacts both shared IP and multiple IP scenarios.

# Checking DNS TTL with dig
dig +nocmd +noall +answer +ttlid www.example.com

Shared IP scenarios can affect SSL/TLS certificate validation and SNI (Server Name Indication) requirements in modern HTTPS connections.