Linux网络服务DHCP详解(我这一生都是坚定不移的唯物主义者,唯有你,我希望有来生)

目录

一、DHCP工作原理

1.1 DHCP原理

1.2 使用DHCP的好处

1.3 DHCP的分配方式

1.4 DHCP中继原理

1.5 配置文件术语

二、实验:DHCP服务器配置

2.1 自动获取

2.2 根据物理地址获取固定地址

2.3 DHCP中继

2.3.1 实验要求

2.3.2 实验过程

三、 总结


一、DHCP工作原理

1.1 DHCP原理

        动态主机配置协议(DHCP)是一种基于udp协议且仅限于在局域网内部使用的网络协议,主要用于大型的局域网环境或者存在较多移动办公设备的局域网环境中,其主要用途是为局域网内部的设备或网络供应商自动分配IP地址等参数。

其他原理部分移步至我的另外两篇博客

什么是DHCP(接口模式+全局模式)

什么是DHCP(中继模式)

1.2 使用DHCP的好处

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

1.3 DHCP的分配方式

  1. 自动分配:分配到一个IP地址后永久使用
  2. 手动分配:由DHCP服务器管理员专门指定IP地址
  3. 动态分配:使用完后释放该IP地址,供其他客户机使用

1.4 DHCP中继原理

当企业的内部网络规模较大时,通常被划分为多个不同的子网,网络内配置了vlan,vlan能隔离广播而DHCP协议使用广播,如果DHCP服务器在vlan100中,就只有vlan100内的客户机能从这台DHCP服务器上获取IP地址,如果vlan2或vlan3中的客户机也要使用这台DHCP服务器怎么办:

  1. 为每一个网段安装一台DHCP服务器,但这种方式存在资源上的浪费,而且不利于集中管理
  2. 在连接不同网段的设备上开启DHCP中继功能,将DHCP这种特殊的广播信息在vlan之间转发,让其他vlan的客户机也能从DHCP服务器获得IP地址

1.5 配置文件术语

作用域:一个完整的IP地址段,DHCP协议根据作用域来管理网络的分布、分配IP地址及其他配置参数

超级作用域:用于管理处同一个物理网络中的多逻辑子网段。超级作域包含了可以统一管理的作用域列表

排除范围:把作用域中的某些IP地址排除,确保这些IP地址不会分配给DHCP客户端

地址池:在定义了DHCP的作用域并应用了排除范围后,剩下来动态分配的IP地址范围

租约:客户端使用IP地址的时间

预约:保证网络中的特定设备总是获取到相同的IP地址

DHCP的配置文件位于/etc/dhcp/dhcpd.conf,这个配置文件里面本身是没有内容的,需要我们手写,我们当然没有这个水平,所以可以参考/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example这个文件,直接复制过去,然后修改就可。


# 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;   #最大租约时间


#全局配置dns

# 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 10.152.187.0 netmask 255.255.255.0 {
}

#子网网段声明

# This is a very basic subnet declaration.

subnet 10.254.239.0 netmask 255.255.255.224 {
  range 10.254.239.10 10.254.239.20;
  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}


#动态获取地址

# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.

subnet 10.254.239.32 netmask 255.255.255.224 {
  range dynamic-bootp 10.254.239.40 10.254.239.60;
  option broadcast-address 10.254.239.31;
  option routers rtr-239-32-1.example.org;
}

#内部子网配置

# 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;
}

# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.

host passacaglia {
  hardware ethernet 0:0:c0:5d:bd:95;
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}

# Fixed IP addresses can also be specified for hosts.   These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP.   Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.


#根据物理地址固定获取
host fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;
}

# You can declare a class of clients and then do address allocation
# based on that.   The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.

class "foo" {
  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}

