LinuxTOY 是一个致力于提供 Linux 相关资讯的专题站点。如果您发现了好用好玩的 Linux 东东并愿意发扬自由、分享的精神,可以点击顶部导航 Contact 按钮进行投稿。

Related entries

pdns-recursor:dns 解析器

[撰文/hmy]

pdns-recursor 是一个 dns 解析器(recursor),是 debian 从 powerdns 里面单独编译出来的只做 dns 解析器应用的软件。

powerdns 本身是一个支持 mysql 数据库的 dns 服务器。

很多人应该都习惯了设置 isp 提供的 dns 服务器,然后忍受被劫持域名的搔扰,查不到内容就给你整到 114 页面(上海电信 dns),其实 linux 用户完全不必受这个气,自己装一个 dns 解析器就行了。优点是安全,不用受 dns 劫持,不用怕最新的 dns 安全漏洞,完全没有任何缺点。唯一的缺点可能就是多耗你几 M 内存而已。

再加上最近发现的 dns 协议漏洞,打过补丁的 bind 甚至还有问题,所以推荐用 pdns-recursor,powerdns 官方早就预防了这个问题。可以参考 powerdns 官方的文档。

pdns-recursor 跑起来以后,记的把 dns 地址改到 127.0.0.1,反正就是你监听到那个 ip,就修改你的 dns 到那个 ip。

使用 pppoe 联网一般会自动修改 dns,可以禁止自动修改 dns 就行。

更新

以 Ubuntu/Debian 为例,要安装 pdns-recursor,只需简单的执行下列命令即可:

sudo apt-get install pdns-recursor

注意,没有 pdns-recursor 的 Linux 发行版,可以安装 Bind 这个软件包。

然后在 /etc/resolv.conf 开头加一行:

nameserver 127.0.0.1

或者点 Ubuntu 桌面右上角网络图标,选“手动配置 → 解锁 → DNS”添 127.0.0.1。

[感谢雪梨朋友补充]

署名 • 注明出处 • 非商业性使用

