China FreeBSD

unbound 内网版域名解析

  • 贡献者:HHJ
  • 阅读:2589
  • 类别:脚本工具
  • 更新时间:2017-12-05 14:43:00
unbound 局域网内域名解析    不能向外发布权威域名.

创建默认配置文件    /usr/sbin/local-unbound-setup   
生成root.key              /usr/sbin/unbound-anchor   
启动/关闭/状态等      /usr/sbin/unbound-control
                                    /usr/sbin/unbound       
检查配置文件            /usr/sbin/unbound-checkconf

更新 root.key
unbound-anchor -a /etc/unbound/root.key

设置自动启动
# echo 'local_unbound_enable="YES"' >> /etc/rc.conf
# service local_unbound start

生成默认配置文件
# local-unbound-setup
Extracting forwarders from /etc/resolv.conf.
No forwarders found in resolv.conf, unbound will recurse.
/var/unbound/lan-zones.conf created
/var/unbound/control.conf created
/var/unbound/unbound.conf created
Stopping local_unbound.
Starting local_unbound.
/etc/resolvconf.conf not modified
/etc/resolv.conf not modified

 # cat /etc/resolvconf.conf
# This file was generated by local-unbound-setup.
# Modifications will be overwritten.
resolv_conf="/dev/null" # prevent updating /etc/resolv.conf
unbound_conf="/var/unbound/forward.conf"
unbound_pid="/var/run/local_unbound.pid"
unbound_service="local_unbound"
unbound_restart="service local_unbound reload"

# cat /etc/resolv.conf
# Generated by resolvconf
# nameserver 192.168.100.1

nameserver 127.0.0.1
options edns0

# cat unbound.conf
# This file was generated by local-unbound-setup.
# Modifications will be overwritten.
server:
    username: unbound
    directory: /var/unbound
    chroot: /var/unbound
    pidfile: /var/run/local_unbound.pid
    auto-trust-anchor-file: /var/unbound/root.key

include: /var/unbound/lan-zones.conf
include: /var/unbound/control.conf
include: /var/unbound/conf.d/*.conf

# cat control.conf
# This file was generated by local-unbound-setup.
# Modifications will be overwritten.
remote-control:
    control-enable: yes
    control-interface: /var/run/local_unbound.ctl
    control-use-cert: no

# cat lan-zones.conf
# This file was generated by local-unbound-setup.
# Modifications will be overwritten.
server:
    # Unblock reverse lookups for LAN addresses
    unblock-lan-zones: yes
    insecure-lan-zones: yes

    private-domain: "fb103.net"
    local-zone: "fb103.net." static
    local-data: "www.fb103.net. IN A 10.0.2.15"
    local-data-ptr: "10.0.2.15 www.fb103.net"
    local-data: "mail.fb103.net. IN CNAME www.fb103.net"
    local-data: "ftp.fb103.net. IN CNAME www.fb103.net"
    local-data: "mx.fb103.net. IN MX 10 www.fb103.net"
    local-data: 'mx.fb103.net. IN TXT "txt record"'

# cat forward.conf
# Generated by local-unbound-setup
forward-zone:
        name: "."
        forward-addr: 8.8.8.8
        forward-addr: 8.8.4.4

创建/var/unbound/forward.conf  设定上游 DNS 服务器
# Generated by local-unbound-setup
forward-zone:
        name: "."
        forward-addr: 8.8.8.8
        forward-addr: 8.8.4.4


本地缓存 DNS 服务设置

注意!本文内容仅适用于 FreeBSD 10。

基础设置

自从 FreeBSD 10 开始,BIND 被弃用。系统默认的 DNS 服务提供者是 unbound。相对于前者,unbound 的设置要更简单一些。本文就是简单介绍如何设置 unbound 作为本地缓存 DNS 服务器。非常简单,步骤如下:

# echo 'local_unbound_enable="YES"' >> /etc/rc.conf
# service local_unbound start

需要注意的几点

不要让 PPP 修改你的 /etc/resolv.conf

如果使用 PPP 拨号,包括 PPPoE,你的 /etc/ppp/ppp.conf 不能包含如下 dns 有关的代码:

enable dns

如果你的 /etc/ppp/ppp.conf 包含这一行,请注释掉。因为有了这个设置以后,PPP 会自动修改你的 /etc/resolv.conf。那么 local_unbound 的工作就失效了。

修改 unbound 的配置文件以设置监听界面/地址

为了细化 unbound 的配置,可以考虑修改 /var/unbound/unbound.conf 来控制监听的方式:

# Generated by local-unbound-setup
server:
        username: unbound
        directory: /var/unbound
        chroot: /var/unbound
        pidfile: /var/run/local_unbound.pid
        auto-trust-anchor-file: /var/unbound/root.key

	interface: 0.0.0.0
	access-control: 10.0.0.0/8 allow

include: /var/unbound/forward.conf

如果将 interface 的值设置为 0.0.0.0,那么 local_unbound 将在所有地址上监听。当然你可以设置为特定的地址,比如只针对内网等等。

interface: 192.168.1.1

如果你只希望特定网段的机器访问 local_unbound,那么就设定 access-control 这个字段。

access-control: 192.168.1.0/24

指定 unbound 使用 TCP 还是 UDP 通讯

unbound 可以提供 TCP 或者 UDP 类型的查询服务。

提供 TCP 方式:

do-tcp: yes (或者 no)

提供 UDP 方式:

do-udp: yes (或者 no)

同时,也可以指定与上游服务器用 TCP 通讯(默认是 UDP):

tcp-upstream: yes (或者 no)

IPv4 还是 IPv6

do-ipv4: yes (或者 no)
do-ipv6: yes (或者 no)

修改 /var/unbound/forward.conf 以设定上游 DNS 服务器

# Generated by local-unbound-setup
forward-zone:
        name: "."
        forward-addr: 8.8.8.8
        forward-addr: 8.8.4.4

这个配置文件一目了然。你可以选择自己喜欢的 DNS 服务器地址填到这里。

上游 DNS 服务器设置不是必须的。如果不设置上游 DNS 服务器,unbound自动运行与递归模式,直接通过13台根 DNS 服务器递归查询,以利于减少 DNS 污染。

修改配置以后别忘了重新启动 local_unbound 服务

# service local_unbound restart

解析本地域名

unbound 也可以用来解析本地域名。比如你在局域网内有一台邮件服务器位于 10.0.0.6,希望通过域名 mail.home.lan 来访问,只需要在 unbound.conf 里面增加如下几条就可以正反双向解析了。

private-domain: "home.lan"
local-zone: "home.lan." static
local-data: "mail.home.lan. IN A 10.0.0.6"
local-data-ptr: "10.0.0.6 mail.home.lan"

include: /var/unbound/forward.conf   # 确保这一行在最后

可用命令# unbound-checkconf unbound.conf 检查文件是否书写正确。

然后重启 local_unbound:

# service local_unbound restart

最后说一句,service(8) 这个命令很赞。


https://en.wikipedia.org/wiki/Transmission_Control_Protocol

https://wenku.baidu.com/view/1aa909f685254b35eefdc8d376eeaeaad1f3169f.html