bind9的初步使用(1)

本文首發於個人博客: bind9的初步使用(1)

前言

週五把本身的電腦重裝了一下,仍是使用的經典的windows+vmware+ubuntu的經典方式(對我來講)。可是我不想每次都修改host文件來實現個人域名訪問,因此我在想有沒有一個更好的方式,可讓我實現域名映射。這個時候我想到了本身架設一個dns服務器。說幹就幹,我就準備用dns的開源系統bind9來搞一番。ubuntu

環境介紹

  • Ubuntu: 18.10 (ip: 192.168.1.230)
  • bind9: 9.11.4
  • Windows 10 (ip: 192.168.1.230)

安裝

其實安裝很是簡單,一條命令就搞定了windows

sudo apt install bind9

管理命令

啓動:數組

sudo systemctl start bind9

中止:bash

sudo systemctl stop bind9

重啓:服務器

sudo systemctl restart bind9

狀態:ide

sudo systemctl status bind9

配置域名

舉個例子,好比如今咱們有個域名是:baoguoxiao.pro。如今咱們要對這個域名進行虛擬映射。測試

首先打開/etc/bind/named.conf.local,追加以下內容到文件尾部:rest

zone "baoguoxiao.pro" {
    type master;
    file "/etc/bind/zones/baoguoxiao.pro.db";
};

那麼如今這個文件的內容完整以下:code

//
// 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"; // 這個文件定義了文件地址
};

咱們定義的地址是/etc/bind/zones/baoguoxiao.pro.db。可是咱們的/etc/bind/並無該目錄。因此須要以下命令進行建立:orm

cd /etc/bind/
sudo mkdir zones

進入該目錄:

cd zones

而後建立該文件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

這樣就設置完成了。而後咱們將bind9進行重啓。

測試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

好了,這樣就表示已經配置成功了

設置默認本機DNS可用

以前的設置咱們須要指定本機的DNS服務器纔可使用,若是咱們不指定的話,那麼查詢該域名是沒有效果的:

$ 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以後,網卡配置已經更新爲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

注意,我在倒數第二行的數組裏面添加本機的服務器192.168.1.231。關於該文件的配置,能夠查看個人另一篇文章:UBUNTU17.10設置固態IP

這樣咱們在本機就能夠不指定dns服務器的基礎上進行獲取域名的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

本篇文章就說到這裏。下一篇講如何配置可局域網訪問。

相關文章
相關標籤/搜索