DHCP服务器是一种网络协议,它会自动为设备分配IP地址和其他网络配置,它通过在设备和DHCP服务器之间发送和接收DHCP消息来工作,DHCP服务器维护IP地址池并将其分配给设备,DHCP服务器可以配置为根据设备MAC地址分配IP地址, 它还可以使用 DHCP 选项向设备提供其他信息。
在 Rocky Linux 9 上配置 DHCP 服务器
第 1 步。第一步是将系统更新到最新版本的软件包列表。为此,请运行以下命令:
<span class="pln">sudo dnf check</span><span class="pun">-</span><span class="pln">update sudo dnf install dnf</span><span class="pun">-</span><span class="pln">utils</span>
第 2 步。在 Rocky Linux 9 上安装 DHCP 服务器。
在终端中运行以下命令以安装 DHCP 服务器包:
<span class="pln">sudo dnf install dhcp</span><span class="pun">-</span><span class="pln">server</span>
第 3 步。在 Rocky Linux 9 上配置 DHCP 服务器。
安装 DHCP 服务器包后,我们需要对其进行配置。我们将编辑位于以下位置的DHCP服务器配置文件:/etc/dhcp/dhcpd.conf
<span class="pln">nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">dhcp</span><span class="pun">/</span><span class="pln">dhcpd</span><span class="pun">.</span><span class="pln">conf</span>
您可以配置 DHCP 服务器设置,例如 DHCP 服务器应分配的 IP 地址范围、默认网关和 DNS 服务器。下面是一个示例配置:
<span class="pln">subnet </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.0</span><span class="pln"> netmask </span><span class="lit">255.255</span><span class="pun">.</span><span class="lit">255.0</span> <span class="pun">{</span><span class="pln"> range </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.100</span> <span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.200</span><span class="pun">;</span><span class="pln"> option routers </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">;</span><span class="pln"> option domain</span><span class="pun">-</span><span class="pln">name</span><span class="pun">-</span><span class="pln">servers </span><span class="lit">8.8</span><span class="pun">.</span><span class="lit">8.8</span><span class="pun">,</span> <span class="lit">8.8</span><span class="pun">.</span><span class="lit">4.4</span><span class="pun">;</span> <span class="pun">}</span>
保存并关闭文件,然后使用以下命令启动 DHCP 服务:
<span class="pln">sudo systemctl start dhcpd sudo systemctl enable dhcpd</span>
此配置适用于 IP 地址在 到 的范围内且网络掩码为 的子网。DHCP 服务器将分配 IP 地址,范围为 ,默认网关将设置为 192.168.0.1,DNS 服务器将设置为 和192.168.0.0
192.168.0.255
255.255.255.0
192.168.0.100
192.168.0.200
8.8.8.8
8.8.4.4
第 4 步。配置防火墙。
现在我们将防火墙配置为打开 UDP 端口 67,启动服务并使客户端连接到我们的服务器:
<span class="pln">sudo firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="kwd">add</span><span class="pun">-</span><span class="pln">port</span><span class="pun">=</span><span class="lit">67</span><span class="pun">/</span><span class="pln">udp </span><span class="pun">--</span><span class="pln">permanent sudo firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>
第5步。配置 DHCP 保留。
要配置 DHCP 保留,可以将主机部分添加到 DHCP 配置文件,例如:
<span class="pln">host client1 </span><span class="pun">{</span><span class="pln"> hardware ethernet </span><span class="lit">00</span><span class="pun">:</span><span class="lit">36</span><span class="pun">:</span><span class="lit">46</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">44</span><span class="pun">:</span><span class="lit">55</span><span class="pun">;</span> <span class="kwd">fixed</span><span class="pun">-</span><span class="pln">address </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.50</span><span class="pun">;</span> <span class="pun">}</span>
这会将 IP 地址 192.168.0.50 分配给具有 MAC 地址的设备00:36:46:10:44:55
第 6 步。测试 DHCP 服务器。
要测试 DHCP 服务器,您可以将设备连接到网络并检查它是否从 DHCP 服务器接收 IP 地址。
感谢您使用本教程在 Rocky Linux 9 系统上配置 DHCP 服务器。如需其他帮助或有用信息,我们建议您查看 Rocky Linux 官方网站。