引言
因为在本地环境搭建了一个git服务器,有很多同事一起使用,每次通过地址访问实在是感觉很不好,通过域名访问,又需要修改hosts,为了一劳永逸解决这个纠结的问题, 配置一个本地dns服务器是最好的选择
系统环境
- dns服务器操作系统: ubutnu16.0.4
- dns服务器地址: 10.0.0.10
- git服务器地址. 10.0.0.11
- dns工具: bind9
目标
通过配置dns地址为10.0.0.10之后
把域名git.crisen.org解析到10.0.0.11的服务器上
安装
安装bind工具,在当前时间节点bind的版本是bind9.使用下列命令安装
sudo apt update
sudo apt install bind9 -y
# centos请使用 sudo yum install bind
配置
打开/etc/bind/named.conf.local 文件
sudo vim /etc/bind/named.conf.local
输入如下内容
zone "crisen.org"{
type master;
file "db.crisen.org";
};
zone 指定域名, file指定域名解析文件
配置域名解析文件
sudo cp /etc/bind/db.local /var/cache/bind/db.crisen.org
sudo vim /var/cache/bind/db.crisen.org
打开看到如下内容
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA localhost. root.localhost. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS localhost.
@ IN A 127.0.0.1
@ IN AAAA ::1
最后一行加上
@ IN A 10.0.0.11
细心一点 这里就能发现 @表示泛解析 A表示A记录 10.0.0.11就是解析的ip地址 使用过其他域名服务商的朋友们应该比较熟悉这个配置了,也就是说如果我想添加git.crisen.org的解析到10.0.0.11,那么配置应该是git IN A 10.0.0.11
配置dns服务器访问外网
sudo vim /etc/bind/named.conf.options
在13行左右的位置,修改forwarders配置,这里配置为114.114.114.114,可以替换成任意dns地址
forwarders {
114.114.114.114;
};
重启bind服务并清除缓存
sudo service bind9 restart
sudo rndc flush
测试
将10.0.0.10地址作为首选dns服务器,使用ping命令测试
ping git.crisen.org
#
PING git.crisen.org (10.0.0.11) 56(84) bytes of data.
64 bytes from gitlab.crisen.org (10.0.0.11): icmp_seq=1 ttl=64 time=0.405 ms
...
最后域名成功解析到了10.0.0.11服务器上,DNS服务器配置完毕