安装

1
sudo apt install isc-dhcp-server -y

配置dhcp服务

修改dhcpd.conf

1
vim /etc/dhcp/dhcpd.conf

文件中增加以下内容

1
2
3
4
5
6
7
8
9
10
11
# A slightly different configuration for an internal subnet.
subnet 192.168.8.0 netmask 255.255.255.0 {
range 192.168.8.100 192.168.8.200; # DHCP地址池
option domain-name "vm-pxe";
option routers 192.168.8.88; # 路由地址
option broadcast-address 192.168.8.255; # 广播地址
default-lease-time 6000;
max-lease-time 72000;
next-server 192.168.8.88;
filename "pxelinux.0";
}

配置dhcp使用的网卡

编辑配置文件添加用于监听客户端DHCP请求信息的网卡,使用ip a命令查看网卡信息,我这里选用第二块网卡ens33,配置DHCP的网卡地址需要设置成固定IP。

1
2
3
4
5
6
7
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether 00:0c:29:3b:6c:de brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 192.168.8.88/24 brd 192.168.8.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::d98c:3e8f:1be2:7172/64 scope link noprefixroute
valid_lft forever preferred_lft forever
1
vim /etc/default/isc-dhcp-server

修改

1
2
3
4
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4=""
INTERFACESv6=""

1
2
3
4
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4="ens33" # IP4DHCP网卡
INTERFACESv6="ens33" # IP6DHCP网卡

重启dhcp服务

1
systemctl restart isc-dhcp-server

查看状态

1
systemctl status isc-dhcp-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
● isc-dhcp-server.service - ISC DHCP IPv4 server
Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-06-01 10:58:21 CST; 4 days ago
Docs: man:dhcpd(8)
Main PID: 107859 (dhcpd)
Tasks: 4 (limit: 4567)
Memory: 4.5M
CGroup: /system.slice/isc-dhcp-server.service
└─107859 dhcpd -user dhcpd -group dhcpd -f -4 -pf /run/dhcp-server/dhcpd.pid -cf /etc/dhcp/dhcpd.conf ens33

6月 05 10:54:52 vm dhcpd[107859]: DHCPOFFER on 192.168.8.106 to 00:0c:29:27:87:42 ((none)) via ens33
6月 05 10:54:53 vm dhcpd[107859]: reuse_lease: lease age 158 (secs) under 25% threshold, reply with unaltered, existing lease for 192.168.8.106
6月 05 10:54:53 vm dhcpd[107859]: DHCPREQUEST for 192.168.8.106 (192.168.8.88) from 00:0c:29:27:87:42 ((none)) via ens33
6月 05 10:54:53 vm dhcpd[107859]: DHCPACK on 192.168.8.106 to 00:0c:29:27:87:42 ((none)) via ens33
6月 05 10:55:36 vm dhcpd[107859]: reuse_lease: lease age 201 (secs) under 25% threshold, reply with unaltered, existing lease for 192.168.8.106
6月 05 10:55:36 vm dhcpd[107859]: DHCPDISCOVER from 00:0c:29:27:87:42 ((none)) via ens33
6月 05 10:55:36 vm dhcpd[107859]: DHCPOFFER on 192.168.8.106 to 00:0c:29:27:87:42 ((none)) via ens33
6月 05 10:55:36 vm dhcpd[107859]: reuse_lease: lease age 201 (secs) under 25% threshold, reply with unaltered, existing lease for 192.168.8.106
6月 05 10:55:36 vm dhcpd[107859]: DHCPREQUEST for 192.168.8.106 (192.168.8.88) from 00:0c:29:27:87:42 ((none)) via ens33
6月 05 10:55:36 vm dhcpd[107859]: DHCPACK on 192.168.8.106 to 00:0c:29:27:87:42 ((none)) via ens33