有时候开发需要,得在内网搭建一个DNS服务器,这样进行内网开发与测试,都方便得多,不用每个人都去手动设置一一大堆的hosts,而且移动app测试也更加方便,只需要将wifi的陆由器的DHCP设置,中的DNS地址设置为我们的dns服务器就好。
在ubuntu上建一个dns服务器,可以用bind9实现。
1 | sudo apt-get install bind9 |
然后就是修改bind9的配置文件了:
下面配置 forwarders ,表示本地dns服务器没有命中的记录,通过里面设置的dns去查询并返回。
1 2 3 4 5 6 | sudo vim /etc/bind/named .conf.options #加入: forwarders { 8.8.8.8; 202.96.134.133; // 这是深圳的DNS,我在深圳,别的地方请查当地的DNS }; |
假如现在我们想要在dns服务器上设置 test.com这个域名的指定ip地址。
1 2 3 4 5 6 | sudo vim /etc/bind/named .conf. local #加入: zone "test.com" { type master; file "/etc/bind/db.test.com" ; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | sudo vim /etc/bind/db . test .com #加入: ; ; BIND data file for local loopback interface ; $TTL 604800 @ IN SOA test .com. root. test .com. ( 20130326 ; Serial 604800 ; Refresh 86400 ; Retry 2419200 ; Expire 604800 ) ; Negative Cache TTL ; @ IN NS ns. @ IN A ns. @ IN AAAA ::1 yes IN A 192.168.2.80 |
20130326 ; Serial 序列号可以设置成当前日期。以上设置后 yes.test.com 就会指向 192.168.2.80 这个IP。
然后 sudo service bind9 restart。
可以用命令测试:
dig test.com