在Linux系统中,DHCP(动态主机配置协议)与DNS的联动可以通过多种方式实现,以确保客户端在获取IP地址的同时,也能获得正确的DNS服务器信息,从而能够正常进行域名解析。以下是几种常见的实现方法:
安装必要软件包:
在Debian/Ubuntu系统上:
sudo apt-get update
sudo apt-get install isc-dhcp-server bind9
在CentOS/RHEL系统上:
sudo yum install dhcp bind bind-utils
配置DHCP服务器:
编辑 /etc/dhcp/dhcpd.conf 文件,添加以下内容:
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option routers 192.168.1.1;
option domain-name "example.com";
option domain-name-servers 192.168.1.1;
ddns-update-style interim;
update-static-leases on;
}
配置BIND DNS服务器:
编辑 /etc/bind/named.conf.local 文件,添加以下内容:
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
allow-update { key rndc-key; };
};
创建并编辑 /etc/bind/db.example.com 文件,添加以下内容:
$TTL 86400
@ IN SOA ns1.example.com. admin.example.com. (
2021071001 ; Serial
3600 ; Refresh
1800 ; Retry
1209600 ; Expire
86400 ) ; Minimum TTL
IN NS ns1.example.com.
IN NS ns2.example.com.
ns1 IN A 192.168.1.10
ns2 IN A 192.168.1.11
www IN A 192.168.1.100
重启服务:
sudo systemctl restart isc-dhcp-server
sudo systemctl restart bind9
安装dnsmasq:
在Debian/Ubuntu系统上:
sudo apt-get update
sudo apt-get install dnsmasq
在CentOS/RHEL系统上:
sudo yum install dnsmasq
配置dnsmasq:
编辑 /etc/dnsmasq.conf 文件,添加以下内容:
interface=eth0
dhcp-range=192.168.1.100,192.168.1.200,12h
dhcp-option=option:router,192.168.1.1
dhcp-option=option:dns-server,192.168.1.1
配置DNS转发:
编辑 /etc/resolv.conf 文件,添加以下内容:
nameserver 192.168.1.1
重启服务:
sudo systemctl restart dnsmasq
对于桌面环境,可以使用NetworkManager管理动态DNS更新:
编辑NetworkManager配置:
编辑 /etc/NetworkManager/NetworkManager.conf 文件,添加以下内容:
[main]
dns=dnsmasq
创建NetworkManager DNS配置文件:
创建 /etc/NetworkManager/dnsmasq.d/dhcp-to-dns.conf 文件,添加以下内容:
dhcp-option=option:domain-search,example.com
重启NetworkManager服务:
sudo systemctl restart NetworkManager