安装Bind9用于本地域名解析

对于家里的移动客户端,在家的时候用Wi-Fi,在外的时候用的移动网,那我们希望在家用Wi-Fi的时候可以直接连内网ip,而不需要连公网ip再转发到Nas的内网ip。以前用小米路由器的时候可以自定义hosts,非常方便。群晖中有一个DNS Server套件可以实现域名直接解析到内网IP,Ubuntu中可以安装Bind9实现同样功能。

Bind9的官方Howto在这里

Bind9安装

1
2
sudo apt update
sudo apt install bind9

Bind9的配置文件位于/etc/bind/,主要配置文件为下面的三个:

1
2
3
/etc/bind/named.conf
/etc/bind/named.conf.options
/etc/bind/named.conf.local

配置转发DNS地址,就是当本地没有解析记录的,就转发到指定的DNS地址解析。编辑配置文件/etc/bind/named.conf.options,指定转发DNS,一般填大网络公司的公共DNS,谷歌8.8.8.8;阿里:223.5.5.5等:

1
2
3
4
5
6
7
8
[...]

forwarders {
8.8.8.8;
114.114.114.114;
};

[...]

现在Bind9就可以工作了,重启Bind9:

1
sudo service bind9 restart

现在就可以在路由器的DHCP设置中将DNS服务器地址设置为NAS的IP了。

接下来我们要编辑区域文件/etc/bind/named.conf.local,增加我们要解析的域名Zone:

1
2
3
4
5
6
7
8
[...]

zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};

[...]

这一段记录example.com域名的解析文件为/etc/bind/db.example.com,接下来我们编辑/etc/bind/db.example.com/etc/bind目录中有一份样板文件db.local:

1
sudo cp /etc/bind/db.local /etc/bind/db.example.com

编辑区域文件/etc/bind/db.example.com,把localhost修改为你的域名example.com,保留后面的点,把127.0.0.1修改为本机IP,root.localhost. 修改为邮件地址,只不过要把@替换为.,同样末尾点保留。

增加一条A记录,把ns.example.com解析到DNS服务器IP,然后就可以添加其他子域名解析,比如我只有一台NAS,所有的记录都解析到NAS的IP上,修改完大概就是这样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA ns.example.com. root.example.com. (
1 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ns.example.com.
ns IN A 192.168.1.10

;also list other computers
www IN A 192.168.1.10
music IN A 192.168.1.10

这样内网访问www.example.commusic.example.com都会解析到192.168.1.10。

注意每次修改此文件,都需要把Serial值增加一次,这样重启的时候Bind9就会知道配置文件有修改。

很多人喜欢用修改日期来作为Serial的值,例如 2023080500,表示2023年8月5日第一次修改。

修改完成后记得重启bind9:

1
sudo service bind9 restart

这样移动设备连接内网的时候,从路由器获取的DNS服务器就是你的NAS地址,NAS就会把你的域名解析到NAS上。这样你就可以用一个域名根据网络环境自动切换内网和外网了。

当然,Bind9除了用来解析域名到局域网外,还可以用来解析某些被DNS劫持的域名,我们可以先去https://tools.ipip.net/dns.php,查询一下域名对应IP,再ping一下选一个延迟低丢包少的ip,然后配置bind9,把域名解析到查询到的ip上。

安装Bind9用于本地域名解析

http://tux.red/2023/08/05/Nas/bind9-dns-server/

作者

redtux

发布于

2023-08-05

更新于

2023-10-01

许可协议

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.