使用BIND服务转换特定网址

2011年09月16日 18:16

UPDATE:

用dnsmasq,把需要自定义的IP放在/etc/hosts文件中即可。可以更简单的满足类似需求。


有的时候我们需要使用特定的IP访问特定的网站。现在比较流行的方法是修改hosts文件。但这方法需要对每一个客户端做修改,比较麻烦。为了方便起见,我在自己的服务器上尝试了配置DNS服务器来为特定网址提供特定IP的方法。

首先安装bind,在Ubuntu服务器上当然就是:

sudo apt-get install bind9

然后我们将服务器先配置成缓存模式,在 named.conf.options 文件中增加以下内容:

        forwarders {
            8.8.8.8;
            8.8.4.4;
        };
        allow-recursion { any; };

其中forwarders是将DNS请求转发,allow-recursion是设置客户端的访问权限,如果是内网使用,也可以设置网址段。

然后我们使用一个新的配置文件 named.conf.redirect 专门处理特定网址转发请求。在named.conf中增加一句:

include "/etc/bind/named.conf.redirect";

然后新建一个 named.conf.redirect 文件,将特定的网址变成master类型(由本机处理):

zone "plus.google.com" {

    type master;

    file "/etc/bind/redirect/zone_plus.google.com";

};

然后我们去 /etc/bind/redirect 目录下新建一个 zone_plus.google.com 文件,配置DNS规则,简单起见,我们可以这样配置:

$TTL    86400
@ IN    SOA     localhost. root.localhost. (
                              1         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                          86400 )       ; Negative Cache TTL
;

@ IN  NS  localhost.
@ IN   A    1.2.3.4
* IN   A    1.2.3.4

上面的部分可以作为模板,关键就是配置 @ 和 * 两个A地址解析而已。其中 * 可以作为泛域名配置,匹配全部的子域名。

that's all.

注:在named.conf.redirect里可以配置多个zone,他们使用的规则file可以相同也可以不同,可以视情况而定。

注2:如果DNS服务器在国外,这样做仍然无法解决DNS污染的问题。

注3:文中所用IP为演示用,并非真实IP。

推送到[go4pro.org]

2条评论

gravatar for airhunter
令狐威武 airhunter (Email) - 2011年09月17日 11:22

gravatar for nvshow
令狐太棒了 nvshow - 2011年09月17日 14:52




Remember personal info?
Notify
Hide email
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Design downloaded from free website templates.