DHCP服务器只作用于局域网同一网段内,客户端是通过广播消息来获得DHCP服务器响应后才能得到IP地址的,但广播消息不能跨越子网,那么如何让客户端获取到DHCP服务器提供的IP地址呢?这就是DHCP中继服务器的功能了
添加一块网卡
192.168.94.11 ens33(NAT) 192.168.29.29(VMnet2)
[root@DaMoWang ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.94.11 netmask 255.255.255.0 broadcast 192.168.94.255 inet6 fe80::20c:29ff:fe54:3372 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:54:33:72 txqueuelen 1000 (Ethernet) RX packets 9733 bytes 11834218 (11.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3320 bytes 436871 (426.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.29.29 netmask 255.255.255.0 broadcast 192.168.29.255 inet6 fe80::20c:29ff:fe54:337c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:54:33:7c txqueuelen 1000 (Ethernet) RX packets 122 bytes 13196 (12.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 82 bytes 12760 (12.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 9 bytes 1149 (1.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 9 bytes 1149 (1.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
修改DHCP服务配置文件
[root@DaMoWang ~]# yum install dhcp -y [root@DaMoWang ~]# vim /etc/dhcp/dhcpd.conf option domain-name-servers 202.106.0.20,8.8.8.8; subnet 192.168.94.0 netmask 255.255.255.0 { range 192.168.94.100 192.168.94.254; option routers 192.168.94.11; } subnet 192.168.29.0 netmask 255.255.255.0 { range 192.168.29.100 192.168.29.254; option routers 192.168.29.29; }
启动DHCP服务,并检查状态
[root@DaMoWang ~]# systemctl start dhcpd [root@DaMoWang ~]# systemctl status dhcpd ● dhcpd.service - DHCPv4 Server Daemon Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled) Active: active (running) since 二 2018-08-21 13:58:25 CST; 1min 31s ago Docs: man:dhcpd(8) man:dhcpd.conf(5) Main PID: 3421 (dhcpd) Status: "Dispatching packets..." CGroup: /system.slice/dhcpd.service └─3421 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --n... 8月 21 13:58:25 DaMoWang dhcpd[3421]: All rights reserved. 8月 21 13:58:25 DaMoWang dhcpd[3421]: For info, please visit https://www.isc.org/softw...cp/ 8月 21 13:58:25 DaMoWang dhcpd[3421]: Not searching LDAP since ldap-server, ldap-port ...ile 8月 21 13:58:25 DaMoWang dhcpd[3421]: Wrote 0 leases to leases file. 8月 21 13:58:25 DaMoWang dhcpd[3421]: Listening on LPF/ens37/00:0c:29:54:33:7c/192.168.../24 8月 21 13:58:25 DaMoWang dhcpd[3421]: Sending on LPF/ens37/00:0c:29:54:33:7c/192.168.../24 8月 21 13:58:25 DaMoWang dhcpd[3421]: Listening on LPF/ens33/00:0c:29:54:33:72/192.168.../24 8月 21 13:58:25 DaMoWang dhcpd[3421]: Sending on LPF/ens33/00:0c:29:54:33:72/192.168.../24 8月 21 13:58:25 DaMoWang dhcpd[3421]: Sending on Socket/fallback/fallback-net 8月 21 13:58:25 DaMoWang systemd[1]: Started DHCPv4 Server Daemon. Hint: Some lines were ellipsized, use -l to show in full.
配置DHCP中继服务器
# 开启路由转发功能 [root@DaMoWang ~]# vim /etc/sysctl.conf # 添加 net.ipv4.ip_forward=1 [root@DaMoWang ~]# sysctl -p net.ipv4.ip_forward = 1 [root@DaMoWang ~]# vim /etc/sysconfig/dhcrelay INTERFACES=ens33 # DHCP服务器网卡名称 DHCPSERVERS="192.168.94.11" # DHCP服务器IP [root@DaMoWang ~]# dhcrelay 192.168.94.11 # 启动DHCP中继代理服务 Dropped all unnecessary capabilities. Internet Systems Consortium DHCP Relay Agent 4.2.5 Copyright 2004-2013 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/ens37/00:0c:29:54:33:7c Sending on LPF/ens37/00:0c:29:54:33:7c Listening on LPF/ens33/00:0c:29:54:33:72 Sending on LPF/ens33/00:0c:29:54:33:72 Sending on Socket/fallback
用另一台虚拟机测试
网卡先用VMnet2
网段 IP 掩码 DNS都和DHCP服务器分配的一致
NAT
分配的IP等信息也一致
DHCP中继服务配置完成