1. 程式人生 >實用技巧 >其它 >Linux自建DNS

Linux自建DNS


[[email protected] ~]# nslookup www.beidu.com
Server:		192.168.31.1
Address:	192.168.31.1#53

Non-authoritative answer:
Name:	www.beidu.com
Address: 184.154.126.180

[[email protected] ~]# 

# 安裝 bind - 10.4.7.11
[[email protected] ~]# yum install bind -y

# 檢視版本
[[email protected] ~]# rpm -qa bind
bind-9.11.4-26.P2.el7_9.9.x86_64

# 1-主配置
[[email protected] ~]# vim /etc/named.conf 

# 修改的地方
listen-on port 53 { 192.168.31.31; };
allow-query     { any; };
# 添加了一個閘道器地址
forwarders      { 192.168.31.1 };
# 使用yes 採用遞迴
recursion yes;
# 這兩個設定成 no
dnssec-enable no;
dnssec-validation no;


# 檢查配置是否有問題
[[email protected] ~]# named-checkconf

# 2-配置區域檔案
[[email protected] named]# vim /etc/named.rfc1912.zones 

# 配置兩個域,新增如下內容
zone "host.com" IN {
        type master;
        file "host.com.zone";
        allow-update { none; };
};
zone "od.com" IN {
        type master;
        file "od.com.zone";
        allow-update { none; };
};

# 3-配置區域資料檔案
[[email protected] ~]# cd /var/named/

[[email protected] named]# cp -a named.localhost host.com.zone

[[email protected] named]# vim host.com.zone 
$ORIGIN host.com.
$TTL 1D
@       IN SOA  dns.host.com. root.host.com. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      dns.host.com.
dns     A       192.168.31.31
HD-31   A       192.168.31.31
HD-32   A       192.168.31.218
HD-33   A       192.168.31.132

[[email protected] named]# cp -a host.com.zone od.com.zone
[[email protected] named]# vim od.com.zone 
$ORIGIN od.com.
$TTL 1D
@       IN SOA  dns.od.com. root.host.com. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      dns.od.com.
dns     A       192.168.31.31

# 檢查配置檔案是否有問題
[[email protected] named]# named-checkconf

# 啟動
[[email protected] named]# systemctl start named

# 檢視 53 埠
[[email protected] named]# netstat -luntp|grep 53
tcp        0      0 192.168.31.31:53        0.0.0.0:*               LISTEN      2334/named          
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1369/dnsmasq        
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      2334/named          
tcp6       0      0 ::1:53                  :::*                    LISTEN      2334/named          
tcp6       0      0 ::1:953                 :::*                    LISTEN      2334/named          
udp        0      0 192.168.31.31:53        0.0.0.0:*                           2334/named          
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1369/dnsmasq        
udp        0      0 0.0.0.0:57534           0.0.0.0:*                           576/avahi-daemon: r 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           576/avahi-daemon: r 
udp6       0      0 ::1:53                  :::*                                2334/named 

# 檢查主機域
[[email protected] named]# dig -t hostname.host.com @192.168.31.31 +short

# 要linxu 和Windows 客戶端都可以使用。
修改DNS 為(主域IP)

# Windows 的首選DNS 寫主域的IP
[[email protected] network-scripts]# cd /etc/sysconfig/network-scripts
[[email protected] network-scripts]# vim ifcfg-ens33 
GATEWAY=192.168.31.1
DNS1=192.168.31.31
DNS2=192.168.31.1

[[email protected] network-scripts]# systemctl restart network

[[email protected] network-scripts]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 192.168.31.31
nameserver 192.168.31.1


# ping 通本機 新增如下(主域+其他域都加)
vim /etc/resolv.conf
search host.com