如何在 RHEL 9 上配置 DHCP 服务器
DHCP
![](/data/attachment/album/202305/24/113729hi6cn36hicjgknjy.jpg)


DHCP 是 “<ruby> 动态主机配置协议 <rt>  Dynamic Host Configuration Protocol </rt></ruby>” 的首字母缩写词，它是一种网络协议，可自动为计算机网络中的客户端系统分配 IP 地址。它从 DHCP 池或在其配置中指定的 IP 地址范围分配客户端。虽然你可以手动为客户端系统分配静态 IP，但 DHCP 服务器简化了这一过程，并为网络上的客户端系统动态分配 IP 地址。


在本文中，我们将演示如何在 RHEL 9 / Rocky Linux 9 上安装和配置 DHCP 服务器。


### 先决条件


* 预装 RHEL 9 或 Rocky Linux 9
* 具有 sudo 管理权限的普通用户
* 本地配置的 YUM/DNF 仓库或 RHEL 9 的 Red Hat 订阅
* 互联网连接


事不宜迟，让我们进入 DHCP 服务器安装步骤。


### 1、在 RHEL 9 / Rocky Linux 9 上配置静态 IP 地址


开始时，必须在 RHEL 或 Rocky Linux 系统上设置静态 IP 地址。有多种方法可以执行此操作，但最简单和最直观的方法是使用 `nmtui` 或 `nmcli` 实用程序。


要确认你的 Linux 系统的 IP 地址，请运行以下 `ip` 命令：



```
$ ip a

```

![](/data/attachment/album/202305/24/113959vpdmdmoziqlzuceq.jpg)


### 2、安装和配置 DHCP 服务器


配置静态 IP 后，下一步就是安装 DHCP 服务器。RHEL 9 或 Rocky Linux 9 仓库 (BaseOS) 默认提供 `dhcp-server` 包，你可以如图所示安装它：



```
$ sudo dnf install dhcp-server -y

```

![](/data/attachment/album/202305/24/114007suu79zudswdwruup.jpg)


安装 dhcp 服务器后，我们需要进一步并配置设置。因此，打开 DHCP 配置文件：



```
$ sudo vi /etc/dhcp/dhcpd.conf

```

将以下代码行粘贴到配置文件中。请务必将子网配置为你的首选值：



```
default-lease-time 3600;
max-lease-time 86400;
authoritative;
subnet 192.168.10.0 netmask 255.255.255.0 {
    range 192.168.10.100 192.168.10.200;
    option routers 192.168.10.1;
    option subnet-mask 255.255.255.0;
    option domain-name-servers 192.168.10.1;
}

```

保存并关闭文件。


![](/data/attachment/album/202305/24/114014hbylyyuyhocyutfz.jpg)


让我们看一下其中的一些值：


* `default-lease-time` 值指定 DHCP 服务器将地址租给客户端的时间。在这种情况下，默认租用时间值为 3600 秒或 1 小时。
* `max-lease-time` 是将 IP 租给客户端的最长持续时间。在我们的例子中，这被设置为 86400 秒或 24 小时。
* 下一部分是子网配置。在此设置中，`192.168.10.0` 是子网，`255.255.255.0` 是子网掩码。IP 地址范围从 `192.168.10.100` 一直到 `192.168.10.200`。
* `router` 选项定义默认网关。在本例中为 `192.168.10.1`。
* `subnet-mask` 选项确定分配给每个客户端或主机的子网掩码。在本例中为 `255.255.255.0`。
* 最后，`domain-name-servers` 选项指定 DNS 服务器。在本例中为 `192.168.10.1`。


完成后，保存更改并退出。然后启用并启动 DHCP 服务。



```
$ sudo systemctl enable --now dhcpd
$ sudo systemctl status dhcpd

```

![](/data/attachment/album/202305/24/114121btodpgs4zi2b5dfs.jpg)


请注意：


此时，DHCP 服务应该分发 IP 地址。如果你的 LAN 中有另一个 DHCP 服务器或路由器，关闭它以防止 IP 地址分配冲突是明智的。这将导致一些客户端从 RHEL 或 Rocky Linux 服务器上的 DHCP 服务器获得 IP 分配，而其余的则从路由器获得 IP 分配，这当然不是你想要发生的事情。因此，请记住关闭 LAN 设置中的任何其他 DHCP 服务器。


### 3、测试 DHCP 服务器安装


在我们模拟的 LAN 设置中，你可以看到 Ubuntu 系统已经从 RHEL 或 Rocky Linux DHCP 服务器中选择了一个 IP。


![](/data/attachment/album/202305/24/114129uxry9pverzrpwwrr.jpg)


回到我们的 DHCP 服务器并在 `/var/log/message` 文件中搜索 Ubuntu 机器的 IP 地址：



