CNAME Record
A Canonical Name (CNAME) record is a type of DNS resource record that maps one domain name (an alias) to another domain name (the canonical name). When a DNS resolver queries a domain with a CNAME record, it effectively follows the alias to the target domain, then looks up the final IP address.
How it works
A CNAME record points from an alias domain to a 'canonical' domain. Instead of pointing directly to an IP address like an A record, a CNAME creates an alias that redirects to another domain name. This allows multiple hostnames to resolve to the same IP address by pointing them all to a single canonical name.
When a DNS resolver encounters a CNAME record, the resolution process follows these steps:
1. A DNS query is made for the alias domain (e.g.,
www.example.com)
2. The resolver finds a CNAME record pointing to the canonical name (e.g., example.com)
3. The resolver restarts the lookup for the canonical domain
4. It finds the A or AAAA record for the canonical domain
5. Finally, it returns the IP address to the client
In short: CNAME → Canonical Name → A (IPv4) or AAAA (IPv6) Record → IP Address. If a host's IP address changes, it is only necessary to update the DNS A record for the root domain. All CNAME records, including those of the alias or subdomains, will automatically change when the root domain's A record is updated.Key Points
- CNAME records must always point to another domain name, never directly to an IP address
- If a domain name has a CNAME record, it cannot have any other record types (A, MX, TXT, NS, SOA) at the same level
- CNAME records cannot be used for the root domain (@) - only for subdomains
- Each CNAME adds an extra DNS lookup step, which can slightly impact resolution time
- Chaining multiple CNAMEs (CNAME to CNAME) is technically possible but not recommended due to performance impact
Common Use Cases
- Subdomain Aliasing: Point
www.example.comandblog.example.comtoexample.com, which has the actual A record - Third-Party Service Integration: Point your subdomain to a third-party service like
shop.example.com→shops.shopify.com - CDN Configuration: Point your domain to a CDN endpoint (e.g.,
cdn.example.com→d111111abcdef8.cloudfront.net) - Centralized Management: Update multiple subdomains by changing only one canonical A record instead of updating each subdomain individually
code Example Configuration
| Type | Host / Name | Value / Points to | TTL |
|---|---|---|---|
| A | @ | 192.0.2.1 | 3600 |
| CNAME | www | example.com | 3600 |
| CNAME | blog | example.com | 3600 |
| CNAME | shop | shops.myshopify.com | 3600 |
* CNAME records create aliases that point to other domain names. The target domain must have an A or AAAA record.
Frequently Asked Questions
What is the difference between A and CNAME records?expand_more
An A record points a hostname directly to an IP address, while a CNAME record points a hostname to another hostname. A records provide direct IP mapping, whereas CNAMEs create aliases. Use A records for root domains and when you need to point directly to an IP. Use CNAMEs for subdomains when you want them to follow another domain's IP address.
Can I use a CNAME for my root domain?expand_more
No, CNAME records cannot be used for the root (apex) domain according to RFC 1912. The root domain must use A or AAAA records. This is because CNAME records are incompatible with other essential records (like SOA and NS) that must exist at the root level. You can only use CNAMEs for subdomains.
Can a CNAME point to another CNAME?expand_more
While technically possible, chaining CNAMEs (CNAME to CNAME) is strongly discouraged. Each hop in the chain adds an extra DNS lookup, increasing resolution time and potentially impacting performance. Most DNS providers and best practices recommend avoiding CNAME chains and pointing directly to the canonical domain.
Can I have other records alongside a CNAME?expand_more
No, if a hostname has a CNAME record, it cannot have any other record types (A, AAAA, MX, TXT, etc.) at the same level. This is a fundamental DNS rule (RFC 1034). If you need multiple record types for the same hostname, use an A or AAAA record instead of a CNAME.
When should I use CNAME instead of A record?expand_more
Use CNAME when:
- Pointing a subdomain to another domain (especially third-party services)
- You want changes to the target domain's IP to automatically apply to the alias
- Managing multiple subdomains that should all point to the same destination
Use A record when:
- Configuring the root domain
- You need the fastest possible DNS resolution
- You need other record types (MX, TXT) for the same hostname