Resolve a domain name to CNAME alias locally using dnsmasq

I'd like to resolve a domain name somedomain.com to a CNAME (AWS load balancer, e.g. some-balancer-1213231237.ap-southeast-2.elb.amazonaws.com), but only locally.

What I mean to achieve is that whenever I try to visit somedomain.com, I want to be served by the above mentioned load-balancer - this should only be limited to my own computer.

It's not possible to achieve this by editing /etc/hosts as in there only A records (IP addresses) can be mapped. I read somewhere that dnsmasq would be the most robust solution to achieve this. However, the documentation is very unclear about how this can be achieve. I'd appreciate your advice and perhaps a piece of config with an example. Thanks!


You can add the following to your configuration file in dnsmasq:

cname=somedomain.com,some-balancer-1213231237.ap-southeast-2.elb.amazonaws.com

as specified in the man page:

--cname=<cname>,[<cname>,]<target>[,<TTL>]

Return a CNAME record which indicates that <cname> is really <target>. There are significant limitations on the target; it must be a DNS name which is known to dnsmasq from /etc/hosts (or additional hosts files), from DHCP, from --interface-name or from another --cname. If the target does not satisfy this criteria, the whole cname is ignored. The cname must be unique, but it is permissible to have more than one cname pointing to the same target. Indeed it's possible to declare multiple cnames to a target in a single line, like so: --cname=cname1,cname2,target

If the time-to-live is given, it overrides the default, which is zero or the value of --local-ttl. The value is a positive integer and gives the time-to-live in seconds.

As the man page specifies, you will have to define the target in your /etc/hosts file though:

203.0.113.80   some-balancer-1213231237.ap-southeast-2.elb.amazonaws.com

So I'm not sure this would be very useful to you.