```
$ sudo tail -50 /var/log/messages | grep -i 192.168.10.100

```

![](/data/attachment/album/202305/24/114136v4ra2a3m24gm424g.jpg)


完美，上面的输出确认 Ubuntu 机器从我们的 DHCP 服务器获得了 IP 服务器。


### 结论


这篇文章到此结束。在本指南中，你学习了如何在 RHEL 9 / Rocky Linux 9 上安装和配置 DHCP 服务器。请在下面的评论部分发表你的疑问和反馈。


（题图：MJ/d396485d-963c-4d17-8c4b-f3c8e11dcc5d）




---


via: <https://www.linuxtechi.com/configure-dhcp-server-on-rhel-rockylinux/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
DHCP, an acronym for Dynamic Host Configuration Protocol, is a network protocol that auto-assigns IP addresses to client systems in a computer network. It assigns clients from a DHCP pool or range of IP addresses specified in its configuration. While you can manually assign a static IP to client systems, a DHCP server simplifies this and dynamically assigns IP addresses to the client systems on your network.

In this post, we will demonstrate how to install and configure DHCP Server on RHEL 9 / Rocky Linux 9

#### Prerequisites

- Pre-Installed RHEL 9 or Rocky Linux 9
- Regular User with sudo sdmin rights
- Locally Configured YUM/DNF Repo or Red Hat Subscription for RHEL 9
- Internet Connectivity

Without any further delay, let’s jump into DHCP server installation steps.

## 1) Configure Static IP address on RHEL 9 / Rocky Linux 9

As you get started, it’s imperative to set a static IP address on your RHEL or Rocky Linux system. There are various ways of doing this, but the easiest and most intuitive one is using the nmtui or nmcli utility.

Read Also : [How to Set Static IP Address on RHEL 9](https://www.linuxtechi.com/set-static-ip-address-on-rhel-9/)

To confirm the IP address of your linux system, run below [ip command](https://www.linuxtechi.com/ip-command-examples-for-linux-users/)

$ ip a

## 2) Install and Configure DHCP Server

Once you have configured a static IP, the next course of action is to install a DHCP server. RHEL 9 or Rocky Linux 9 repositories (BaseOS) provide the dhcp-server package by default and you can install it as shown.

$ sudo dnf install dhcp-server -y

Post dhcp server installation, we need to go a step further and configure the settings. So, open the DHCP configuration file.

$ sudo vi /etc/dhcp/dhcpd.conf

Paste the following lines of code into the configuration file. Be sure to configure the subnet to your preferred values.

default-lease-time 3600; max-lease-time 86400; authoritative; subnet 192.168.10.0 netmask 255.255.255.0 { range 192.168.10.100 192.168.10.200; option routers 192.168.10.1; option subnet-mask 255.255.255.0; option domain-name-servers 192.168.10.1; }

Save & close the file.

Let’s take a look at some of these values:

- The default-lease-time value specifies how long the DHCP server will lease an address to a client. In this case, the default-lease-time value is 3600 seconds or 1 hour. The max-lease-time is the maximum duration that the IP will be leased to a client. In our case, this is set to 86400 seconds or 24 hours.
- The next section is the subnet configuration. In this setup, 192.168.10.0, is the subnet and 255.255.255.0 is the subnet mask. The IP address range starts from 192.168.10.100 right through 192.168.10.200.
- The option router option defines the default gateway. In this case, 192.168.10.1.
- The option subnet-mask option determines the subnet-mask assignment to each client or host. In this case, 255.255.255.0.
- Lastly, domain-name-servers specifies the DNS servers. In this case 192.168.10.1.

Once done, save the changes and exit. Then enable and start the DHCP service.

$ sudo systemctl enable --now dhcpd $ sudo systemctl status dhcpd

#### PLEASE NOTE

At this point, the DHCP service should be dishing out IP addresses. In case you have another DHCP server or router in your LAN, it’s prudent to turn it off to prevent a conflict of IP address assignment. This would lead to some clients getting the IP assignment from the RHEL or Rocky Linux server and the rest from the router which is certainly not what you want to happen. So, remember to turn off any other DHCP server in your LAN setup.

## 3) Test DHCP Server installation

In our simulated LAN setup, you can see that Ubuntu system has already picked an IP from the RHEL or Rocky Linux DHCP server.

Head back to our dhcp server and search the Ubuntu machine ip address in /var/log/message file

$ sudo tail -50 /var/log/messages | grep -i 192.168.10.100

Perfect, output above confirms that Ubuntu machine got the IP server from our DHCP server.

#### Conclusion

This draws this post to a close. In this guide, you have learned how to install and configure the DHCP server on RHEL 9 / Rocky Linux 9. Kindly do post your queries and feedback in below comments section.