zaro

What does a CNAME do?

Published in DNS Record 3 mins read

A CNAME record in DNS maps an alias name to a canonical (true) domain name. Essentially, it creates a shortcut, pointing one domain name or subdomain to another.

Understanding CNAME Records

CNAME records are crucial for managing complex domain structures. They help in various ways:

  • Pointing Subdomains: A common use case is directing subdomains like www.example.com or mail.example.com to the main domain example.com where the actual website or email server is hosted. This makes configuration and updates simpler.
  • Managing Services: When you use third-party services, they often provide CNAME records to point your subdomain (e.g., blog.example.com) to their servers, ensuring seamless functionality of their service with your domain.
  • Dynamic IP Changes: CNAME records are particularly useful when the server's IP address changes, because you don't need to modify the CNAME record itself. Instead, the canonical domain's A record is updated.

How CNAMEs Work

Let's break down how CNAMEs function:

  1. DNS Query: When a user types www.example.com into their browser, the DNS (Domain Name System) resolver begins to search for its IP address.
  2. CNAME Lookup: The resolver will encounter a CNAME record for www.example.com, which specifies the actual canonical name, for instance, example.com.
  3. A Record Lookup: The resolver then looks up the A record (which maps a domain to an IP address) for example.com.
  4. IP Address: The A record provides the IP address, which enables the user's browser to connect to the website's server.

Benefits of Using CNAME Records

  • Reduced Updates: When a server's IP address changes, you only need to update the A record for the canonical domain, without needing to change multiple aliases.
  • Simplified Configuration: CNAMEs help you easily manage domain structures by linking aliases to the correct location.
  • Enhanced Flexibility: They provide the flexibility to change how your domains and subdomains are hosted, and the canonical name can be modified without reconfiguring all the CNAME records.

CNAME vs. A Records

While both CNAME and A records map names to addresses, their functions are distinct.

Feature CNAME Record A Record
Purpose Maps an alias to another name Maps a name to an IP address
Target Canonical domain name IP address
Use case Aliases, pointing to the host Direct mapping to a server
Redirection Indirect, through another name Direct

Example

Suppose example.com is hosted on a server at the IP address 192.0.2.1.

  • The DNS record for example.com is an A record:
    example.com. IN A 192.0.2.1
  • www.example.com is set up as a CNAME record:
    www.example.com. IN CNAME example.com.
    • Any query for www.example.com is re-routed to example.com which then provides the IP address.

Using CNAME in this manner is far more efficient than adding an A record to every alias such as www.example.com.