1. 首页 > Linux教程 > 正文

Linux教程FG234-DHCP服务器配置

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

本文

风哥提示:

档详细介绍DHCP服务器的安装、配置和管理方法。

Part01-DHCP安装

1.1 安装DHCP服务

# 安装DHCP服务器
$ sudo dnf install -y dhcp-server
Last metadata expiration check: 0:45:23 ago on Thu 03 Apr 2026 22:40:15 AM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
dhcp-server x86_64 12:4.4.2-19.b1.el9 appstream 1.0 M

Transaction Summary
================================================================================
Install 1 Package

Total download size: 1.0 M
Installed size: 2.5 M
Downloading Packages:
dhcp-server-12:4.4.2-19.b1.el9.x86_64.rpm 1.0 MB/s | 1.0 MB 00:01
——————————————————————————–
Total 1.0 MB/s | 1.0 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : dhcp-server-12:4.4.2-19.b1.el9.x86_64 1/1
Running scriptlet: dhcp-server-12:4.4.2-19.b1.el9.x86_64 1/1
Verifying : dhcp-server-12:4.4.2-19.b1.el9.x86_64 1/1

Installed:
dhcp-server-12:4.4.2-19.b1.el9.x86_64

Complete!

# 启动DHCP服务
$ sudo systemctl start dhcpd

# 设置开机自启动
$ sudo systemctl enable dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.

# 查看服务状态
$ sudo systemctl status dhcpd
● dhcpd.service – DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; preset: disabled)
Active: active (running) since Thu 2026-04-03 22:40:00 CST; 10s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 12358 (dhcpd)
Status: “Dispatching packets…”
Tasks: 1 (limit: 49152)
Memory: 5.2M
CPU: 15ms
CGroup: /system.slice/dhcpd.service
└─12358 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd –no-pid

Apr 03 22:40:00 rhel10 dhcpd[12358]: Internet Systems Consortium DHCP Server 4.4.2
Apr 03 22:40:00 rhel10 dhcpd[12358]: Copyright 2004-2021 Internet Systems Consortium.
Apr 03 22:40:00 rhel10 dhcpd[12358]: All rights reserved.
Apr 03 22:40:00 rhel10 dhcpd[12358]: For info, please visit https://www.isc.org/software/dhcp/
Apr 03 22:40:00 rhel10 dhcpd[12358]: Config file: /etc/dhcp/dhcpd.conf
Apr 03 22:40:00 rhel10 dhcpd[12358]: Database file: /var/lib/dhcpd/dhcpd.leases
Apr 03 22:40:00 rhel10 dhcpd[12358]: PID file: /var/run/dhcpd.pid
Apr 03 22:40:00 rhel10 dhcpd[12358]: Listening on LPF/eth0/08:00:27:12:34:56/192.168.1.0/24
Apr 03 22:40:00 rhel10 dhcpd[12358]: Sending on LPF/eth0/08:00:27:12:34:56/192.168.1.0/24
Apr 03 22:40:00 rhel10 dhcpd[12358]: Sending on Socket/fallback/fallback-net

# 配置防火墙
$ sudo firewall-cmd –permanent –add-service=dhcp
success
$ sudo firewall-cmd –reload
success

Part02-DHCP配置文件

2.1 配置dhcpd.conf

# 备份原配置文件
$ sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak

# 编辑配置文件
$ sudo tee /etc/dhcp/dhcpd.conf << EOF # 全局配置 option domain-name "fgedu.net.cn"; option domain-name-servers ns1.fgedu.net.cn, ns2.fgedu.net.cn; default-lease-time 600; max-lease-time 7200; authoritative; log-facility local7; # 子网配置 subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.100 192.168.1.200; option routers 192.168.1.1; option subnet-mask 255.255.255.0; option broadcast-address 192.168.1.255; option domain-name-servers 192.168.1.100, 8.8.8.8; option domain-name "fgedu.net.cn"; default-lease-time 3600; max-lease-time 86400; } # 固定IP地址 host server1 { hardware ethernet 08:00:27:12:34:56; fixed-address 192.168.1.10; } host server2 { hardware ethernet 08:00:27:ab:cd:ef; fixed-address 192.168.1.11; } EOF # 检查配置语法 $ sudo dhcpd -t -cf /etc/dhcp/dhcpd.conf Internet Systems Consortium DHCP Server 4.4.2 Copyright 2004-2021 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Configuration file errors detected! # 重启服务 $ sudo systemctl restart dhcpd

Part03-多子网配置

3.1 配置多子网DHCP

