Creating and using a CNAME record in DNS

Now that we are familiar with moving around a little bit inside the DNS management tool, we are going to create and test another type of record. This one is called a CNAME (which is short for canonical name), and it is easiest to think of this one as an alias record. Rather than taking a DNS name and pointing it at an IP address as we do with a host record, with a CNAME, we are going to take a DNS name and point it at another DNS name! Why would this be necessary? If you are hosting multiple services on a single server but want those services to be contacted by using different names, CNAME records can be your best friend. Some corporate naming schemes are also very unfriendly to being used by users (imagine getting your users to remember that W19UE1WEB7 is the payroll server), and a CNAME allows us to assign a more memorable name for users to use.

Getting ready

We are going to make use of the same environment that we used to create our A records in the Creating an A or AAAA record in DNS recipe. There is a DC/DNS server online where we are going to create our records. We will also need WEB01 running, a server where we are hosting a website, as well as some file shares. We will also use a Windows 10 client to test out our CNAME records after they have been created.

How to do it…

To create and test a CNAME record, perform the following steps:

  1. WEB01 is hosting a website and a file share. Currently, the only DNS record that exists for WEB01 is the primary A record, so users have to type in the WEB01 name to access both the website and the file shares. Our goal is to create aliases for these services by using CNAME records in DNS. First, we need to log into the DNS server and launch DNS Manager.
  2. Once inside DNS Manager, expand Forward Lookup Zones and then your domain name so that we can see the list of DNS records that exist already.
  3. Now, right-click on your domain and select New Alias (CNAME)….
  4. We would like our users to be able to browse the website by typing in http://intranet. So, in our CNAME record, we want Alias name to be intranet and FQDN for target host to be web01.ad.cookbook.com, which is the server where the website is being hosted:

    Figure 2.19 – The New Resource Record screen in the DNS Manager for creating a CNAME

  5. We also want our file shares to be accessible by using \\FILESERVER\SHARE so that the actual name of the server hosting this share is not visible to the users. Create another CNAME record with the Alias name field as FILESERVER and the FQDN for target host field as web01.ad.cookbook.com.
  6. Log into the test client machine and give it a try. Users are now able to open their web browser and successfully browse to http://intranet. They are also able to open File Explorer and access \\fileserver\share.

To create these CNAME records via PowerShell, you need to import the DNSServer module and then use the Add-DnsServerResourceRecordCname cmdlet:

Import-Module DNSServer

Add-DnsServerResourceRecordCName -Name intranet -ZoneName ad.cookbook.packt.com -HostNameAlias web01.ad.cookbook.packt.com

Add-DnsServerResourceRecordCName -Name fileserver -ZoneName ad.cookbook.packt.com -HostNameAlias web01.ad.cookbook.packt.com

How it works…

We have a server in our environment called WEB01. There is a website running on this server. It is also hosting a file share called SHARE. By creating a couple of quick CNAME records inside DNS, we can give users the ability to use some intuitive names to access these resources. By following this recipe, we have masked the actual server name from the users, making knowledge of that name unnecessary.

See also

  • The Creating an A or AAAA record in DNS recipe