DHCP原理和配置

DHCP原理

DHCP的工作原理

在这里插入图片描述
1)客户端请求IP地址–DHCP客户端在网络广播中一个DHCP Discover,请求IP地址,DHCP Discover包的源地址为0.0.0.0。目的地址为255.255.255.255
2)服务器响应请求–当DHCP服务器接收到客户端请求IP地址信息时,就在自己的 库中查找是否有合法的IP地址提供给客户,如果有,将此IP标记,广播一个DHCP offer包。这个包中包含:客户端的MAC地址;提供的合法IP;子网掩码;租约期限;服务器标示;其他参数等。因为客户端没有IP地址,所以还是以广播方式发送,源地址:0.0.0.0目的地址255.255.255.255。
3)客户端选择IP地址–DHCP客户端接收到第一个DHCP offer包中选择IP地址,并再次广播一个DHCP request包到所有服务器,该包中包含为客户端提供的IP配置的服务器的服务标示符(IP地址),服务器查看标示符,以确定自己提供的IP地址是否被IP地址是否被客户端选中
4)服务器确认IP 租约–DHCP租约的最后一步,服务器确认租约,发送一个DHCP ack/DHCP NACK包。服务器收到DHCP request包后,以DHCP ack包向客户端广播出去,当客户端收到后,就配置了IP地址

使用DHCP的好处

减少管理员的工作量
避免输入错误的可能
避免IP地址冲突
当网络更改IP地址段时,不需要再重新配置每个用户的IP地址
提高了IP地址的利用率
方便客户端的配置

DHCP的分配方式

自动分配
自动分配是当DHCP客户机第一次成功地从DHCP服务器获取到一个IP地址后,就永久地使用这个地址

手动分配
手动分配是由DHCP服务器管理员专门指定IP地址

动态分配
动态分配是当DHCP客户机第一次从DHCP服务器获取到IP地址后,并非永久地使用该地址,而是在每次使用后,DHCP客户机就会释放这个IP地址,供其他客户机使用

模拟配置DHCP

环境:

DHCP服务器:虚拟机centos7.6
DHCP中继服务器 :ensp 三层交换机
DHCP客户机 :ensp的pc机

拓扑
在这里插入图片描述

要求:

centos7 做DHCP服务器
三层交换机 做DHCP中继
pc2,pc3 动态获取地址
pc1 获取固定地址

1)首先查看是否安装DHCP(未安装可以使用yum -y install dhcp下载安装)

[root@localhost ~]# rpm -q dhcp
dhcp-4.2.5-82.el7.centos.x86_64
[root@localhost ~]# 

2)查看配置文件
/etc/dhcp/dhcpd.conf

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf

#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example  //我们将模板复制到此配置文件
#   see dhcpd.conf(5) man page
#

3)将模板复制到配置文件

[root@localhost ~]# cat /usr/share/doc/dhcp*/dhcpd.conf.example > /etc/dhcp/dhcpd.conf 
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf  //重新查看配置文件,发现内容已复制

# 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

4)修改DHCP配置文件,添加我们需要配置网段

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf

# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";  //全局配置本地域名(非dns)
option domain-name-servers ns1.example.org, ns2.example.org;   //dns

default-lease-time 600;  //默认租约时间
max-lease-time 7200;  //最大租约时间



# A slightly different configuration for an internal subnet.
subnet 10.5.5.0 netmask 255.255.255.224 {    //配置模板,无需修改
  range 10.5.5.26 10.5.5.30;
  option domain-name-servers ns1.internal.example.org;
  option domain-name "internal.example.org";
  option routers 10.5.5.1;
  option broadcast-address 10.5.5.31;
  default-lease-time 600;
  max-lease-time 7200;
}
subnet 192.168.213.0 netmask 255.255.255.0 {    //需要配置和网卡地址在同一网段的地址池,否则会报错
  range 192.168.213.100 192.168.213.200;
}
subnet 192.168.10.0 netmask 255.255.255.0 {  //配置vlan10的地址池
  range 192.168.10.10 192.168.10.20;
  option routers 192.168.10.1;
}
subnet 192.168.20.0 netmask 255.255.255.0 {   //配置vlan20的地址池
  range 192.168.20.10 192.168.20.20;
  option routers 192.168.20.1;
}

host fantasia {   //模板
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;
}
host pc1 {             //按模板配置给pc1固定ip  
  hardware ethernet 54:89:98:0b:21:40;
  fixed-address 192.168.10.100;
}

5)划分vlan,配置中继
三层交换机配置
在这里插入图片描述

interface Vlanif10
 ip address 192.168.10.1 255.255.255.0
 dhcp select relay
 dhcp relay server-ip 192.168.213.2
#
interface Vlanif20
 ip address 192.168.20.1 255.255.255.0
 dhcp select relay
 dhcp relay server-ip 192.168.213.2
#
interface Vlanif213
 ip address 192.168.213.1 255.255.255.0
#
interface MEth0/0/1
#
interface GigabitEthernet0/0/1
 port link-type trunk
 port trunk allow-pass vlan 2 to 4094

二层交换机配置

interface Ethernet0/0/1
 port link-type access
 port default vlan 10
#
interface Ethernet0/0/2
 port link-type access
 port default vlan 20
#
interface Ethernet0/0/3
 port link-type trunk
 port trunk allow-pass vlan 2 to 4094
#
interface Ethernet0/0/4
 port link-type access
 port default vlan 213
#
interface Ethernet0/0/5
 port link-type access
 port default vlan 10

6)验证结果
pc1
在这里插入图片描述
pc2
在这里插入图片描述

pc3
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值