shared-network 224-29 {
  subnet 10.17.224.0 netmask 255.255.255.0 {
    option routers rtr-224.example.org;
  }
  subnet 10.0.29.0 netmask 255.255.255.0 {
    option routers rtr-29.example.org;
  }
  pool {
    allow members of "foo";
    range 10.17.224.10 10.17.224.250;
  }
  pool {
    deny members of "foo";
    range 10.0.29.10 10.0.29.230;
  }
}

二、实验:DHCP服务器配置

2.1 自动获取

1.先在有网环境下安装DHCP软件包

2.查看仅主机模式下的子网地址

3.此时xshell断链,我们进入虚拟机重新修改网卡配置文件

我们在做DHCP实验的时候用不到网络,所以将DNS注释掉,不注释也没关系

 4.用仅主机下的IP地址连接xshell

 5.查看配置文件

 6.修改配置文件

vim dhcpd.conf

每一行后面一定要加分号,且每个字母都不能敲错,否则会启动服务失败,截图中DNS后面我敲了冒号,导致服务起不来,我已经改了

7.启动服务

 8.开启win10虚拟机测试,也改为仅主机模式

2.2 根据物理地址获取固定地址

1.查看win10的mac地址,同样也是要在仅主机模式下

 2.依然是修改centos7-1的配置文件,重启服务,设置的IP地址与上个实验设置的地址池没有关系

 3.重启服务

 4.在win10中测试

2.3 DHCP中继

2.3.1 实验要求

sw2只做转发功能,sw1具有中继功能

2.3.2 实验过程

1.网络拓扑

2. 修改配置,cloud相当于是Linux服务器

 

两台pc都改为DHCP获取并应用 

3.sw2配置,可以当做脚本使用

u t m
sys
vlan batch 10 20 102
int e0/0/2
port link-type access
port default vlan 10
int e0/0/3
port link-type access
port default vlan 20
int e0/0/4
port link-type access
port default vlan 102
int e0/0/1
port link-type trunk
port trunk allow-pass vlan all

4.sw1

u t m
sys
vlan batch 10 20 102
dhcp enable
int g0/0/1
port link-type trunk
port trunk allow-pass vlan all
int vlan 10
ip add 192.168.10.1 24
dhcp select relay
dhcp relay server 192.168.102.131
int vlan 20
ip add 192.168.20.1 24
dhcp select relay
dhcp relay server 192.168.102.131
int vlan 102
ip add 192.168.102.10 24
dhcp select relay
dhcp relay server 192.168.102.131


#
interface Vlanif10
 ip address 192.168.10.1 255.255.255.0
 dhcp select relay
 dhcp relay server-ip 192.168.102.131
#

5.DHCP网卡配置

 

6.测试连通性

 7.修改DHCP服务器的配置文件

vim /etc/dhcp/dhcpd.conf

 8.重启服务

[root@localhost ~]# systemctl restart dhcpd

9.测试

三、 总结

  1. 安装yum install dhcp -y
  2. 网卡模式调为仅主机模式
  3. 将配置文件/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example    复制到 dhcpd.conf
  4. 修改配置文件

subnet 192.168.102.0 netmask 255.255.255.0 {

#####你的网段
  range 192.168.102.20 192.168.102.70;

#####你的地址池
  option routers 192.168.102.1;

#####你的网关
  option domain-name-servers 114.114.114.114;

#####可以不写   DNS服务器
}

摘抄刘遄的《Linux就该这么学》书后的几道复习题

1.DHCP协议的主要用途?

答:为局域网内部的设备或网络供应商自动分配IP地址等参数

2.DHCP协议能为客户机分配什么网卡资源?

答:IP地址、子网掩码、网关地址以及DNS

3.真正供用户使用的IP地址范围是作用域还是地址池?

答:地址池,因为作用域还包括了要排除掉的IP地址

4.把IP地址与主机的什么信息绑定,就可以保证该主机一直获取到固定的IP地址

答:主机网卡的MAC地址

这篇博客做一半我把CSDN改名了,所以截图会出现两个署名,并不是抄袭别人的,哈哈

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Steve lu

感谢大佬的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值