本文介绍DHCP服务器的工作方式及配置方法
1. DHCP服务器的作用
把一个主机接入TCP/IP网络,要为主机配置的网络参数:
IP/mask:
Gateway:网关(可完成网络间通讯)
DNS Server:
Wins Server , NTP Server
配置这些参数的方式:
手动配置
动态分配
早期:bootp
现在:dhcp 引入了“租约”的bootp。也可以实现为特定主机保留固定地址。
2. DHCP的报文格式
DHCP DISCOVER: 客户端广播,请求获取IP地址
DHCP OFFER : 服务器到客户端:给客户端分配地址
DHCP REQUEST: 客户端到服务器:确认使用IP地址
DHCP ACK : 服务器到客户端:确认信息
DHCP NAK: 服务器到客户端,通知用户无法分配合适的IP地址:一般为地址池空
DHCP DECLINE : 客户端到服务器,指示地址已被使用:地址冲突
DHCP RELEASE: 客户端到服务器,放弃网络地址和取消剩余的租约时间:客户端释放地址
DHCP INFORM: 客户端到服务器, 客户端如果需要从DHCP服务器端获取更为详细的配置信息,则发送Inform报文向服务器进行请求,极少用到
3. DHCP的工作流程
工作流程:
1. 客户端:发请求报文 dhcp discover
2. 服务端:dhcp offer (提供ip/mask , gw ……)
发送的dhcp offer中对ip地址有效期有租约期限:lease time(最长2天)
3. 客户端:dhcp request 向服务端发确认请求
4. 服务嚣:dhcp ack 服务端确认
DHCP的租约:
假如租约为2hours:
租约还剩50%时,向DHCP服务器发送续约请求(dhcp request),若DHCP无响应,租约还剩25%时(50%的一半),客户端再向DHCP服务嚣发送续约请求,若DHCP还是无响应,租约剩12.5%时,客户端再向本网络发送dhcp discover请求。
4. DHCP服务的实现
dhcp:由ISC提供,只提供dhcp服务(本文仅讨论本实现)
dnsmasq:可提供dhcp服务和dns服务(云计算时会用到)
dhcp:
dhcpd:提供dhcp服务
dhcrelay:提供中继服务(中继与dhcp服务两者不能同时提供)
服务的启动:
CentOS 6 : chkconfig dhcpd on ; service dhcpd start
CentOS 7: systemctl start dhcpd
DHCP服务监听的端口:
服务端:67UDP端口
客户端:68UDP端口
5. 配置文件
dhcp服务端配置文件默认为/etc/dhcpd/dhcpd.conf文件,但是文件里只有以下这段话:
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
可从/usr/share/doc/dhcp(release-version)/下复制一份配置文件,改名为/etc/dhcpd/dhcpd.conf再编辑。
其中option为全局配置文件,也可在subnet中指定,如在subnet中指定,以subnet中的为准。
下面是一份精简的配置文件:
option domain-name "lxk.com"; #指明DHCP服务器名称
option domain-name-servers 8.8.8.8; #指明DNS服务器地址
default-lease-time 60000; #默认租约
max-lease-time 720000; #最长约租
log-facility local7;
subnet 192.168.200.0 netmask 255.255.255.0 { #网段及掩码
range 192.168.200.10 192.168.200.200; #分配的地址段范围
option routers 192.168.200.254; #默认路由
next-server 192.168.200.254; #PXE启动时下一个服务器的地址
filename "pxelinux.0"; #要加载的PXE启动的文件
}
其它选项:
filename: 指明引导文件名称;
The filename statement can be used to specify the name of the initial boot file which is to be loaded by a client. The filename should be a filename recognizable to whatever file transfer protocol the client can be expected to use to load the file.
next-server:提供引导文件所在的服务器主机的IP地址
The next-server statement is used to specify the host address of the server from which the initial boot file (specified in the filename statement) is to be loaded. Server-name should be a numeric IP address or a domain name. If no next-server statement applies to a given client, the address 0.0.0.0 is used.
6. DHCP客户端测试工具
DHCP测试工具:
dhclient:DHCP客户端测试
-d:Force dhclient to run as a foreground process 前端运行
7. 服务端查看地址分配记录
[root@centos7 pxeboot]# 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.2.5
lease 192.168.200.10 {
starts 6 2018/04/14 15:15:52;
ends 0 2018/04/15 07:55:52;
tstp 0 2018/04/15 07:55:52;
cltt 6 2018/04/14 15:15:52;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:b5:04:a3;
}
lease 192.168.200.14 {
starts 0 2018/04/15 03:01:48;
ends 0 2018/04/15 19:41:48;
tstp 0 2018/04/15 19:41:48;
cltt 0 2018/04/15 03:01:48;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:85:af:9c;
转载于:https://blog.51cto.com/11975865/2103681