Saturday, February 13, 2010

CNAME

CNAME is short for canonical name which is a type of resource record (resource records include A record, MX, DNAME, AAAA for IPv6 etc.) found in a DNS database. A CNAME specifies that the domain name is an alias of another canonical domain name and is used to point to an already existing A record. A few of CNAME uses is too mask a domain, , point to another subdomain, point several services such as ftp and www to a single IP address, point to an outside domain or service.

The uses of CNAME are summarized below:

1. CNAME is used to point or alias to another domain. Here is an example:

[root@dns1 forward]# more example.com.db
;
$TTL 43200
@ IN SOA dns1.example.com.ph. postmaster.example.com.ph. (
2010021002 ; Serial
1800 ; Refresh - 30 minutes
300 ; Retry - 5 minutes
604800 ; Expire - 1 week
2880 ) ; Minimum - 8 hours

IN NS dns1.example.com.ph.
IN NS dns2.example.com.ph.
only IN A 10.1.1.1
carlo IN CNAME only

The above example is a real file zone taken from a unix base DNS server obviously masked for confidentiality. In this example, the subdomain is carlo.example.com which is pointed to another domain which is only.example.com which already has an existing A record.

Another example below:

[root@dns1 forward]# more example.com.db
;
$TTL 43200
@ IN SOA dns1.example.com.ph. postmaster.example.com.ph. (
2010021002 ; Serial
1800 ; Refresh - 30 minutes
300 ; Retry - 5 minutes
604800 ; Expire - 1 week
2880 ) ; Minimum - 8 hours

IN NS dns1.example.com.ph.
IN NS dns2.example.com.ph.
www IN A 10.1.1.1
www1 IN CNAME www

Result: Browsing to www1.example.com would render the same page as www.example.com

A CNAME-record should always point to an A-record and never to itself or another CNAME-record to avoid circular references. For example, in our example above, there's already an existing A record before we CNAMEd carlo and www1.

*Take note though that CNAMEing the main domain example.com to another domain such as www.carlo.com is not allowed for some reasons. I tried it once and check but it's not resolving the canonical name. This is only allowed for the subdmains.

2. Another use of CNAME is when you want a subdomain to point to a host outside of your domain. For example, you might want "ftp.example.com" to go to an ftp server of another domain. Instead of putting in the IP address, you could put configure the following:

Subdomain to enter: ftp.example.com
Hostname to enter: ftp.server.com

That way if the IP address of the ftp.server.com changed, you wouldn't have to make any changes.

If you check the zone file of server.com, you will see an A record for ftp.server.com pointing to IP add 10.1.1.2 (means ftp.server.com points to 10.1.1.2).

By adding a CNAME for ftp.example.com, your basically pointing it to that IP add without creating another a record.

ftp IN A 10.1.1.2

3. Another simple example of CNAME is when you wish to mask your domain:

Example: To point www.example.com to another website while maintaining www.example.com in the address bar of the browser:

Subdomain: to enter: www
Hostname to enter: www.proxy.com

4. To run multiple services like ftp, mail, www etc, each running on different ports from a single IP address. Each service can can then have its own entry in

DNS like ftp.example.com, mail.example.com, www.example.com. Example for this zone file is:

[root@dns1 forward]# more example.com.db
;
$TTL 43200
@ IN SOA dns1.example.com.ph. postmaster.example.com.ph. (
2010021002 ; Serial
1800 ; Refresh - 30 minutes
300 ; Retry - 5 minutes
604800 ; Expire - 1 week
2880 ) ; Minimum - 8 hours

IN NS dns1.example.com.ph.
IN NS dns2.example.com.ph.
server IN A 10.1.1.1
ftp IN CNAME server
mail IN CNAME server
www IN CNAME server

In this example ftp.example.com, mail.example.com and www.example.com all points to server.example.com (10.1.1.1).

====================================

Below are the ways to check CNAME if it's working

[root@dns1 forward]# more example.com.db
;
$TTL 43200
@ IN SOA dns1.example.com.ph. postmaster.example.com.ph. (
2010021002 ; Serial
1800 ; Refresh - 30 minutes
300 ; Retry - 5 minutes
604800 ; Expire - 1 week
2880 ) ; Minimum - 8 hours

IN NS dns1.example.com.ph.
IN NS dns2.example.com.ph.

www IN CNAME www.carlo.com

dns1.example.com.ph. ---->10.1.1.4
dns2.example.com.ph. ---->10.1.1.5
www.carlo.com ----------->10.1.1.6
===================

[root@dns1 forward]# nslookup www.example.com
Server: 10.1.1.3
Address: 10.1.1.3#53

Non-authoritative answer:
www.example.com canonical name = www.carlo.com.
Name: www.carlo.com
Address: 10.1.1.6

www.example.com resolves to canonical name www.carlo.com

===================

C:\Documents and Settings>ping www.example.com

Pinging www.globe.com.ph [10.1.1.6] with 32 bytes of data:

Reply from 10.1.1.6: bytes=32 time=2ms TTL=121
Reply from 10.1.1.6: bytes=32 time=2ms TTL=121
Reply from 10.1.1.6: bytes=32 time=1ms TTL=121
Reply from 10.1.1.6: bytes=32 time=1ms TTL=121

Ping statistics for 10.1.1.6:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 1ms, Maximum = 2ms, Average = 1ms

*Noticed that the ping reply comes from the IP address of www.carlo.com (10.1.1.6)

==================

[root@dns1 ~]# dig www.example.com

; <<>> DiG 9.3.4-P1 <<>> www.example.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55801
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 3, ADDITIONAL: 3

;; QUESTION SECTION:
;www.example.com. IN A

;; ANSWER SECTION:
www.example.com. 43200 IN CNAME www.carlo.com.
www.carlo.com. 3256 IN A 10.1.1.6

;; AUTHORITY SECTION:
carlo.com. 3145 IN NS dns1.example.com.
carlo.com. 3145 IN NS dns2.example.com.


;; ADDITIONAL SECTION:
dns1.example.com . 209 IN A 10.1.1.4
dns2.example.com. 11 IN A 10.1.1.5

;; Query time: 2 msec
;; SERVER: 10.1.1.3#53(10.1.1.3)
;; WHEN: Wed Feb 10 21:12:27 2010
;; MSG SIZE rcvd: 195

[root@dns1 ~]#


In the above example if a query for the address of www.example.com is received, two look-up operations are performed on the master or slave server. The first finds www.example.com which finds a CNAME RR. This is followed by a query for www.carlo.com to obtain the IP, that is, the CNAME chain is followed to attempt to resolve the request for an IP address.

========================

I also have included some usefull DNS lookup tool below for your reference:

DNS lookup tool
http://www.dnsstuff.com/tools/

DNS Dig Tool
http://www.kloth.net/services/dig.php

No comments:

Post a Comment