# 配置多子网
$ sudo tee /etc/dhcp/dhcpd.conf << EOF # 全局配置 option domain-name "fgedu.net.cn"; option domain-name-servers 192.168.1.100, 8.8.8.8; default-lease-time 3600; max-lease-time 86400; authoritative; log-facility local7; # 子网1:192.168.1.0/24 subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.100 192.168.1.200; option routers 192.168.1.1; option subnet-mask 255.255.255.0; option broadcast-address 192.168.1.255; } # 子网2:192.更多学习教程公众号风哥教程itpux_com168.2.0/24 subnet 192.168.2.0 netmask 255.255.255.0 { range 192.168.2.100 192.168.2.200; option routers 192.168.2.1; option subnet-mask 255.255.255.0; option broadcast-address 192.168.2.255; } # 子网3:10.0.0.0/24 subnet 10.0.0.0 netmask 255.255.255.0 { range 10.0.0.100 10.0.0.200; option routers 10.0.0.1; option subnet-mask 255.255.255.0; option broadcast-address 10.0.0.255; } # 使用共享网络 shared-network OFFICE { subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.100 192.168.1.150; option routers 192.168.1.1; } subnet 192.168.2.0 netmask 255.255.255.0 { range 192.168.2.100 192.168.2.150; option routers 192.168.2.1; } } # 使用类区分客户端 class "printers" { match if substring(hardware, 1, 3) = 00:11:22; } class "voip-phones" { match if substring(hardware, 1, 3) = 00:33:44; } subnet 192.168.1.0 netmask 255.255.255.0 { pool { allow members of "printers"; range 192.168.1.50 192.168.1.60; } pool { allow members of "voip-phones"; range 192.168.1.70 192.168.1.80; } pool { deny members of "printers"; deny members of "voip-phones"; range 192.168.1.100 192.168.1.200; } } EOF # 重启服务 $ sudo systemctl restart dhcpd

Part04-DHCP中继配置

4.1 配置DHCP中继

# 安装DHCP中继
$ sudo dnf install -y dhcp-relay

# 配置DHCP中继
$ sudo tee /etc/systemd/system/dhcrelay.service << EOF [Unit] Description=DHCP Relay Agent After=network.target [Service] Type=simple ExecStart=/usr/sbin/dhcrelay -d --no-pid 192.168.1.100 Restart=on-failure [Install] WantedBy=multi-user.target EOF # 启动DHCP中继服务 $ sudo systemctl daemon-reload $ sudo systemctl enable --now dhcrelay # 查看服务状态 $ sudo systemctl status dhcrelay ● dhcrelay.service - DHCP Relay Agent Loaded: loaded (/etc/systemd/system/dhcrelay.service; enabled; preset: disabled) Active: active (running) since Thu 2026-04-03 22:45:00 CST; 10s ago Main PID: 12359 (dhcrelay) Tasks: 1 (limit: 49152) Memory: 1.2M CPU: 10ms CGroup: /system.slice/dhcrelay.service └─12359 /usr/sbin/dhcrelay -d --no-pid 192.168.1.100 Apr 03 22:45:00 rhel10 systemd[1]: Started DHCP Relay Agent.

Part05-DHCP管理

5.1 查看租约信息

# 查看租约文件
$ cat /var/lib/dhcpd/dhcpd.leases
lease 192.168.1.100 {
starts 4 2026/04/03 22:45:00;
ends 4 2026/04/03 23:45:00;
cltt 4 2026/04/03 22:45:00;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 08:00:27:ab:cd:ef;
client-hostname “client1”;
}

lease 192.168.1.101 {
starts 4 2026/04/03 22:46:00;
ends 4 2026/04/03 23:46:00;
cltt 4 2026/04/03 22:46:00;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 08:00:27:12:34:56;
client-hostname “client2”;
}

# 查看DHCP日志
$ sudo tail -f /var/log/messages | grep dhcp
Apr 3 22:45:00 rhel10 dhcpd[12358]: DHCPDISCOVER from 08:00:27:ab:cd:ef via eth0
Apr 3 22:45:00 rhel10 dhcpd[12358]: D学习交流加群风哥微信: itpux-comHCPOFFER on 192.168.1.100 to 08:00:27:ab:cd:ef via eth0
Apr 3 22:45:00 rhel10 dhcpd[12358]: DHCPREQUEST for 192.168.1.100 (192.168.1.10) from 08:00:27:ab:cd:ef via eth0
Apr 3 22:45:00 rhel10 dhcpd[12358]: DHCPACK on 192.168.1.100 to 08:00:27:ab:cd:ef via eth0

# 测试DHCP客户端
$ sudo dhclient -d eth0
Internet Systems Consortium DHCP Client 4.4.2
Copyright 2004-2021 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/08:00:27:12:34:56
Sending on LPF/eth0/08:00:27:12:34:56
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6
DHCPOFFER of 192.168.1.100 from 192.168.1.10
DHCPREQUEST of 192.168.1.100 on eth0 to 255.255.255.255 port 67
DHCPACK of 192.168.1.100 from 192.168.1.10
bound to 192.168.1.100 — renewal in 1500 seconds.

# 释放IP地址
$ sudo dhclient -r eth0

# 重新获取IP地址
$ sudo dhclient eth0

风哥针对from PG视频:www.itpux.com 配置建议:
1. 合理规划IP地址池范围
2. 为重要设备配置固定IP
3. 配置适当的学习交流加群风哥QQ113257174租约时间
4. 定期检查租约文件
5. 监控DHCP服务状态

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息