1、预分配网络参数如下:
linux服务器:eth0 IP为192.168.8.250 做为局域网DHCP服务器
局域网网段设置为192.168.8.0/24;
内部计算机的router为192.168.8.254;
DNS主机为联通的202.102.224.68和202.102.227.68
每个用户默认租约为3天(3*24*60*60=259200秒),最长为6天
要分配的IP范围只有192.168.8.101至192.168.8.199,其他IP则保留
2、根据网络参数,编辑DHCP服务配置文件/etc/dhcp/dhcpd.conf如下:
#整体环境设置
ddns-update-style none;
ignore client-update ;
default-lease-time 259200;
max-lease-time 518400;
option routers 192.168.8.254;
option domain-name "centos.me"
option domain-name-servers 202.102.224.68,202.102.227.68;
#IP分配
subnet 192.168.8.0 netmask 255.255.255.0 {
range 192.168.8.101 192.168.8.199;
}
3、dhcp服务器的启动与观察
3.1 启动前要注意
1、linux服务器的网络环境已经设置好
2、防火墙规则已经处理好
如果以上都设置好了那么现在开始启动dhcp
[root@NMS dhcp]# /etc/init.d/dhcpd start
正在启动 dhcpd: [失败]
很不幸!失败了~那么为什么会失败呢?我们来查看下启动日志/var/log/messages
这里面记录了dhcp的启动过程,都发生了什么!
[root@NMS dhcp]# tail -n 30 /var/log/messages
Aug 31 12:25:00 NMS rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1214" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 31 15:40:48 NMS dhcpd: Internet Systems Consortium DHCP Server 4.1.1-P1
Aug 31 15:40:48 NMS dhcpd: Copyright 2004-2010 Internet Systems Consortium.
Aug 31 15:40:48 NMS dhcpd: All rights reserved.
Aug 31 15:40:48 NMS dhcpd: For info, please visit https://www.isc.org/software/dhcp/
Aug 31 15:40:48 NMS dhcpd: /etc/dhcp/dhcpd.conf line 4: expecting allow/deny key
Aug 31 15:40:48 NMS dhcpd: ignore client-update
Aug 31 15:40:48 NMS dhcpd: ^
Aug 31 15:40:48 NMS dhcpd: /etc/dhcp/dhcpd.conf line 4: expecting a parameter or declaration
Aug 31 15:40:48 NMS dhcpd: ignore client-update ;
Aug 31 15:40:48 NMS dhcpd: ^
Aug 31 15:40:48 NMS dhcpd: /etc/dhcp/dhcpd.conf line 9: semicolon expected.
Aug 31 15:40:48 NMS dhcpd: option
Aug 31 15:40:48 NMS dhcpd: ^
Aug 31 15:40:48 NMS dhcpd: Configuration file errors encountered -- exiting
Aug 31 15:40:48 NMS dhcpd:
Aug 31 15:40:48 NMS dhcpd: This version of ISC DHCP is based on the release available
Aug 31 15:40:48 NMS dhcpd: on ftp.isc.org. Features have been added and other changes
Aug 31 15:40:48 NMS dhcpd: have been made to the base software release in order to make
Aug 31 15:40:48 NMS dhcpd: it work better with this distribution.
Aug 31 15:40:48 NMS dhcpd:
Aug 31 15:40:48 NMS dhcpd: Please report for this software via the CentOS Bugs Database:
Aug 31 15:40:48 NMS dhcpd: http://bugs.centos.org/
Aug 31 15:40:48 NMS dhcpd:
Aug 31 15:40:48 NMS dhcpd: exiting.
由日志我们看出来配置文件错误造成dhcp启动失败,“Configuration file errors encountered -- exiting”
那么我来看配置文件,根据提示找出其中的错误;
[root@NMS dhcp]# cat -n /etc/dhcp/dhcpd.conf
1 #整体环境设置
2
3 ddns-update-style none;
4 ignore client-update ;
5 default-lease-time 259200;
6 max-lease-time 518400;
7 option routers 192.168.8.254;
8 option domain-name "centos.me"
9 option domain-name-servers 202.102.224.68,202.102.227.68;
10
11 #IP分配
12
13 subnet 192.168.8.0 netmask 255.255.255.0 {
14 range 192.168.8.101 192.168.8.199;
15 }
16
第一个错误信息表示配置文件第4行有错误,是关键字错误!仔细看是update少了个s应该是updates
Aug 31 15:40:48 NMS dhcpd: /etc/dhcp/dhcpd.conf line 4: expecting allow/deny key
Aug 31 15:40:48 NMS dhcpd: ignore client-update
第二个错误也是在第4行,根据指数符号(^)标注的位置,原来配置项结束后要紧接";",我不小多了个空格,所以报错
Aug 31 15:40:48 NMS dhcpd: /etc/dhcp/dhcpd.conf line 4: expecting a parameter or declaration
Aug 31 15:40:48 NMS dhcpd: ignore client-update ;
第三个错误指出错误在第9行,可以看出我们在最后没有加";"所以报错,semicolon是分号的意思;
Aug 31 15:40:48 NMS dhcpd: /etc/dhcp/dhcpd.conf line 9: semicolon expected.
Aug 31 15:40:48 NMS dhcpd: option
按照日志信息更改后的配置文件如下:
[root@NMS etc]# cat /etc/dhcp/dhcpd.conf -n
1 #整体环境设置
2
3 ddns-update-style none;
4 ignore client-updates;
5 default-lease-time 259200;
6 max-lease-time 518400;
7 option routers 192.168.8.254;
8 option domain-name "centos.me";
9 option domain-name-servers 202.102.224.68,202.102.227.68;
10
11 #IP分配
12
13 subnet 192.168.8.0 netmask 255.255.255.0 {
14 range 192.168.8.101 192.168.8.199;
15 }
16
接下来再次启动DHCP,
[root@NMS etc]# /etc/init.d/dhcpd start
正在启动 dhcpd: [确定]
可以看到这次能启动了。
DHCP使用的端口是port 67,查看端口是否监听;
[root@NMS etc]# netstat -tlunp |grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 17017/dhcpd
再次查看日志文件输出信息
[root@NMS etc]# tail -n 10 /var/log/messages
Aug 31 16:09:16 NMS dhcpd: Internet Systems Consortium DHCP Server 4.1.1-P1
Aug 31 16:09:16 NMS dhcpd: Copyright 2004-2010 Internet Systems Consortium.
Aug 31 16:09:16 NMS dhcpd: All rights reserved.
Aug 31 16:09:16 NMS dhcpd: For info, please visit https://www.isc.org/software/dhcp/
Aug 31 16:09:16 NMS dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
Aug 31 16:09:16 NMS dhcpd: Wrote 0 leases to leases file.
Aug 31 16:09:16 NMS dhcpd: Listening on LPF/eth0/00:0c:29:23:fe:20/192.168.8.0/24
Aug 31 16:09:16 NMS dhcpd: Sending on LPF/eth0/00:0c:29:23:fe:20/192.168.8.0/24
Aug 31 16:09:16 NMS dhcpd: Sending on Socket/fallback/fallback-net
看到这些信息说明成功了!没有报错!
4、服务器端已经准备好,万事俱备只欠东风了就等给客户端服务了!接下来看下客户端的设置
4.1客户端是linux
客户端环境
[root@nmserver-7 ~]# uname -a
Linux nmserver-7.test.com 3.10.0-514.el7.centos.plus.i686 #1 SMP Wed Jan 25 12:55:04 UTC 2017 i686 i686 i386 GNU/Linux
[root@nmserver-7 ~]# cat /etc/redhat-release
CentOS release 7.3.1611 (AltArch)
在客户端修改网卡配置文件,让主机自动取得网络参数;
[root@nmserver-7 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=dhcp
~
~
"/etc/sysconfig/network-scripts/ifcfg-ens33" 5L, 54C
注意:重点是将BOOTPROTO由原来的静态“static“,改为”dhcp“就可通过dhcp服务器来获取网络参数了!
修改配置文件后重启客户端网络
[root@nmserver-7 ~]# systemctl restart network
查看IP,可以看到客户端已经获取到的IP是192.168.8.101/24
[root@nmserver-7 ~]# ifconfig
bridge0: flags=4099
ether b6:83:d4:66:3a:4c txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163
inet 192.168.8.101 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::20c:29ff:fe56:bccf prefixlen 64 scopeid 0x20
ether 00:0c:29:56:bc:cf txqueuelen 1000 (Ethernet)
RX packets 29323 bytes 2119863 (2.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 29052 bytes 2256533 (2.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 2212 bytes 150063 (146.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2212 bytes 150063 (146.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看一下客户端路由
[root@nmserver-7 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.8.254 0.0.0.0 UG 100 0 0 ens33
192.168.8.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
查看客户端DHCP连接状态,
[root@nmserver-7 ~]# netstat -tunlp |grep dhc
udp 0 0 0.0.0.0:68 0.0.0.0:* 4195/dhclient
udp 0 0 0.0.0.0:22424 0.0.0.0:* 4195/dhclient
udp6 0 0 :::56121 :::* 4195/dhclient
查看客户端DNS设置
[root@nmserver-7 dhclient]# cat /etc/resolv.conf
# Generated by NetworkManager
search centos.me test.com
nameserver 202.102.224.68
nameserver 202.102.227.68
通过以上信息可以看到客户端获取的网络参数与之前DHCP服务器上设置的一致!!
那么在客户端网络重启的过程中,DHCP服务器都做了什么呢?我们来看下DHCP服务器的日志信息,可以清楚看到
DHCP协议工作的4个过程:
[root@NMS etc]# tail -n 10 /var/log/messages
Aug 31 16:09:16 NMS dhcpd: Listening on LPF/eth0/00:0c:29:23:fe:20/192.168.8.0/24
Aug 31 16:09:16 NMS dhcpd: Sending on LPF/eth0/00:0c:29:23:fe:20/192.168.8.0/24
Aug 31 16:09:16 NMS dhcpd: Sending on Socket/fallback/fallback-net
Aug 31 16:52:21 NMS dhcpd: DHCPDISCOVER from 00:0c:29:56:bc:cf via eth0 #IP租用请求
Aug 31 16:52:22 NMS dhcpd: DHCPOFFER on 192.168.8.101 to 00:0c:29:56:bc:cf (nmserver-7) via eth0 #IP租用提供
Aug 31 16:52:22 NMS dhcpd: DHCPREQUEST for 192.168.8.101 (192.168.8.6) from 00:0c:29:56:bc:cf (nmserver-7) via eth0 #IP租用选择
Aug 31 16:52:22 NMS dhcpd: DHCPACK on 192.168.8.101 to 00:0c:29:56:bc:cf (nmserver-7) via eth0 #IP租用确认
查看DHCP服务器与客户端的租约信息,
[root@NMS etc]# cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.1.1-P1
lease 192.168.8.101 {
starts 4 2017/08/31 09:22:42;
ends 0 2017/09/03 09:22:42;
cltt 4 2017/08/31 09:22:42;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:56:bc:cf;
client-hostname "nmserver-7";
}
server-duid "\000\001\000\001!:\200\254\000\014)#\376 ";
从这个文件里面我们就知道有多少个客户端已经向DHCP申请IP了。目前只有一个客户端:
客户端主机名:nmserver-7
MAC地址:00:0c:29:56:bc:cf
客户端IP:192.168.8.101