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<UP,BROADCAST,MULTICAST> mtu 1500
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<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
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<link>
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<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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