目录
一、DHCP概述
DHCP(Dynamic Host Configuration Protocol) 是一种应用层网络协议,用于动态分配IP地址和其他网络配置信息给网络中的设备(如计算机、路由器、打印机等)。DHCP是一个客户端-服务器协议,其中DHCP 服务器 会为连接到网络中的DHCP 客户端分配 IP 地址、子网掩码、默认网关、DNS 服务器等信息,从而简化了设备配置。
DHCP工作原理
- 客户端发送DHCPDISCOVER广播消息。
- DHCP 服务器收到消息后,发送DHCPOFFER响应,提供一个可用的 IP 地址。
- 客户端选择其中一个DHCPOFFER,并发送DHCPQUEST请求使用该 IP 地址。
- 服务器确认请求,发送DHCPACK,正式分配IP地址。
- 客户端配置网络并开始使用分配的IP地址。
二、DHCP部署实操
2.1、安装DHCP软件
cpp
[root@localhost ~]# systemctl stop firewalld.service //关闭防火墙
[root@localhost ~]# setenforce 0 //关闭selinux
[root@localhost ~]# yum install dhcp -y //安装软件
[root@localhost ~]# 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
2.2、拷贝配置文件
cpp
[root@localhost dhcp]# ls /etc/dhcp/
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
[root@localhost dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example //dhcp配置文件范本,直接拷贝并修改
# see dhcpd.conf(5) man page
#
[root@localhost dhcp]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcp.conf
[root@localhost dhcp]# vim /etc/dhcp/dhcp.conf
2.3、配置文件详解
cpp
6 # option definitions common to all supported networks...
7 option domain-name "example.org"; //dns服务器域名
8 option domain-name-servers ns1.example.org, ns2.example.org; //dns服务器地址
9
10 default-lease-time 600;
11 max-lease-time 7200;
12
20 # Use this to send dhcp log messages to a different log file (you also
21 # have to hack syslog.conf to complete the redirection).
22 log-facility local7; //日志设施类型
23
46 # A slightly different configuration for an internal subnet.
47 subnet 10.5.5.0 netmask 255.255.255.224 { //分配的网段,拷贝整段进行重新配置
48 range 10.5.5.26 10.5.5.30; //分配的地址范围
49 option domain-name-servers ns1.internal.example.org;
50 option domain-name "internal.example.org";
51 option routers 10.5.5.1; //指定网关地址
52 option broadcast-address 10.5.5.31;
53 default-lease-time 600;
54 max-lease-time 7200;
55 }
75 host fantasia { //主机分配固定IP地址
76 hardware ethernet 08:00:07:26:c0:a5; //绑定MAC地址
77 fixed-address fantasia.fugue.com; //保留的IP地址
78 }
cpp
//在空行处粘贴范文
27 subnet 192.168.153.0 netmask 255.255.255.0 { //DHCP服务器网段与子网掩码
28 range 192.168.153.20 192.168.153.100; //分配的地址范围
30 option routers 192.168.153.2; //网关地址
31 default-lease-time 600; //租约时间
32 }
2.4、重启软件服务
cpp
[root@localhost dhcp]# systemctl restart dhcpd
● dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2025-02-26 22:28:19 CST; 4min 13s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 2671 (dhcpd)
Status: "Dispatching packets..."
Tasks: 1
2.5、新开一台服务器,查看dhcp地址获取
