在Ubuntu虛擬機中配置DNS服務器可以通過以下幾種方法實現,這里以使用systemd-resolved和bind9為例進行說明:
systemd-resolved配置DNS服務器(適用于Ubuntu 18.04及更高版本)安裝systemd-resolved(如果尚未安裝):
sudo apt update
sudo apt install systemd-resolved
編輯systemd-resolved配置文件:
sudo nano /etc/systemd/resolved.conf
取消注釋并設置DNS服務器:
在[Resolve]部分添加或修改DNS服務器地址,例如:
DNS=8.8.8.8 8.8.4.4
重啟systemd-resolved服務以應用更改:
sudo systemctl restart systemd-resolved
驗證DNS設置: 使用以下命令檢查當前的DNS設置:
systemd-resolve --status
bind9配置DNS服務器安裝bind9:
sudo apt update
sudo apt install bind9 bind9utils bind9-doc
編輯bind9的主配置文件:
sudo nano /etc/bind/named.conf.options
配置DNS服務器的全局選項,例如設置轉發器:
options {
directory "/var/cache/bind";
forwarders {
8.8.8.8;
8.8.4.4;
};
dnssec-validation auto;
auth-nxdomain no; # conform to RFC1035
listen-on-v6 { any; };
};
編輯bind9的區域配置文件:
sudo nano /etc/bind/named.conf.local
添加區域配置:
zone "example.local" {
type master;
file "/etc/bind/db.example.local";
};
創建區域數據庫文件:
sudo cp /etc/bind/db.local /etc/bind/db.example.local
sudo nano /etc/bind/db.example.local
添加區域記錄:
$TTL 604800
@ IN SOA ns1.example.local. admin.example.local. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
@ IN NS ns1.example.local.
ns1 IN A 192.168.1.10
www IN A 192.168.1.10
重啟bind9服務以應用更改:
sudo systemctl restart bind9
驗證DNS配置: 使用以下命令檢查配置文件是否正確:
sudo named-checkconf
sudo named-checkzone example.local /etc/bind/db.example.local
測試DNS服務器:
使用nslookup或dig命令測試:
nslookup www.example.local
dig @127.0.0.1 www.example.local
在Ubuntu客戶端上配置DNS服務器:
編輯/etc/systemd/resolved.conf文件,在[Resolve]部分添加你的DNS服務器IP:
sudo nano /etc/systemd/resolved.conf
添加以下內容:
DNS=192.168.1.100
重啟網絡服務:
sudo systemctl restart systemd-resolved
在其他客戶端(如Android、Windows)配置DNS: 設置DNS地址為Ubuntu服務器的IP。