大网数据广告活动
服务器运维问答

Resolve a domain name to CNAME alias locally using dnsmasq

· 52CE 我爱测

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 somedomai…

问题原文

原帖链接 · luqo33 · CC BY-SA 3.0

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!

社区高分解答

原帖链接 · Tommiie · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。