本文首發(fā)于我的博客:bind9的初步使用(1)
前言
周五把自己的電腦重裝了一下柬批,還是使用的經(jīng)典的windows+vmware+ubuntu的經(jīng)典方式(對(duì)我來說)。但是我不想每次都修改host文件來實(shí)現(xiàn)我的域名訪問获枝,所以我在想有沒有一個(gè)更好的方式,可以讓我實(shí)現(xiàn)域名映射局服。這個(gè)時(shí)候我想到了自己架設(shè)一個(gè)dns服務(wù)器磷仰。說干就干,我就準(zhǔn)備用dns的開源系統(tǒng)bind9來搞一番颁井。
環(huán)境介紹
- Ubuntu: 18.10 (ip: 192.168.1.230)
- bind9: 9.11.4
- Windows 10 (ip: 192.168.1.230)
安裝
其實(shí)安裝非常簡單厅贪,一條命令就搞定了
sudo apt install bind9
管理命令
啟動(dòng):
sudo systemctl start bind9
停止:
sudo systemctl stop bind9
重啟:
sudo systemctl restart bind9
狀態(tài):
sudo systemctl status bind9
配置域名
舉個(gè)例子,比如現(xiàn)在我們有個(gè)域名是:baoguoxiao.pro⊙疟觯現(xiàn)在我們要對(duì)這個(gè)域名進(jìn)行虛擬映射养涮。
首先打開/etc/bind/named.conf.local
,追加如下內(nèi)容到文件尾部:
zone "baoguoxiao.pro" {
type master;
file "/etc/bind/zones/baoguoxiao.pro.db";
};
那么現(xiàn)在這個(gè)文件的內(nèi)容完整如下:
//
// Do any local configuration here
//
// Consider adding the 1918 zones here, if they are not used in your
// organization
//include "/etc/bind/zones.rfc1918";
zone "baoguoxiao.pro" {
type master;
file "/etc/bind/zones/baoguoxiao.pro.db"; // 這個(gè)文件定義了文件地址
};
我們定義的地址是/etc/bind/zones/baoguoxiao.pro.db
眉抬。但是我們的/etc/bind/
并沒有該目錄单寂。所以需要如下命令進(jìn)行創(chuàng)建:
cd /etc/bind/
sudo mkdir zones
進(jìn)入該目錄:
cd zones
然后創(chuàng)建該文件baoguoxiao.pro.db
,并追加如下命令:
; BIND data file for baoguoxiao.pro
;
$TTL 14400
@ IN SOA ns1.baoguoxiao.pro. host.baoguoxiao.pro. (
201006601 ; Serial
7200 ; Refresh
120 ; Retry
2419200 ; Expire
604800) ; Default TTL
;
baoguoxiao.pro. IN NS ns1.baoguoxiao.pro.
;baoguoxiao.pro. IN A 192.168.1.231
ns1 IN A 192.168.1.231
www IN A 192.168.1.231
這樣就設(shè)置完成了吐辙。然后我們將bind9進(jìn)行重啟宣决。
測試DNS效果
$ dig @192.168.1.231 www.baoguoxiao.pro
; <<>> DiG 9.11.4-3ubuntu5-Ubuntu <<>> @192.168.1.231 www.baoguoxiao.pro
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35630
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: f077ba72f04b75a1ac9b27275c16148f4732abac11c21ce8 (good)
;; QUESTION SECTION:
;www.baoguoxiao.pro. IN A
;; ANSWER SECTION:
www.baoguoxiao.pro. 14400 IN A 192.168.1.231
;; AUTHORITY SECTION:
baoguoxiao.pro. 14400 IN NS ns1.baoguoxiao.pro.
;; ADDITIONAL SECTION:
ns1.baoguoxiao.pro. 14400 IN A 192.168.1.231
;; Query time: 0 msec
;; SERVER: 192.168.1.231#53(192.168.1.231)
;; WHEN: Sun Dec 16 17:02:07 CST 2018
;; MSG SIZE rcvd: 117
好了,這樣就表示已經(jīng)配置成功了
設(shè)置默認(rèn)本機(jī)DNS可用
之前的設(shè)置我們需要指定本機(jī)的DNS服務(wù)器才可以使用昏苏,如果我們不指定的話尊沸,那么查詢該域名是沒有效果的:
$ dig baoguoxiao.pro
; <<>> DiG 9.11.4-3ubuntu5-Ubuntu <<>> baoguoxiao.pro
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 52385
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;baoguoxiao.pro. IN A
;; Query time: 274 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Dec 16 17:03:59 CST 2018
;; MSG SIZE rcvd: 43
在ubuntu17.10之后,網(wǎng)卡配置已經(jīng)更新為netplan贤惯。該配置文件的目錄是/etc/netplan/
洼专。不過里面的文件不一定是相同的名字。我的文件打開是這樣的孵构。
$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource. Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
ens33:
addresses: [192.168.1.231/24]
dhcp4: false
dhcp6: false
gateway4: 192.168.1.1
nameservers:
addresses: [192.168.1.231,114.114.114.114]
version: 2
注意屁商,我在倒數(shù)第二行的數(shù)組里面添加本機(jī)的服務(wù)器192.168.1.231
。關(guān)于該文件的配置颈墅,可以查看我的另外一篇文章:UBUNTU17.10設(shè)置固態(tài)IP蜡镶。
這樣我們在本機(jī)就可以不指定dns服務(wù)器的基礎(chǔ)上進(jìn)行獲取域名的ip了。
$ dig www.bgx.me
; <<>> DiG 9.11.4-3ubuntu5-Ubuntu <<>> www.baoguoxiao.pro
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58219
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 15d0881d8eed3292569558cd5c1623fa33a2d05212e7e662 (good)
;; QUESTION SECTION:
;www.baoguoxiao.pro. IN A
;; ANSWER SECTION:
www.baoguoxiao.pro. 14400 IN A 192.168.1.231
;; AUTHORITY SECTION:
baoguoxiao.pro. 14400 IN NS ns1.baoguoxiao.pro.
;; ADDITIONAL SECTION:
ns1.baoguoxiao.pro. 14400 IN A 192.168.1.231
;; Query time: 0 msec
;; SERVER: 192.168.1.231#53(192.168.1.231)
;; WHEN: Sun Dec 16 18:07:54 CST 2018
;; MSG SIZE rcvd: 117
本篇文章就說到這里恤筛。下一篇講如何配置可局域網(wǎng)訪問官还。