DHCP服务相关配置

DHCP:动态主机配置协议采用c/s方式工作,DHCPclient 向DHCPserver动态请求配置信息,DHCP server根据策略返回相应的配置。

DHCP的工作模式:

DHCP客户端首次登陆网络时,主要通过4个阶段与DHCP服务器建立联系。
发现阶段:DHCP客户端寻找DHCP服务器阶段。客户端以广播方式发送DHCP_Discover报文,只有DHCP服务器才会进行响应。

提供阶段:DHCP服务器提供IP地址的阶段。DHCP服务器接收到客户端的DHCP_Discover报文后,从IP地址池中挑选一个尚未分配的IP地址分配给客户端,向该客户端发送包含出租IP地址和其他设置的DHCP_Offer报文。

选择阶段:DHCP客户端选择IP地址的阶段。如果有多台DHCP服务器向该客户端发来DHCP_Offer报文,客户端只接受第一个收到的DHCP_Offer报文,然后以广播方式向各DHCP服务器回应DHCP_Request报文。

确认阶段:DHCP服务器确认所提供IP地址的阶段。当DHCP服务器收到DHCP客户端回答的DHCP_Request报文后,便向客户端发送包含它所提供的IP地址和其他设置的DHCP_ACK确认报文

DHCP的配置:

[root@node2 ~]# yum install dhcp -y   安装dhcp的软件包,依赖epel源
[root@node4 ~]# cd /etc/dhcp/
[root@node4 dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
[root@node4 dhcp]# cat dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@node4 dhcp]# cp /usr/share/doc/dhcp-4.2.5/
dhcpd6.conf.example  dhcpd.conf.example   ldap/                
[root@node4 dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example ./dhcpd
dhcpd6.conf  dhcpd.conf   
[root@node4 dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example ./dhcpd.conf   复制dhcp的配置文件到dhcp.conf中。
cp: overwrite ‘./dhcpd.conf’? y
[root@node4 dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts

配置DHCP的配置文件。使得成为DHCP服务器分配地址。

# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
 
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
 
default-lease-time 600;    默认租用期限
max-lease-time 7200;       默认租用最大期限。
 
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
 
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
 
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
 
# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.
 
subnet 192.168.20.0 netmask 255.255.255.0 {    多个subnet多个子网。这表示为192.168.20.0网段分配掩码。现在只为一个网段分配地址
    range 192.168.20.10 192.168.20.30;   这表示分配地址的范围。
    option routers 192.168.20.7;   这表示依据选取的路由。可以定义到全局段,现在只对当前段有效。
}
[root@node2 dhcp]# systemctl restart dhcpd  重启dhcp服务。

找一台客户端分配地址进行测试。通过dhcpclient 命令

[root@localhost ~]# dhclient -d
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth1/00:0c:29:c3:9e:71
Sending on   LPF/eth1/00:0c:29:c3:9e:71
Listening on LPF/eth0/00:0c:29:c3:9e:67
Sending on   LPF/eth0/00:0c:29:c3:9e:67
Sending on   Socket/fallback
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 8 (xid=0x7a8449f6)
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x89cc346)
DHCPACK from 10.5.100.254 (xid=0x89cc346)
bound to 10.5.100.94 -- renewal in 309310 seconds.
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 12 (xid=0x7a8449f6)
DHCPOFFER from 192.168.20.7
DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0x7a8449f6)
DHCPACK from 192.168.20.7 (xid=0x7a8449f6)     通过这个DHCPF服务器地址
bound to 192.168.20.11 -- renewal in 286 seconds.   为这个客户端主机分配的地址
使用ctrl+c结束。

通过服务器端查看DHCP服务端的分配记录。

[root@node2 dhcp]# cat /var/lib/dhcpd/dhcpd.leases  这个文件记录了dhcp的分配记录
# 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.20.10 {
  starts 4 2020/01/09 12:12:14;
  ends 4 2020/01/09 12:22:14;
  tstp 4 2020/01/09 12:22:14;
  cltt 4 2020/01/09 12:12:14;
  binding state free;
  hardware ethernet 00:0c:29:c3:9e:71;
}
server-duid "\000\001\000\001%\251\324\214\000\014)\255\257\352";

lease 192.168.20.10 {
  starts 5 2020/01/10 05:07:40;
  ends 5 2020/01/10 05:07:40;
  cltt 5 2020/01/10 05:07:40;
  binding state abandoned;
  next binding state free;
  rewind binding state free;
}
lease 192.168.20.11 {
  starts 5 2020/01/10 05:07:50;
  ends 5 2020/01/10 05:17:50;
  cltt 5 2020/01/10 05:07:50;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:c3:9e:71;
}
lease 192.168.20.10 {
  starts 5 2020/01/10 05:07:40;
  ends 5 2020/01/10 05:07:40;
  cltt 5 2020/01/10 05:07:40;
  binding state free;
}

DHCP基本服务搭建完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值