在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。