Linux网络—DHCP原理与配置
一:DHCP工作原理
1.1:DHCP概述
- DHCP(Dynamic Host Configuration Protocol,动态主机配置协议),是由Internet工作任务小组设计开发的,专门用于为TCP/IP网络中的计算机自动分配TCP/IP参数的协议
DHCP一般作用在局域网中,若在广域网中使用DHCP,需要在路由器上面配置
1.2:使用DHCP好处
-
Internet的用户数量众多,所以不可能给每个用户固定一个IP地址,同时也为了避免浪费,我们使用DHCP服务来解决这个问题。具有以下好处:
-
减少管理员的工作量
-
避免IP地址冲突
-
当网络更改IP地址段时,不需要再重新配置每个用户的IP地址
-
提高了IP地址的利用率
-
方便客户端的配置
1.3:DHCP的分配方式
-
DHCP的分配方式
-
自动分配:
分配到一个ip地址后永久使用
当DHCP客户机第一次成功的从DHCP服务器获取到一个IP地址后,就永久的使用这个IP地址,固定的IP地址
如 打印机
-
手动分配:由DHCP服务器管理员专门指定的IP地址
-
动态分配:使用完后释放该IP,供其它客户机使用
当DHCP客户机第一次从DHCP服务器获得IP地址后,并非永久的使用该地址,而是在每次使用完后,DHCP客户机就会释放这个IP地址,供其他客户机使用
二:DHCP的工作原理
2.1:租约过程概述
- 客户机从DHCP服务器获得IP地址的过程称为DHCP的租约过程
- 租约过程分为四个步骤
1: discover 请求ip地址 客户—》服务器 广播
2:offer 服务器回应可用ip信息(可用ip,子网掩码,网关,dns,租约期限) 服务器—》客户端 广播(客户端没有地址只能广播)
3:request 客户端请求确认可用IP(是否真实可用) 客户–》服务端 广播
4:ack 服务端确认IP可用 服务端—》客户 广播
-
特殊情况1:如果offer 中的ip被占用,直接回应noack, 然后再回到discover
-
特殊情况2:比如租约期限 8 天 租约期限过半 续约时间在第5天,客户端发送单播,request ack完成,继续使用,没到期之前就可以用
-
特殊情况3:没有可用DHCP服务器的时候你就会拿到,169.254.0.0/24 这个地址,这个没啥用
-
注意:offer里的IP,客户端不一定能使用,只有request ack 完成后才能真正使用
三:DHCP服务器的配置
3.1:DHCP服务
-
为大量客户机自动分配地址,提供集中管理
减轻管理和维护成本,提高网络配置效率 -
可分配的地址信息主要包括
- 网卡的IP地址,子网掩码
- 对应的网络地址,广播地址
- 默认网关地址
- DNS服务器地址
3.2:安装DHCP服务器
- DHCP服务器软件
- centos镜像中的dhcp…rpm
- dhcp软件包的主要文件
- 主配置文件:/etc/dhcpd.conf
- 执行程序:/usr/sbin/dhcpd,/usr/sbin/dhcrelay
- 执行参数配置:/etc/sysconfig/dhcpd
- 注意:任何服务的核心文件是命令脚本文件和配置文件
3.4:DHCP主配置文件详解
3.4.1:dhcpd.conf的内容构成
ddns-update-style interim; '全局配置参数,针对所有子网段'
...省略内容
subnet 10.152.187.0 netmask 255.255.255.0 {'网段声明'
option routers 10.5.5.1; '配置选项,网关'
...
default-lease-time 600; '配置参数,租约'
...
host ns{
...
fixed-address 207.175.42.254; '主机声明,固定的主机地址'
}
}
3.4.2:全局设置,作用于整个DHCP服务器
配置 | 解释 |
---|---|
ddns-update-style none; | 禁止dns动态更新 |
default-lease-time 21600; | 默认租约期限 |
max-lease-time 43200; | 最大租约期限 |
option domain-name"domain.org"; | 指定域名 |
option domain-name-servers 202.106.0.20; | 指定域名解析DNS地址 |
3.4.3:subnet网段声明,作用于整个子网段
- range参数:设置用于分配的IP地址池
- option subnet-mask 参数: 设置客户机的子网掩码
- option routers 参数: 设置客户机的默认网关地址
[root@localhost dhcp]# vim /etc/dhcp/dhcpd.conf
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;
}
3.4.4:host主机声明,作用于单个主机
hardware ethernet 参数:指定对应主机的MAC地址
fixed-address参数:指定为该主机保留的IP地址
3.4.5:查看租约文件
启动DHCP
查看租约文件 /var/lib/dhcpd/dhcpd.lease
[root@localhost ~]# vim /var/lib/dhcpd/dhcpd.leases
或者
[root@localhost ~]# less /var/lib/dhcpd/dhcpd.leases
或者其他的
四:DHCP客户端的使用方法
4.1:修改网卡配置文件(如ifcfg-ens33)
- 其中BOOTPROTO=dhcp
[root@localhost dhcpd]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
...省略内容
4.2:使用dhclient命令
命令基本格式
dhclient [-d] [网络接口名]
例如
[root@localhost ~]# dhclient -d ens33
五:DHCP实验
5.1:DHCP配置命令
命令基本格式
ip helper-address HDCPsrv-IPAddress
(HDCPsrv-IPAddress为DHCP服务器的IP地址)
5.2:实验环境
一台CentOS 7.6做DHCP服务器
一台WIN10做客户端
5.3:实验步骤
规划:
1.CentOS设置DHCP固定IP地址
2.安装DHCP包
3.修改配置文件(see 根据模板设置)
设置 subnet {地址池,子网掩码,网关,dns地址}
4.启动服务
客户端:ipconfig /release; ipconfig /renew
5.3.1:CentOS设置DHCP固定IP地址
[root@lpf dhcp]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #设置静态
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=e927c915-1f32-4f8e-b4d4-77c7a3ed4823
DEVICE=ens33
ONBOOT=yes
IPADDR=20.0.0.41 #配地址
PREFIX=24
GATEWAY=20.0.0.2
DNS1=8.8.8.8
5.3.2:关闭防火墙
sentenforce 0 安全功能关闭
iptables -F 清空防火墙规则
5.3.3:安装DHCP
[root@lpf ~]# yum install dhcp* -y
[root@lpf ~]# rpm -q dhcp
dhcp-4.2.5-79.el7.centos.x86_64
[root@lpf ~]# rpm -qc dhcp
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases
5.3.4:修改配置文件(see 根据模板设置)
[root@lpf ~]# cd /etc/dhcp
[root@lpf dhcp]# ls
dhclient.d dhcpd6.conf scripts
dhclient-exit-hooks.d dhcpd.conf
[root@lpf dhcp]# vim dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
"dhcpd.conf" 5L, 117C
#模板在/usr/share/doc/dhcp*/dhcpd.conf.example
[root@lpf dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? yes
[root@lpf dhcp]# ll
总用量 8
drwxr-xr-x. 2 root root 37 4月 2 21:29 dhclient.d
drwxr-xr-x. 2 root root 28 7月 3 16:19 dhclient-exit-hooks.d
-rw-r--r--. 1 root root 120 4月 2 21:29 dhcpd6.conf
-rw-r--r--. 1 root root 3262 7月 3 16:46 dhcpd.conf
drwxr-x---. 2 root dhcpd 28 7月 3 16:19 scripts
5.3.5:修改配置文件 全局设置与子项设置
[root@lpf dhcp]# vi dhcpd.conf
......
3 # Sample configuration file for ISC dhcpd
4 #
7 option domain-name "example.org"
8 option domain-name-servers 8.8.8.8;
9 default-lease-time 600;
10 max-lease-time 7200;
..........
#上面是全局设置
27 subnet 20.0.0.0 netmask 255.255.255.0 {
28 range 20.0.0.128 20.0.0.200;
29 option routers 20.0.0.2;
30 }
#这里是局部配置
[root@lpf dhcp]# systemctl start dhcpd
[root@lpf dhcp]# netstat -nuap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:5353 0.0.0.0:* 8553/avahi-daemon:
udp 0 0 0.0.0.0:48428 0.0.0.0:* 8553/avahi-daemon:
udp 0 0 192.168.122.1:53 0.0.0.0:* 9576/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 10924/dhcpd
udp 0 0 0.0.0.0:67 0.0.0.0:* 9576/dnsmasq
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
udp 0 0 0.0.0.0:659 0.0.0.0:* 8545/rpcbind
udp6 0 0 :::111 :::* 1/systemd
udp6 0 0 :::659 :::* 8545/rpcbind
检查服务已经开启
5.3.6:Centos 7.6 和WIN 10都采用仅主机模式绑定VM1
5.3.7:WIN 10客户端自动获取地址
5.3.8:更改子项的DHCP服务器地址,对比看全局和子项哪个优先生效呢?
[root@lpf lpf]# vi /etc/dhcp/dhcpd.conf
.......
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 8.8.8.8;
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 20.0.0.0 netmask 255.255.255.0 {
range 20.0.0.128 20.0.0.200;
option routers 20.0.0.1;
option domain-name-servers 9.9.9.9; #更改子项DNS服务器地址
}
[root@lpf lpf]# systemctl restart dhcpd
实验证明子项优先生效,此处忘记截图
5.3.9:给用户分配固定IP地址
[root@lpf lpf]# vi /etc/dhcp/dhcpd.conf
..........
70 # Fixed IP addresses can also be specified for hosts. These addresses
71 # should not also be listed as being available for dynamic assignment.
72 # Hosts for which fixed IP addresses have been specified can boot using
73 # BOOTP or DHCP. Hosts for which no fixed address is specified can only
74 # be booted with DHCP, unless there is an address range on the subnet
75 # to which a BOOTP client is connected which has the dynamic-bootp flag
76 # set.
77 host boss {
78 hardware ethernet 00:0C:29:CD:C1:61;
79 fixed-address 20.0.0.88;
80 }
81
82 # You can declare a class of clients and then do address allocation
83 # based on that. The example below shows a case where all clients
84 # in a certain class get addresses on the 10.17.224/24 subnet, and all
85 # other clients get addresses on the 10.0.29/24 subnet.
86
[root@lpf lpf]# systemctl restart dhcpd