At the beginning of working with an online resource, everyone is faced with DNS records. We have already talked about how the Domain Name System works, but today we are going to discuss one of the records - CNAME (canonical name) and the purpose of CNAME.
What is a CNAME record?
A CNAME record, or canonical name (cname) record, is a DNS record used to map one domain name to another. In other words, canonical names and aliases.
The CNAME record format:
mail.myfirstwebsite.com CNAME myfirstwebsite.com
The answer to a canonical name query might look like this:
mail.myfirstwebsite.com canonical name = myfirstwebsite.com
Canonical name in DNS can be presented as follows (canonical name examples):
Domain (subdomain) / Host
CNAME makes it easy to use multiple services (for instance, ftp or mail) from the same IP address. If you change the IP, you need to set up only the A record.
DNAME vs CNAME vs ANAME
There are also ANAME and DNAME records. ANAME, also known as ALIAS, is an alternative to CNAME with its own advantages. DNAME record provides redirection from one part of the DNS name tree to another (for example, to redirect traffic from the old myfirstwebsite.com to the new myfirstwebsite.org).
How does CNAME work?
When you enter mail.mywebsite.com into the address bar, the DNS server starts searching for the A record. After the CNAME record is found, the query is restarted and the canonical domain name (myfirstwebsite.com) is used instead of the original one (mail.myfirstwebsite.com.). This happens because mail.myfirstwebsite.com is a subdomain of myfirstwebsite.com. As a result, the required IP address will be found through the A record of myfirstwebsite.com.
The query can be set to search for CNAME records first. In this case, the response will be the canonical name (cname), not the IP.
What is CNAME record used for?
Use cases of the CNAME (canonical name) are not limited by this list. CNAME record used for various tasks:
- to specify multiple domain names (for example, multiple sites) that belong to the same company and must be pointed to the main website;
- to use different network services and to indicate the root domain;
- to map each foreign version of the site to the main (canonical) domain;
- to set up a CDN (Content Delivery Networks) with the CNAME record for the main server.
How to set CNAME record with restrictions?
When writing the CNAME record, you should consider the following restrictions:
- The CNAME record cannot be created for the main domain, as the main domain (the canonical name itself) must be mapped to an IP. Instead, you need to create an A record;
- CNAME records must be pointed to the canonical domain, not to the IP;
- If you write a CNAME record for a particular subdomain, there must be no other resource records. The exceptions are RRSIG and NSEC;
- CNAME records mapping to other records of the same type are not recommended due to inefficiency and unresolvable loops;
- MX and NS records must never point to a CNAME;
- Domains used in SMTP MAIL and RCPT protocol commands may not have a CNAME record. Existence of this record may cause services to work incorrectly.
Let's create a canonical name (cname) record:
- Go to the host site, to the DNS management console, and then to all records.
- If you have several domains, choose the one that you want to make a CNAME record for (myfirstwebsite.com).
- In the list of records, select the CNAME type.
- Enter the subdomain in the Host field (mail.myfirstwebsite.com.).
- Fill in the TTL (Time to Live) field, which specifies how long the record will be kept in the cache of systems that request it. You may leave this parameter by default, however it is recommended to set 1800 or higher for static IPs and 1800 or less for dynamic IPs.
- Save changes.
We remind you, it can take up to 72 hours for new records to work.
How to check CNAME online
On the Internet, you can find services called CNAME Lookup. These services, after entering the domain, will provide a list of CNAME records in order of priority.
Some tools allow you to see not only CNAME, but also other records:
Another way is to use the nslookup application, which is available on Windows, MacOS, and Linux.
So, to check the CNAME on MacOS or Linux, start a terminal. On Windows, you need to run the command line (Start → Run, type cmd and press Enter).
Then enter the command with the desired subdomain:
nslookup -q=cname mail.myfirstwebsite.com
The app will give you information:
mail.myfirstwebsite.com canonical name = myfirstwebsite.com.
That is, mail.myfirstwebsite.com points to the canonical name (cname) myfirstwebsite.com.
A record vs CNAME vs ALIAS record vs Redirect
In the domain name system, CNAME, A, and ALIAS records may look similar, but you should know their key differences. Owners of websites or other online projects are faced with each type of record during the initial work with domains.
The difference between A record and CNAME record is that A record is used to map a root domain to an IP address. This is the main difference because in the CNAME record, you can only map one domain (mail.myfirstwebsite.com) to another (myfirstwebsite.com.). Subdomains with CNAME records cannot have A records.
Just like the CNAME, the ALIAS maps one domain to another. However, CNAME doesn't allow adding other resource records for these domains. But with ALIAS, it is possible. Also, an ALIAS query immediately returns an IP address, without mediating with another domain name. This reduces the processing time for the query.
Redirect and CNAME are not directly related. To create a redirect, you need to configure the server so that it will give a specific response for a specific HTTP request. CNAME cannot guarantee it.