WittCode💻

CNAME Records Explained

By

Learn what a canonical name record (CNAME record) is, its relation to DNS, what an A record is, how to use the linux dig command, and how a CNAME record is different from an A record.

Table of Contents 📖

What is a CNAME Record?

A CNAME record, or canonical name record, is a record type in the domain name system that serves as an alias for another domain name. In simpler terms, a CNAME record maps one domain name to another. CNAME records are commonly used when working with subdomains.

CNAME Record Example

An example scenario of using a CNAME record could be wanting to point the subdomain blog.wittcode.com to the domain wittcode.com. Specifically, if wittcode.com is located at the IP address 192.168.0.1, then we could have a CNAME record that maps blog.wittcode.com to wittcode.com. Now, blog.wittcode.com and wittcode.com would be sharing the same IP address.

Image

CNAME Record vs A Record

An A record is not the same as a CNAME record. An A record is a type of DNS record that maps a domain name to an IP address while a CNAME record is a type of DNS record that maps one domain name to another.

DNS CNAME and dig Command

We can obtain information about a DNS name server by using the linux dig command. The dig command stands for domain information groper and it is used for querying DNS. If we want to get the CNAME for a particular domain, we use the command dig CNAME. For example, if we want to get the CNAME for blog.wittcode.com we can use the command dig blog.wittcode.com CNAME.

dig blog.wittcode.com CNAME

CNAME Records and DNS Lookup

A downside of CNAME records is that they require multiple DNS lookups. For example, lets say the domain name wittcode.com points to the IP address 192.168.0.1 and the CNAME record blog.wittcode.com points to wittcode.com. In this case, the DNS server will have to do two DNS lookups when accessing blog.wittcode.com. The first is for the CNAME record (blog.wittcode.com pointing to wittcode.com) and then another for the A record (wittcode.com pointing to 192.168.0.1).