目的
自己有许多VM,之前通过路由器绑定IP地址,每次访问VM都是通过IP访问,比较麻烦,今天心血来潮,打算自建DNS不用劳神费力记忆IP地址。之前都是配置ssh的 ~/.ssh/config文件,自动ssh到VM。
环境
自行依照环境实验,如果发现有环境不一致的情况,只能作为参考
key | value |
---|---|
操作系统 | CentOS 7 |
DNS机器 | 192.168.0.104 |
自建域名 | opensky.com |
转发DNS | 192.168.1.1 |
准备安装工具
yum -y install bind bind-chroot bind-utils
修改配置文件 /etc/named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.root.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone "opensky.com" IN {
type master;
file "named.opensky.com";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
准备zone文件, /var/named/named.opensky.com, 不过需要注意的是bind必须有该文件的权限,否则结果不符合预期。在这里卡了很久才意识到权限问题。愚蠢了。
$TTL 3H
@ IN SOA @ www.opensky.com. (0 1D 1H 1W 3H)
NS @
A 192.168.0.104
c7-1.opensky.com. IN A 192.168.0.104
c7-2.opensky.com. IN A 192.168.0.112
c7-3.opensky.com. IN A 192.168.0.113
c6-1.opensky.com. IN A 192.168.0.100
c6-1.opensky.com. IN A 192.168.0.100
c6-2.opensky.com. IN A 192.168.0.105
ubuntu.opensky.com. IN A 192.168.0.110
客户端
编辑 /etc/resolv.conf
search opensky.com
nameserver 192.168.0.104
CentOS 7 下resolv.conf 被NetworkManager管理,因此如果重启VM,会导致resolv.conf失效。因此需要修改/etc/sysconfig/network-scripts/ifcfg-enp0s3 脚本。需要修改的内容如下,其他保持不变
PEERDNS=no
DOMAIN="opensky.com"
DNS1=192.168.0.104
转发功能
局域网的机器不仅仅需要访问自定义域名,如果还需要访问其他域名,自建的DNS就需要将请求转发到其他DNS服务器上,因此需要增加配置。配置内容如下
options {
dnssec-enable no;
dnssec-validation no;
forwarders {
192.168.1.1;
192.168.0.1;
};
forward first;
};