title: DHCP 服务器部署以及配置
search: 2024-03-21
tags:
- “#DHCP 服务器部署以及配置”
CentOS DHCP 服务器部署指南
背景
:因上了Linux
的实验课程,在课程中,老师要求我们自己搭建DHCP
服务器构建局域网,在构建的时候问题百出,不过也极其有意思
一、补充网络基本概念(了解的可以直接跳过)
IP地址
:通俗来讲,我认为 IP 就是相当于在互联网的身份证,是用来标识自己在互联网上的身份的,别的电脑找到你的方法就是通过 IP 地址来找到你
子网掩码
:我认为子网掩码相当于学校的分班,通过子网掩码,我可以判断你和我是否在同一个网段,相当于判断我和你是否在同一个班级,怎么来进行判断呢?比如说有一个 IP 为210.43.64.111
和 一个 IP210.43.65.189
子网掩码是255.255.0.0
我就知道我得看前 16 位是否相同,(这里面的16位意思是 前两个网段,因为一个网段是 8 位二进制数表示的) 在这里 我能够看到210.43
这个都是相同的,于是默认他们在同一个网段,如果子网掩码是255.255.255.0
我们就看前24位是否相同,显然,210.43.64
和210.43.65
不相同,于是他们便不在同一个网段
网关
:我认为网关相当于是一个中间人,比如说 A 在 1 班(网段),B 在 2 班(网段),C 在 1 班 (网段),A 和 C 讲话,可以直接大声喊(广播),C 就能够听得到 (接受信息),但是 A 想和 B 讲话,直接讲是联系不上的,这时就有一个 D (网关) ,他能够连接 A 和 B,使得他们能够讲话。
DNS
:就是一个把域名转换成为 IP 地址的机器,机器一般都是用 IP 地址来进行访问的,只有人觉得 IP 地址太难记了,就给你一个好记一点的字符串,比如说www.baidu.com
这样你直接给计算机看,计算机是看不懂的,他要去问DNS
服务器,DNS
服务器就在自己的数据库中找,如果没有找到www.baidu.com
这个字段对应的IP
他就会去问 其他的DNS
, 如果都没有,那就告诉你没有www.baidu.com
对应的IP
这样你就找不到百度了
二、DHCP服务器安装与配置
Ubantu
dpkg -l | grep dhcp
systemctl status dhcpd
service dhcpd status
CentOS
1、检查 DHCP
是否已经安装
以下指令是用于查找所有已经安装的软件包,并在结果中筛选出包含 dhcp
的软件包
rpm -qa | grep dhcp
2、安装 DHCP
服务
如果没装的话就装一下
yum install dhepd
3、检验是否安装完毕
安装完毕后可以在路径 /etc/dhcp
下找到以下五个文件,其他有没有都不重要,重要的是一定要有 dhcpd6.conf
和 dhcpd.conf
这两个文件,因为他们需要我们进行 DHCP
服务配置
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
4、查看 dhcpd.conf
配置文件
在路径 /etc/dhcp
下使用指令查看 dhcpd.conf
文件
vi dhcpd.conf
可以看到如下内容
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
5、复制样例配置文件
仔细阅读英语后发现,DHCP
服务配置文件,可以参考在路径 /usr/share/doc/dhcp*/dhcpd.conf.example
这个文件
我们使用指令将样例文件拷贝过来,覆盖在 /etc/dhcp
路径下的 dhcpd.conf
文件
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
6、默认选y
此时,CentOS
提示我们,输入 y
即可
cp:是否覆盖"/etc/dhcp/dhcpd.conf"?
7、dhcpd.conf
配置文件解读
配置 dhcpd.conf
文件,这个是我的样例 dhcpd.conf
,此时不进行配置,直接启动 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 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;}
}
1. DHCP
全局配置
-
option domain-name "example.org";
这行配置指示DHCP
服务器在向客户端分配IP
地址的同时,也将域名“example.org”
作为DHCP
选项中的domain-name
发送出去。这意味着连接到该网络的客户端计算机将自动将其DNS
后缀设置为“example.org”
,从而简化主机名到FQDN
(完全限定域名)的转换过程. -
option domain-name-servers ns1.example.org, ns2.example.org;
这行配置指定了DHCP
客户端应使用的DNS
服务器地址。当客户端请求网络配置时,它们将会收到这两个DNS
服务器的IP
地址,以便进行名称解析。ns1.example.org
和ns2.example.org
是DNS
服务器的主机名,实际使用时这些名字需要能被解析为有效的DNS
服务器IP
地址。 -
default-lease-time 600;
这行设置了DHCP
客户端默认的租约时间(lease time)
。这意味着如果没有特别指定,DHCP
服务器将为每个客户端分配一个IP
地址的有效时间为600秒
(即10分钟)。 -
max-lease-time 7200;
这里定义了DHCP
客户端可以请求的最大租约时间。当客户端请求一个IP
地址时,它可以请求比默认租约时间更长的有效期,但最长不能超过7200
秒(即2小时)。如果客户端请求了一个较长的租约,而该值在有效范围内,DHCP
服务器会尊重这个请求;否则,它将仅提供默认租约时间或小于最大租约时间的有效租期. -
log-facility local7;
这条配置将DHCP
服务器的日志记录级别设置为local7
,这是syslog
的一种日志设施级别。这意味着DHCP
服务器的事件和错误信息将被发送到syslog
服务,并标记为本地设施7
,管理员可以根据syslog
配置将这部分日志定向到特定的文件或其他处理方式。
2. DHCP
子网配置
-
subnet 10.152.187.0 netmask 255.255.255.0 {};
这个子网配置不提供任何DHCP
服务,但它有助于DHCP
服务器理解网络拓扑结构。实际上,在这个配置下,DHCP
服务器不会对属于这个子网的设备分配IP
地址。 -
subnet 10.254.239.0 netmask 255.255.255.224 { ... }
对于这个子网,DHCP
服务器将为请求IP
地址的客户端提供一个范围内的静态IP地址。范围是从10.254.239.10
到10.254.239.20
,并且设置了网关路由器为rtr-239-0-1.example.org
和rtr-239-0-2.example.org
。 -
subnet 10.254.239.32 netmask 255.255.255.224 { ... }
对于这个子网,DHCP
服务器将为BOOTP
客户端提供动态分配的IP
地址,范围是从10.254.239.40
到10.254.239.60
。虽然现代环境中通常推荐使用DHCP
而非BOOTP
,但这里仍然支持了BOOTP
协议。此外,还指定了广播地址为10.254.239.31
,并设置网关路由器为rtr-239-32-1.example.org
。 -
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; } /* 这个配置定义了一个子网 `10.5.5.0`(由`netmask 255.255.255.224` 指定),在这个子网内,`DHCP`服务器将为客户端分配`IP`地址范围 `10.5.5.26` 到 `10.5.5.30`。此外,还为`DHCP`客户端指定了`DNS`服务器 `(ns1.internal.example.org)`、域名 `(internal.example.org)`、默认网关 `(10.5.5.1)` 以及广播地址 `(10.5.5.31)`。最后,设定了默认租约时间为600秒,最大租约时间为7200秒。*/
3. DHCP
特定主机配置
-
host passacaglia {hardware ethernet 0:0:c0:5d:bd:95;filename "vmunix.passacaglia";server-name "toccata.fugue.com"; } /* 此配置为具有`MAC`地址 `0:0:c0:5d:bd:95` 的主机 `passacaglia` 提供了特殊配置。当这个特定主机请求`DHCP`服务时,服务器将返回预设的配置,包括引导文件名 "vmunix.passacaglia" 和服务器名称 "toccata.fugue.com"。如果未指定固定`IP`地址,`DHCP`服务器会从子网范围中动态分配一个地址给这个主机。*/
-
host fantasia {hardware ethernet 08:00:07:26:c0:a5;fixed-address fantasia.fugue.com; } /*对于主机 `fantasia`,其`MAC`地址为 `08:00:07:26:c0:a5`,`DHCP`服务器为其分配固定的`IP`地址 `fantasia.fugue.com`。这意味着无论何时主机`fantasia`启动并向`DHCP`服务器请求`IP`地址,它都将始终获取到预先设定的固定`IP`地址。*/
4. DHCP
类定义
-
class "foo" {match if substring (option vendor-class-identifier, 0, 4) = "SUNW"; }定义了一个名为“`foo`”的`DHCP`客户端类别`(class)`,其中成员资格匹配条件是`DHCP`请求中的`vendor-class-identifier`选项的前四个字符必须等于"SUNW"。`Vendor-class-identifier`是一个客户端可以用来`标识自身类型`的`DHCP`选项,这里可能是某个特定厂商或设备类型(如Sun Microsystems的设备)的标识符。
5. DHCP
共享网络(shared-network
)配置
-
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;} } 在共享网络“`224-29`”中,包含了两个子网:`10.17.224.0/24` 和 `10.0.29.0/24`,分别指定了各自的默认网关(`router`)。接下来定义了两个`IP`地址池(`pool`)。第一个池允许属于类"foo"的客户端从中获取`IP`地址,范围是从 `10.17.224.10` 到 `10.17.224.250`。第二个池则是拒绝属于类"foo"的客户端,也就是说,属于"foo"类别的客户端不能从 `10.0.29.10` 到 `10.0.29.230` 的`IP`地址范围内获取`IP`地址。
8、启动 DHCP
服务
service dhcpd start
9、启动失败情况
失败情况报错如下
[root@hdfs004 dhcp]# service dhcpd start
Redirecting to /bin/systemctl start dhcpd.service
Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details.
阅读英语后可知服务并未成功启动,控制进程以错误代码退出。
为了解决问题,提示提供了两个命令供进一步排查问题详情
systemctl status dhcpd.service
:该命令将显示dhcpd
服务当前的状态以及最后一次启动尝试的详细信息,包括任何错误消息。这有助于识别为何服务无法启动的原因。journalctl -xe
:该命令会显示系统的最新日志消息,其中包括服务启动时可能产生的错误日志。通常,错误信息会出现在这里,帮助您找出导致服务启动失败的具体原因。
遇到的具体问题,请实际百度之后各自解决,所有人遇到的问题并不一致,请找到标红的信息,将其复制上百度搜索即可
10、读取日志
启动成功后,可以在路径
/var/log/message
中读取到DHCP
服务器接受到客户端的分配请求日志,还可以在路径/var/lib/dhcpd/dhcpd.leases
中读取到其他客户端的租借信息。
11、附加指令
比较新版本的 CentOS
支持
- 重启
dhcpd
服务
systemctl restart dhcpd.service
- 检查
dhcpd
服务的状态
systemctl status dhcpd.service
- 关闭
dhcpd
服务
systemctl stop dhcpd.service
- 开启
dhcpd
服务
systemctl start dhcpd.service
老版本的 CentOS
支持
-
关闭
dhcpd
服务:service dhcpd stop
-
开启
dhcpd
服务:service dhcpd start
-
重启
dhcpd
服务:service dhcpd restart
-
检查
dhcpd
服务:service dhcpd status
12、卸载 DHCP
服务
提供两种可行方案
强制卸载
列出已经安装的dhcp
软件包所包含的所有文件列表
rpm -ql dhcp
-
rpm
是Red Hat Package Manager
(红帽软件包管理器)命令。 -
-q
表示查询(query)模式。 -
-l
参数意味着列出(list)与软件包相关的所有文件及其安装路径。
如果你担心删除不干净,可以直接将查询列出来的东西全部删掉,这样就强制卸载掉了 DHCP
服务
正常卸载
Step-1
:停止DHCP
服务,可使用指令systemctl stop dhcpd
或service dhcpd stop
Step-2
:禁用DHCP
服务开机启动,可使用指令systemctl disable dhcpd
或chkconfig dhcpd off
Step-3
:检查是否还有DHCP
服务的相关进程还在执行,如果有,应该kill
进程,可使用指令pkill dhcpd
Step-4
:卸载DHCP
软件包,可尝试使用指令rpm -e dhcp
或yum remove dhcp
Step-5
:可选操作,删除DHCP相关的配置文件和数据(根据需要决定是否保留配置以便将来重新安装时恢复)可尝试使用指令rm -rf /etc/dhcp/*
Step-6
:清理缓存和其他相关数据 可尝试使用指令rm -f /var/lib/dhcp/*
注意细节 pkill dhcpd 指令
:
- 这条指令是在Linux系统中使用的,用于向名为
dhcpd
的进程发送一个信号,默认情况下通常是发送SIGTERM
信号,目的是请求该进程终止运行。- 当你运行
pkill dhcpd
时,系统将会寻找所有 名称 中含有dhcpd
的进程,并尝试立即结束它们。- 需要注意的是,
pkill
命令是根据进程名来杀死进程的,所以它会杀死所有符合条件的进程实例。- 如果只想终止特定的
dhcpd
进程而不影响其他可能 同样命名 的进程,应首先确定要终止的进程PID
,并使用kill
命令替代,例如kill <PID>
。- 另外,
pkill
命令可以接受不同的信号作为参数,如果想要强制立即结束进程(不等待其自行清理资源),可以加上-9
参数,例如pkill -9 dhcpd
,这时会发送SIGKILL
信号,该信号不可被捕获或忽略,进程会被立即终结。但这种方式较为粗暴,一般情况下不建议除非必要情况。