Search code examples
apacheubuntusubdomaincnamea-records

How are creating subdomain by adding A record is different from editing /etc/hosts


I was going through this answer about how to create subdomain. I used to create subdomain in my Linode server by adding a A record and then creating a virtual host. What different does it makes to create subdomain using A record and by editing /etc/hosts. Which is better way ? Also should I use CNAME record for it ?


Solution

  • Editing /etc/hosts just makes the subdomain resolvable on the local machine only. If your machine is a DNS authority for your domain, then adding the A record on the server will make that subdomain available for anyone. If your machine is not the DNS authority for the domain, then you need to create the record in the DNS server wherever that is, maybe a corporate DNS or GoDaddy or where you bought the domain from.

    As for A vs CNAME, an A record means you want to resolve the subdomain to an IP address. The CNAME is essentially an alias that can resolve to a different DNS record in the zone file. You may have www.abc.com as a CNAME to your A record of abc.com. It is used to prevent dependencies on IP addresses except where necessary. If you have a domain with lots of subdomains that all go to a small number of servers, if an IP address changes, and all of those records are A records, it means a lot of DNS changes. If they were CNAME records, you only have to change the specific A records that changes.