DHCP服务
网络配置
静态指定
动态获取: bootp:boot protocol MAC与IP一一静态对应
dhcp:增强的bootp,动态
DHCP: (Dynamic Host Configuration Protocol)
动态主机配置协议
局域网协议,UDP协议
主要用途:
用于内部网络和网络服务供应商自动分配IP地址给用户
用于内部网络管理员作为对所有电脑作集中管理的手段
使用场景
自动化安装系统
解决IPV4资源不足问题
DHCP共有八种报文
- DHCP DISCOVER:客户端到服务器
- DHCP OFFER :服务器到客户端
- DHCP REQUEST:客户端到服务器
- DHCP ACK :服务器到客户端
- DHCP NAK:服务器到客户端,通知用户无法分配合适的IP地址
- DHCP DECLINE :客户端到服务器,指示地址已被使用
- DHCP RELEASE:客户端到服务器,放弃网络地址和取消剩余的租约时间
- DHCP INFORM:客户端到服务器,客户端如果需要从DHCP服务器端获取更为详细的配置信息,则发送Inform报文向服务器进行请求,极少用到
续租
- 50% :租赁时间达到50%时来续租,刚向DHCP服务器发向新的DHCPREQUEST请求。如果dhcp服务没有拒绝的理由,则回应DHCPACK信息。当DHCP客户端收到该应答信息后,就重新开始新的租用周期
- 87.5%:如果之前DHCP Server没有回应续租请求,等到租约期的7/8时,主机会再发送一次广播请求
DHCP服务简介
同网段多DHCP服务(广播机制)
DHCP服务必须基于本地
先到先得的原则
相关协议
Arp
rarp
跨网段
RFC 1542 Compliant Routers
dhcrelay: 中继代理
DHCP实现
Linux DHCP协议的实现程序:dhcp, dnsmasq(dhcp,dns)
Dhcp Server
/usr/sbin/dhcpd
/etc/dhcp/dhcpd.conf--> /etc/rc.d/init.d/dhcpd
/etc/dhcp/dhcpd6.conf--> /etc/rc.d/init.d/dhcpd6
/usr/sbin/dhcrelay
/etc/rc.d/init.d/dhcrelay
dhcp server:67/udp
dhcp client: 68/udp
dhcpv6 client:546/udp
Dhcp client
dhclient
自动获取的IP信息:/var/lib/dhclient
DHCP配置文件
/etc/dhcp/dhcpd.conf:
帮助参考:man 5 dhcpd.conf
全局配置
subnet {
...
}
host {
}
地址分配记录
/var/lib/dhcpd/dhcpd.leases
dhcpd.conf示例
option domain-name "magedu.com";
option domain-name-servers 192.168.0.1,8.8.8.8;
default-lease-time 86400;
max-lease-time 86400;
subnet 192.168.100.0 netmask 255.255.255.0 {
range 192.168.100.1 192.168.100.200;
option routers 192.168.100.1;
}
搭建DHCP实验
yum install dhcp
rpm -ql dhcp
/etc/dhcp/dhcpd.conf
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
编写/etc/dhcp/dhcpd.conf文件
[root@centos7 ~]#cat /etc/dhcp/dhcpd.conf
option domain-name "zhangqifei.com";
option domain-name-servers 114.114.114.114,8.8.8.8;
option routers 192.168.109.1;
default-lease-time 84600;
max-lease-time 84600;
log-facility local7;
subnet 192.168.109.0 netmask 255.255.255.0 {
range 192.168.109.100 192.168.109.200;
filename "pxelinux.0";/文件名网络中启动程序的文件名
next-server 192.168.100.100;(tftp)
}
host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address 192.168.109.101;
这里网关和DNS都可以单独设置
}
启动服务 systemctl start dhcpd
查看端口 ss -ntlup
查看网关 route -n
查看DNS cat /etc/resolv.conf
dhcp数据库 /var/lib/dhcpd/dhcpd.leases
[root@centos7 ~]#cat /var/lib/dhcpd/dhcpd.leases
#lease 192.168.109.100 {
starts 6 2018/01/13 06:41:45;
ends 0 2018/01/14 06:11:45;
tstp 0 2018/01/14 06:11:45;
cltt 6 2018/01/13 06:41:45;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:12:6c:bc;
}
[root@centos7 ~]#cat /var/lib/dhclient/dhclient-eth0.leases
DHCP配置文件
其它配置选项:
-
filename: 指明引导文件名称
-
next-server:提供引导文件的服务器IP地址
示例:
-
filename “pxelinux.0”;
-
next-server 192.168.100.100;(tftp)
检查语法
-
service dhcpd configtest(centos6)