35 Comments

  1. 1 这家伙是谁 Commented @ 2008-08-23 7:09 pmReply to this comment

    听起来不错,但没接触过,能写个稍微详细一点的教程吗?

  2. 2 随风 Commented @ 2008-08-23 7:16 pmReply to this comment

    写得思路很乱。
    要实现不被dns劫持,得找一个安全一点的opendns。
    opendns就不错。
    可以用dns缓存类软件指向opendns。
    要不就像这类的dns软件,指向安全点的dns,或者dns root。

  3. 3 hmy Commented @ 2008-08-23 7:30 pmReply to this comment

    @这家伙是谁
    在debian上直接apt-get install pdns-recursor ,服务默认就启动了,然后修改dns地址到127.0.0.1就行了
    @随风
    这个和你找一个安全的opendns是一个意思,相当于你自己假设了一个”opendns”
    而且dns服务器在本机运行,会减少一些解析域名的时候的网络延迟。提高访问速度。

    ps:dns本身是一个很基础很重要的服务,涉及的概念很多,所以要完全说明白很难,所以写的有点乱,这个文章主要的意思就是:“运行自己的dns解析器,不使用isp提供的服务器,增加安全性和解析速度”

  4. 4 TTG Commented @ 2008-08-23 7:42 pmReply to this comment

    听上去不错,等ubuntu也出。

  5. 5 hmy Commented @ 2008-08-23 7:51 pmReply to this comment

    @TTG ubuntu或许已经有了,毕竟ubuntu是基于debian的软件仓库

    其实也可以直接装bind这个软件实现同样的功能。
    总之就是不使用isp提供的dns服务器

  6. 6 hmy Commented @ 2008-08-23 8:34 pmReply to this comment

    装好以后,可以用下面任意一个命令来检测dns是否工作正常。
    1. dig @127.0.0.1 http://www.g.cn

    2. host http://www.g.cn 127.0.0.1

    3. nslookup http://www.g.cn 127.0.0.1

  7. 7 konit Commented @ 2008-08-23 8:47 pmReply to this comment

    想問一下,本地建DNS服務器,DNS數據庫會大概占用多少硬盤空間?

  8. 8 hmy Commented @ 2008-08-23 8:53 pmReply to this comment

    @konit
    这个只是一个dns 解析器,或者说cache dns, 它主要的工作是去”权威“dns服务器查询结果,缓存在本地内存,把结果返回给dns查询的发起程序。
    所以不占用磁盘空间。只耗费很少的一点内存而已。

  9. 9 pythonikkat Commented @ 2008-08-23 9:13 pmReply to this comment

    hmy,能不能简单讲一下如何配置和使用?

  10. 10 雪梨 Commented @ 2008-08-23 9:37 pmReply to this comment

    toy 编辑下这篇文章好了。

    楼上的,
    Ubuntu/Debian 里直接 sudo apt-get install pdns-recursor 就可以了。

    然后在 /etc/resolv.conf 开头加一行
    nameserver 127.0.0.1
    或者点 ubuntu 桌面右上角网络图标,选 手动配置,解锁,DNS,
    添 127.0.0.1

  11. 11 hmy Commented @ 2008-08-23 9:51 pmReply to this comment

    简单说来按以下三步就行了,
    1.有pdns-recursor 的linux发行版推荐安装pdns-recursor,
    没有pdns-recursor的linux发行版可以安装bind这个软件包(默认情况配置下应该就是一个cache dns server了)

    2.装好软件包后一般都是自动把服务起来了的,可以用我上面提到的命令检测,或者是用netstat| -natu|grep 53查询一下53端口是否在监听.

    3.最后一步就是修改你的系统的dns服务器地址为本地dns的地址,一般设置成127.0.0.1就行。

  12. 12 pythonikkat Commented @ 2008-08-23 10:05 pmReply to this comment

    谢谢雪梨和hmy。我用networkmanager,一重启会自动修改/etc/resolv.conf。另外,hmy,是不是可以详细解释一下“一般都是自动把服务起来了的,可以用我上面提到的命令检测,或者是用netstat -natu|grep 53查询一下53端口是否在监听.“我的结果是

    $ netstat -natu |grep 53
    tcp 0 0 128.253.122.107:42170 202.101.189.122:80 ESTABLISHED
    udp 0 0 0.0.0.0:5353 0.0.0.0:*

  13. 13 hmy Commented @ 2008-08-23 10:09 pmReply to this comment

    你这个输出结果并没有dns服务器在运行,看看我的输出结果吧
    com2:/home/hmy# netstat -natu|grep 53
    tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN
    udp 0 0 0.0.0.0:53 0.0.0.0:*
    另外networkmanager应该可以设置成不自动修改dns地址。

  14. 14 雪梨 Commented @ 2008-08-23 10:42 pmReply to this comment

    @pythonikkat
    NetworkManager 就是上边说的:

    点 ubuntu 桌面右上角网络图标,选 手动配置,解锁,DNS,
    添加 127.0.0.1

  15. 15 随风 Commented @ 2008-08-24 12:13 amReply to this comment

    另外一种方案
    pdnsd(其它的dns缓存软件也可以,没必要搞个大块头bind来做缓存)
    指向opendns 现在默认配置里有opendns节,取消注解就行了。
    或者dns根服务器。
    pdnsd — dns缓存器。

  16. 16 hmy Commented @ 2008-08-24 8:00 amReply to this comment

    用pdnsd 指向根dns服务器也是不不错的方法。
    ps: bind其实块头不大

    有时间我发布一个用puppet来配置上面提到的那些内容的puppet配置文件。

  17. 17 aldous Commented @ 2008-08-24 4:04 pmReply to this comment

    我每次修改完系统的dns地址后不久它又自己悄悄该回去了。怎么回事呢?怎么解决啊?

  18. 18 hmy Commented @ 2008-08-24 4:15 pmReply to this comment

    @aldous 你是什么系统,是用pppoe来联网的吗?
    如果是的话,修改/etc/ppp/peers/dsl-provider ,注释掉usepeerdns

  19. 19 aldous Commented @ 2008-08-24 4:33 pmReply to this comment

    我用的是ubuntu8.04,是用pppoe来联宽带的。注释掉usepeerdns后,还是没能解决这个问题,系统的dns地址仍然顽固地一次又一次地回来:(

  20. 20 hmy Commented @ 2008-08-24 4:52 pmReply to this comment

    @aldous 你可能需要重启一下pppoe服务。

  21. 21 pythonikkat Commented @ 2008-08-24 7:30 pmReply to this comment

    @aldous
    这个很可能是因为你用了networkmanager。NM会在每次重启或者resume后重新生成一个/etc/resolv.conf,nameserver lookup全部在这里面。解决方案:手动添加nameserver 127.0.0.1。如何在每次重新开启计算机后自动添加这一行到/etc/resolv.conf,我还没找到答案。如果你是每一次都shutdown计算机的话,可以使用/etc/rc.local。在其中添加一句:sleep 30 && echo “nameserver 127.0.0.1″ >> /etc/resolv.conf,这样可以在系统开启后三十秒将“nameserver 127.0.0.1“追加到/etc/resolv.conf。但是如果你是从hibernation(休眠)状态resume的话,我还没找到一个解决方案。希望大家帮帮忙。我想知道resume的话,会调用什么类似于/etc/rc.local的脚本。

  22. 22 hmy Commented @ 2008-08-24 7:38 pmReply to this comment

    @pythonikkat
    可以用crontab实现.
    不过没必要弄的那么”脏”,一般说来应该可以关掉networkmanager的自动dns修改的.

  23. 23 pythonikkat Commented @ 2008-08-24 8:26 pmReply to this comment

    我找不到networkmanager设置的地方。我用的是arch,不知道那个文件在控制这个。所以找了个我自己都认为比较不好的方法先凑活着。

  24. 24 aldous Commented @ 2008-08-24 10:46 pmReply to this comment

    @hmy&pythonikkat
    谢谢那么的指点。那么怎么关掉networkmanager的自动dns修改呢?我在networkmanager托盘里找了会儿不知道该从哪儿入手

  25. 25 Canal Commented @ 2008-08-24 11:20 pmReply to this comment

    用过opendns, 它本身也有很烦人的CENS0RSHIP

  26. 26 dogfox Commented @ 2008-08-26 10:10 amReply to this comment

    如果你使用的是ubuntu,同时使用pppoeconf拨号上网,就没有必要使用networkmanager来管理拨号,使用sudo apt-get remove networkmanager* 删除即可解决它自作多情的修改dns的功能

  27. 27 mephisto Commented @ 2008-08-27 1:44 pmReply to this comment

    依赖boost。。。。。

  28. 28 Tommy Commented @ 2008-08-27 2:53 pmReply to this comment

    @pythonikkat
    在 /etc/conf.d/dhcpcd 中,DHCPCD_ARGS中加入”-R”参数就可以既用DHCP,防止DHCP修改自己的resolv.conf了,不需要用这么dirty的手段。

    -R, –nodns
    Don’t send DNS information to resolvconf or touch /etc/resolv.conf.

  29. 29 fcicq Commented @ 2008-08-27 4:50 pmReply to this comment

    前面的同学们啊… bind 默认不开启转发功能… djbdns 默认开.

    喜欢简单的就 dnsmasq 也挺好. 设置 bogus-nxdomain=劫持你的IP (可以多写几行) 就可以对抗劫持了.

  30. 30 pythonikkat Commented @ 2008-08-27 7:15 pmReply to this comment

    @Tommy
    我试过,并不工作。NetworkManager并不用那个脚本,wiki上说network才用那个脚本。

  31. 31 Tommy Commented @ 2008-08-28 10:21 amReply to this comment

    @pythonikkat
    那就不知道了。我以前用fluxbox,现在用awesome3,没有用过NetworkManager

  32. 32 Extra Commented @ 2008-08-28 11:42 amReply to this comment

    说到这个,我倒是想问问原来不明白的一个问题,pdns-recuresor和pdns-server有什么区别?

  33. 33 hmy Commented @ 2008-08-28 12:43 pmReply to this comment

    @Extra
    pdns-recuresor 只是一个cache dns,pdns-server是可以做权威服务器的dns
    有点类似于代理服务器 (squid)和web服务器的关系.
    你能理解cache dns和权威dns的区别自然就明白了.

  34. 34 Ivan Chen(西滨) Commented @ 2008-08-29 3:15 pmReply to this comment

    参考opendns的做法,将127.0.0.1加到/etc/dhcp3/dhclient.conf,NM就会每次都用127.0.0.1来作为dns服务器了。

    $ sudo cp /etc/resolv.conf /etc/resolv.conf.auto
    $ gksu gedit /etc/dhcp3/dhclient.conf
    # append the following line to the document
    prepend domain-name-servers 127.0.0.1;
    # save and exit

  35. 35 Ivan Chen(西滨) Commented @ 2008-08-29 3:17 pmReply to this comment

    @hmy,pdns-recuresor 只是一个cache dns,那它又是向哪个权威服务器的dns查询的呢?