日期:2024.11.14
內容:在虛擬的LAN網路中(10.31.0.0/24)的ROCKY9.4(10.31.0.1/24)上搭建DHCP伺服器,供LAN網路中其餘主機自動獲取IP地址
參照:
- 鳥哥Linux私房菜
- 馬哥教育王曉春老師課程
安裝DHCP服務
[root@ROCKY9 ~]# dnf info dhcp-server
Last metadata expiration check: 3:32:54 ago on Wed 13 Nov 2024 11:19:52 PM CST.
Available Packages
Name : dhcp-server
Epoch : 12
Version : 4.4.2
Release : 19.b1.el9
Architecture : x86_64
Size : 1.2 M
Source : dhcp-4.4.2-19.b1.el9.src.rpm
Repository : baseos
Summary : Provides the ISC DHCP server
URL : https://www.isc.org/dhcp/
License : ISC
Description : DHCP (Dynamic Host Configuration Protocol) is a protocol which allows
: individual devices on an IP network to get their own network
: configuration information (IP address, subnetmask, broadcast address,
: etc.) from a DHCP server. The overall purpose of DHCP is to make it
: easier to administer a large network.
:
: This package provides the ISC DHCP server.
[root@ROCKY9 ~]# rpm -q dhcp-server || yum install -y dhcp-server
啥都別問先備份
[root@ROCKY9 ~]# rpm -qc dhcp-server
/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
[root@ROCKY9 ~]# cp /etc/dhcp/dhcpd.conf{,.bak}
[root@ROCKY9 ~]# ll /etc/dhcp/dhcpd.conf*
-rw-r--r--. 1 root root 123 Oct 26 2023 /etc/dhcp/dhcpd.conf
-rw-r--r--. 1 root root 123 Nov 16 00:58 /etc/dhcp/dhcpd.conf.bak
給了一個example例子和一個manual幫助
[root@ROCKY9 ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
example中能用到的
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org
【寫在開頭的是】支援所有網段的通用定義選項
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
用這個指令傳送dhcp日誌資訊到一個不同的日誌檔案(應該是指不和系統檔案放一起)
【想生效的話】還需要修改syslog.cof檔案來完成重新指定
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
subnet 10.152.187.0 netmask 255.255.255.0 {
}
不對這個網段提供服務,只是宣告,幫助dhcp伺服器瞭解網路的拓撲結構。
使用local6記錄日誌
預設租約60000秒
最大租約600000
dns伺服器218.2.2.2,218.4.4.4
配置向lan網段(10.31.0.0)提供DHCP服務
範圍10.31.0.100-10.31.0.199
閘道器 10.31.0.254
[root@ROCKY9 ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
log-facility local6;
default-lease-time 60000;
max-lease-time 600000;
option domain-name-servers 218.2.2.2, 218.4.4.4;
subnet 10.31.0.0 netmask 255.255.255.0 {
range 10.31.0.100 10.31.0.199;
option routers 10.31.0.254;
}
故意漏寫最後半個花括號},語法檢測能查出來,語法檢測透過再起服務。
[root@ROCKY9 ~]# man dhcpd
-t Test the configuration file. The server tests the configuration file for correct syntax, but
will not attempt to perform any network operations. This can be used to test a new configura‐
tion file automatically before installing it.
-cf config-file
Path to alternate configuration file.
[root@ROCKY9 ~]# dhcpd -t -cf /etc/dhcp/dhcpd.conf
Internet Systems Consortium DHCP Server 4.4.2b1
Copyright 2004-2019 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
/etc/dhcp/dhcpd.conf line 13: unexpected end of file
^
Configuration file errors encountered -- exiting
This version of ISC DHCP is based on the release available
on ftp.isc.org. Features have been added and other changes
have been made to the base software release in order to make
it work better with this distribution.
Please report issues with this software via:
https://bugs.rockylinux.org/
exiting.
定義local6日誌檔案存放路徑,重啟rsyslog.service讀取配置檔案。
這裡先嚐試systemctl reload rsyslog.service時沒有生效,個人猜測如果寫配置檔案裡,roload可能會生效,在.d目錄裡新建配置檔案,需要restart
[root@ROCKY9 ~]# echo 'local6.* /var/log/dhcp.log' > /etc/rsyslog.d/dhcp.conf
[root@ROCKY9 ~]# systemctl restart rsyslog.service
起服務,看日誌
[root@ROCKY9 ~]# systemctl enable --now dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
[root@ROCKY9 ~]# cat /var/log/dhcp.log
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Internet Systems Consortium DHCP Server 4.4.2b1
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Copyright 2004-2019 Internet Systems Consortium.
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: All rights reserved.
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: For info, please visit https://www.isc.org/software/dhcp/
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Source compiled to use binary-leases
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Wrote 0 leases to leases file.
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Listening on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Sending on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Sending on Socket/fallback/fallback-net
Nov 16 02:14:47 ROCKY9 dhcpd[4368]: Server starting service.
在10.31.0.0/24網段建立一個虛擬機器,安裝ubuntu16用於測試
用於建立ubuntu16虛擬機器的指令碼
[root@RHEL9 ~]# cat ubuntu16.sh
#guestcreate.sh
#Date: 2024-11-08
#!/bin/bash
bootdev=cdrom
isopath='/data/ISO/ubuntu-16.04.7-server-amd64.iso'
guestname=ubuntu16
guestmem=1048576
guestcups=1
hddir=/kvm/
hdsize=10G
hdpath=${hddir}${guestname}.img
vncport=5902
vncpasswd=hatred
wanmac=''
dmzmac=''
lanmac='52:54:00:10:31:A0'
xmldir=/data/xml/
xmlpath=${xmldir}${guestname}.xml
qemu-img create -f qcow2 ${hdpath} ${hdsize}
cat > ${xmlpath} << EOF
<domain type="kvm">
<name>${guestname}</name>
<uuid>$(uuidgen)</uuid>
<memory>${guestmem}</memory>
<vcpu>${guestcups}</vcpu>
<os>
<type arch="x86_64" machine="q35">hvm</type>
<boot dev="hd"/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode="host-passthrough"/>
<clock offset="utc">
<timer name="rtc" tickpolicy="catchup"/>
<timer name="pit" tickpolicy="delay"/>
<timer name="hpet" present="no"/>
</clock>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2" discard="unmap"/>
<source file="${hdpath}"/>
<target dev="vda" bus="virtio"/>
</disk>
<controller type="usb" model="qemu-xhci" ports="15"/>
<controller type="pci" model="pcie-root"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<console type="pty"/>
<channel type="unix">
<source mode="bind"/>
<target type="virtio" name="org.qemu.guest_agent.0"/>
</channel>
<input type="tablet" bus="usb"/>
<graphics type="vnc" port="${vncport}" listen="0.0.0.0" passwd="${vncpasswd}"/>
<video>
<model type="virtio"/>
</video>
<memballoon model="virtio"/>
<rng model="virtio">
<backend model="random">/dev/urandom</backend>
</rng>
</devices>
</domain>
EOF
[[ -n ${bootdev} ]] && sed -Ei.bak '/ <boot dev="hd"\/>/a \ <boot dev="'''${bootdev}'''"\/>' ${xmlpath}
[[ -n ${isopath} ]] && sed -Ei '/ <controller type="usb" model="qemu-xhci" ports="15"\/>/i \ <disk type="file" device="cdrom">\n <driver name="qemu" type="raw"\/>\n <source file="'''${isopath}'''"\/>\n <target dev="sda" bus="sata"\/>\n <readonly\/>\n <\/disk>' ${xmlpath}
[[ -n ${wanmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="WANbridge"/>\n <mac address="'''${wanmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
[[ -n ${dmzmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="DMZbridge"/>\n <mac address="'''${dmzmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
[[ -n ${lanmac} ]] && sed -Ei '/ <console type="pty"\/>/i \ <interface type="bridge">\n <source bridge="LANbridge"/>\n <mac address="'''${lanmac}'''"\/>\n <model type="virtio"\/>\n <\/interface>' ${xmlpath}
virsh create ${xmlpath}
在rocky9上監聽udp67埠,可以看到在Ubuntu16安裝過程中透過dhcp服務獲取網路引數的經過,一共抓到了5個包,分別是:
1.ubuntu16客戶端Discover尋求dhcp服務,傳送了兩次共計兩個包
2.rocky9伺服器端Offer提供地址池中的IP(10.31.0.100)
3.ubuntu16客戶端Request需求了地址池中的10.31.0.100這個IP
4.rocky9伺服器端ACK回應將IP:10.31.0.100分配給ubuntu16客戶端
[root@ROCKY9 ~]# tcpdump udp port 67 -v
dropped privs to tcpdump
tcpdump: listening on enp1s0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
01:18:45.339182 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from 52:54:00:10:31:a0 (oui Unknown), length 300, xid 0x374f0b5b, Flags [none]
Client-Ethernet-Address 52:54:00:10:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Discover
Parameter-Request (55), length 8:
Subnet-Mask (1), BR (28), Time-Zone (2), Default-Gateway (3)
Domain-Name (15), Domain-Name-Server (6), Hostname (12), NTP (42)
Vendor-Class (60), length 3: "d-i"
01:18:46.340306 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from 52:54:00:10:31:a0 (oui Unknown), length 300, xid 0x374f0b5b, secs 1, Flags [none]
Client-Ethernet-Address 52:54:00:10:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Discover
Parameter-Request (55), length 8:
Subnet-Mask (1), BR (28), Time-Zone (2), Default-Gateway (3)
Domain-Name (15), Domain-Name-Server (6), Hostname (12), NTP (42)
Vendor-Class (60), length 3: "d-i"
01:18:46.340429 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
ROCKY9.bootps > 10.31.0.100.bootpc: BOOTP/DHCP, Reply, length 300, xid 0x374f0b5b, Flags [none]
Your-IP 10.31.0.100
Client-Ethernet-Address 52:54:00:10:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Offer
Server-ID (54), length 4: ROCKY9
Lease-Time (51), length 4: 60000
Subnet-Mask (1), length 4: 255.255.255.0
Default-Gateway (3), length 4: _gateway
Domain-Name-Server (6), length 8: dns1.ctcdma.com,dns2.ctcdma.com
01:18:46.340655 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from 52:54:00:10:31:a0 (oui Unknown), length 300, xid 0x374f0b5b, secs 1, Flags [none]
Client-Ethernet-Address 52:54:00:10:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Request
Server-ID (54), length 4: ROCKY9
Requested-IP (50), length 4: 10.31.0.100
Parameter-Request (55), length 8:
Subnet-Mask (1), BR (28), Time-Zone (2), Default-Gateway (3)
Domain-Name (15), Domain-Name-Server (6), Hostname (12), NTP (42)
Vendor-Class (60), length 3: "d-i"
01:18:46.359480 IP (tos 0x10, ttl 128, id 0, offset 0, flags [none], proto UDP (17), length 328)
ROCKY9.bootps > 10.31.0.100.bootpc: BOOTP/DHCP, Reply, length 300, xid 0x374f0b5b, secs 1, Flags [none]
Your-IP 10.31.0.100
Client-Ethernet-Address 52:54:00:10:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: ACK
Server-ID (54), length 4: ROCKY9
Lease-Time (51), length 4: 60000
Subnet-Mask (1), length 4: 255.255.255.0
Default-Gateway (3), length 4: _gateway
Domain-Name-Server (6), length 8: dns1.ctcdma.com,dns2.ctcdma.com
日誌記錄和監聽結果相同
[root@ROCKY9 ~]# tail /var/log/dhcp.log -f
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Internet Systems Consortium DHCP Server 4.4.2b1
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Copyright 2004-2019 Internet Systems Consortium.
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: All rights reserved.
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: For info, please visit https://www.isc.org/software/dhcp/
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Source compiled to use binary-leases
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Wrote 0 leases to leases file.
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Listening on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Sending on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Sending on Socket/fallback/fallback-net
Nov 18 01:17:05 ROCKY9 dhcpd[2858]: Server starting service.
Nov 18 01:18:45 ROCKY9 dhcpd[2858]: DHCPDISCOVER from 52:54:00:10:31:a0 via enp1s0
Nov 18 01:18:46 ROCKY9 dhcpd[2858]: DHCPOFFER on 10.31.0.100 to 52:54:00:10:31:a0 via enp1s0
Nov 18 01:18:46 ROCKY9 dhcpd[2858]: DHCPREQUEST for 10.31.0.100 (10.31.0.1) from 52:54:00:10:31:a0 via enp1s0
Nov 18 01:18:46 ROCKY9 dhcpd[2858]: DHCPACK on 10.31.0.100 to 52:54:00:10:31:a0 via enp1s0
給宿主機rhel9(192.168..5.253)配置路由表可以直接訪問10.31.0.0/24和172.31.0.0/24這兩個網段,下一跳的地址為rhel8(192.168.5.254),先關閉連到WANbridge裡的rhel8虛擬機器,再up WANbridge
[root@RHEL9 ~]# ip route
default via 192.168.4.1 dev WANbridge proto static metric 428
192.168.4.0/23 dev WANbridge proto kernel scope link src 192.168.5.253 metric 428
[root@RHEL9 ~]# nmcli connection modify WANbridge +ipv4.routes "10.31.0.0/24 192.168.5.254"
[root@RHEL9 ~]# nmcli connection modify WANbridge +ipv4.routes "172.31.0.0/24 192.168.5.254"
[root@RHEL9 ~]# virsh list
Id Name State
--------------------------
2 ubuntu16 running
3 rocky9 running
4 rhel8 running
[root@RHEL9 ~]# virsh shutdown rhel8
Domain 'rhel8' is being shutdown
[root@RHEL9 ~]# nmcli connection up WANbridge
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/18)
[root@RHEL9 ~]# ip route
default via 192.168.4.1 dev WANbridge proto static metric 428
10.31.0.0/24 via 192.168.5.254 dev WANbridge proto static metric 428
172.31.0.0/24 via 192.168.5.254 dev WANbridge proto static metric 428
192.168.4.0/23 dev WANbridge proto kernel scope link src 192.168.5.253 metric 428
啟動rhel8,在rhel9上發起ssh連線Ubuntu16自動獲取到的IP地址,10.31.0.100
[root@RHEL9 ~]# virsh start rhel8
Domain 'rhel8' started
[root@RHEL9 ~]# ssh nova@10.31.0.100
nova@10.31.0.100's password:
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-186-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
* Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
just raised the bar for easy, resilient and secure K8s cluster deployment.
https://ubuntu.com/engage/secure-kubernetes-at-the-edge
104 packages can be updated.
71 updates are security updates.
New release '18.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Mon Nov 18 02:41:50 2024 from 192.168.5.253
nova@ubuntu16:~$
檢視Ubuntu16上的dhcp租約並釋放
nova@ubuntu16:~$ cat /var/lib/dhcp/dhclient.enp1s0.leases
lease {
interface "enp1s0";
fixed-address 10.31.0.100;
option subnet-mask 255.255.255.0;
option routers 10.31.0.254;
option dhcp-lease-time 60000;
option dhcp-message-type 5;
option domain-name-servers 218.2.2.2,218.4.4.4;
option dhcp-server-identifier 10.31.0.1;
renew 1 2024/11/18 22:27:32;
rebind 2 2024/11/19 05:29:18;
expire 2 2024/11/19 07:34:18;
}
nova@ubuntu16:~$ sudo dhclient -r -lf /var/lib/dhcp/dhclient.enp1s0.leases
在rocky9上的監聽結果和日誌檔案,抓取到release包
[root@ROCKY9 ~]# tcpdump -v udp port 67
dropped privs to tcpdump
tcpdump: listening on enp1s0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
23:01:14.675478 IP (tos 0x0, ttl 64, id 35562, offset 0, flags [DF], proto UDP (17), length 328)
10.31.0.100.bootpc > ROCKY9.bootps: BOOTP/DHCP, Request from 52:54:00:10:31:a0 (oui Unknown), length 300, xid 0x486b7638, Flags [none]
Client-IP 10.31.0.100
Client-Ethernet-Address 52:54:00:10:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Release
Server-ID (54), length 4: ROCKY9
Hostname (12), length 8: "ubuntu16"
[root@ROCKY9 ~]# tail -f /var/log/dhcp.log
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Internet Systems Consortium DHCP Server 4.4.2b1
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Copyright 2004-2019 Internet Systems Consortium.
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: All rights reserved.
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: For info, please visit https://www.isc.org/software/dhcp/
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Source compiled to use binary-leases
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Wrote 2 leases to leases file.
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Listening on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Sending on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Sending on Socket/fallback/fallback-net
Nov 18 22:58:11 ROCKY9 dhcpd[1584]: Server starting service.
Nov 18 23:01:14 ROCKY9 dhcpd[1584]: DHCPRELEASE of 10.31.0.100 from 52:54:00:10:31:a0 (ubuntu16) via enp1s0 (found)
在路由rhel8上安裝dhcp-relay服務,將dmz網段(172.31.0.0/24)中主機的dhcp轉發到lan網段(10.31.0.0/24)中的rocky9(10.31.0.1/24)上。
在dmz(172.31.0.0/24)網段中安裝一臺centos6虛擬機器用於測試。安裝過程中centos6無法識別虛擬機器virtio型別的硬碟,修改xml檔案中硬碟的型別為sata
centos6的xml檔案
[root@RHEL9 ~]# cat /data/xml/centos6.xml
<domain type="kvm">
<name>centos6</name>
<uuid>002c11bb-441d-4b29-be21-85d021755c53</uuid>
<memory>1048576</memory>
<vcpu>1</vcpu>
<os>
<type arch="x86_64" machine="q35">hvm</type>
<boot dev="hd"/>
<boot dev="cdrom"/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode="host-passthrough"/>
<clock offset="utc">
<timer name="rtc" tickpolicy="catchup"/>
<timer name="pit" tickpolicy="delay"/>
<timer name="hpet" present="no"/>
</clock>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2" discard="unmap"/>
<source file="/kvm/centos6.img"/>
<target dev="sda" bus="sata"/>
</disk>
<disk type="file" device="cdrom">
<driver name="qemu" type="raw"/>
<source file="/data/iso/CentOS-6.10-x86_64-minimal.iso"/>
<target dev="sdb" bus="sata"/>
<readonly/>
</disk>
<controller type="usb" model="qemu-xhci" ports="15"/>
<controller type="pci" model="pcie-root"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<controller type="pci" model="pcie-root-port"/>
<interface type="bridge">
<source bridge="DMZbridge"/>
<mac address="52:54:00:72:31:A0"/>
</interface>
<console type="pty"/>
<channel type="unix">
<source mode="bind"/>
<target type="virtio" name="org.qemu.guest_agent.0"/>
</channel>
<input type="tablet" bus="usb"/>
<graphics type="vnc" port="5902" listen="0.0.0.0" passwd="hatred"/>
<video>
<model type="virtio"/>
</video>
<memballoon model="virtio"/>
<rng model="virtio">
<backend model="random">/dev/urandom</backend>
</rng>
</devices>
</domain>
在rocky9上編輯DHCP配置檔案,為172.31.0.0/24網段提供dhcp服務。
編輯完後,檢測無語法錯誤,重啟服務
[root@ROCKY9 ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
log-facility local6;
default-lease-time 60000;
max-lease-time 600000;
option domain-name-servers 218.2.2.2, 218.4.4.4;
shared-network dmz{
subnet 172.31.0.0 netmask 255.255.255.0 {
range 172.31.0.100 172.31.0.199;
option routers 172.31.0.254;
}
}
subnet 10.31.0.0 netmask 255.255.255.0 {
range 10.31.0.100 10.31.0.199;
option routers 10.31.0.254;
}
[root@ROCKY9 ~]# dhcpd -t -cf /etc/dhcp/dhcpd.conf
Internet Systems Consortium DHCP Server 4.4.2b1
Copyright 2004-2019 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
ldap_gssapi_principal is not set,GSSAPI Authentication for LDAP will not be used
Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcpd/dhcpd.leases
PID file: /var/run/dhcpd.pid
Source compiled to use binary-leases
[root@ROCKY9 ~]# systemctl restart dhcpd.service
透過centos6的xml檔案建立虛擬機器,安裝完成後透過vnc登入,使用指令 ~]# ifup eth0 啟動網路卡,預設透過dhcp獲取地址成功
rhel8上dhcp-relay的日誌
[root@RHEL8 ~]# tail -f /var/log/dhcprelay.log
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Internet Systems Consortium DHCP Relay Agent 4.3.6
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Copyright 2004-2017 Internet Systems Consortium.
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: All rights reserved.
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: For info, please visit https://www.isc.org/software/dhcp/
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Listening on LPF/enp3s0/52:54:00:00:31:10
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Sending on LPF/enp3s0/52:54:00:00:31:10
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Listening on LPF/enp2s0/52:54:00:00:31:72
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Sending on LPF/enp2s0/52:54:00:00:31:72
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Sending on Socket/fallback
Nov 19 00:30:18 RHEL8 dhcrelay[1513]: Dropped all capabilities.
Nov 19 00:46:00 RHEL8 dhcrelay[1513]: Forwarded BOOTREQUEST for 52:54:00:72:31:a0 to 10.31.0.1
Nov 19 00:46:01 RHEL8 dhcrelay[1513]: Forwarded BOOTREPLY for 52:54:00:72:31:a0 to 172.31.0.100
Nov 19 00:46:01 RHEL8 dhcrelay[1513]: Forwarded BOOTREQUEST for 52:54:00:72:31:a0 to 10.31.0.1
Nov 19 00:46:01 RHEL8 dhcrelay[1513]: Forwarded BOOTREPLY for 52:54:00:72:31:a0 to 172.31.0.100
rocky9上dhcp的日誌
[root@ROCKY9 ~]# tail /var/log/dhcp.log -f
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Internet Systems Consortium DHCP Server 4.4.2b1
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Copyright 2004-2019 Internet Systems Consortium.
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: All rights reserved.
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: For info, please visit https://www.isc.org/software/dhcp/
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Source compiled to use binary-leases
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Wrote 0 leases to leases file.
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Listening on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Sending on LPF/enp1s0/52:54:00:10:31:01/10.31.0.0/24
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Sending on Socket/fallback/fallback-net
Nov 19 00:35:07 ROCKY9 dhcpd[1989]: Server starting service.
Nov 19 00:46:01 ROCKY9 dhcpd[1989]: DHCPDISCOVER from 52:54:00:72:31:a0 via 172.31.0.254
Nov 19 00:46:02 ROCKY9 dhcpd[1989]: DHCPOFFER on 172.31.0.100 to 52:54:00:72:31:a0 via 172.31.0.254
Nov 19 00:46:02 ROCKY9 dhcpd[1989]: DHCPREQUEST for 172.31.0.100 (10.31.0.1) from 52:54:00:72:31:a0 via 172.31.0.254
Nov 19 00:46:02 ROCKY9 dhcpd[1989]: DHCPACK on 172.31.0.100 to 52:54:00:72:31:a0 via 172.31.0.254
rocky9上udp67埠的監聽結果
[root@ROCKY9 ~]# tcpdump -v udp port 67
dropped privs to tcpdump
tcpdump: listening on enp1s0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
00:46:01.439322 IP (tos 0x0, ttl 64, id 438, offset 0, flags [DF], proto UDP (17), length 328)
_gateway.bootps > ROCKY9.bootps: BOOTP/DHCP, Request from 52:54:00:72:31:a0 (oui Unknown), length 300, hops 1, xid 0xd6260e35, Flags [none]
Gateway-IP 172.31.0.254
Client-Ethernet-Address 52:54:00:72:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Discover
Parameter-Request (55), length 13:
Subnet-Mask (1), BR (28), Time-Zone (2), Classless-Static-Route (121)
Domain-Name (15), Domain-Name-Server (6), Hostname (12), YD (40)
YS (41), NTP (42), MTU (26), Unknown (119)
Default-Gateway (3)
00:46:02.440770 IP (tos 0x0, ttl 64, id 35258, offset 0, flags [DF], proto UDP (17), length 328)
ROCKY9.bootps > 172.31.0.254.bootps: BOOTP/DHCP, Reply, length 300, hops 1, xid 0xd6260e35, Flags [none]
Your-IP 172.31.0.100
Gateway-IP 172.31.0.254
Client-Ethernet-Address 52:54:00:72:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Offer
Server-ID (54), length 4: ROCKY9
Lease-Time (51), length 4: 60000
Subnet-Mask (1), length 4: 255.255.255.0
Domain-Name-Server (6), length 8: dns1.ctcdma.com,dns2.ctcdma.com
Default-Gateway (3), length 4: 172.31.0.254
00:46:02.441569 IP (tos 0x0, ttl 64, id 1154, offset 0, flags [DF], proto UDP (17), length 328)
_gateway.bootps > ROCKY9.bootps: BOOTP/DHCP, Request from 52:54:00:72:31:a0 (oui Unknown), length 300, hops 1, xid 0xd6260e35, Flags [none]
Gateway-IP 172.31.0.254
Client-Ethernet-Address 52:54:00:72:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: Request
Server-ID (54), length 4: ROCKY9
Requested-IP (50), length 4: 172.31.0.100
Parameter-Request (55), length 13:
Subnet-Mask (1), BR (28), Time-Zone (2), Classless-Static-Route (121)
Domain-Name (15), Domain-Name-Server (6), Hostname (12), YD (40)
YS (41), NTP (42), MTU (26), Unknown (119)
Default-Gateway (3)
00:46:02.444524 IP (tos 0x0, ttl 64, id 35260, offset 0, flags [DF], proto UDP (17), length 328)
ROCKY9.bootps > 172.31.0.254.bootps: BOOTP/DHCP, Reply, length 300, hops 1, xid 0xd6260e35, Flags [none]
Your-IP 172.31.0.100
Gateway-IP 172.31.0.254
Client-Ethernet-Address 52:54:00:72:31:a0 (oui Unknown)
Vendor-rfc1048 Extensions
Magic Cookie 0x63825363
DHCP-Message (53), length 1: ACK
Server-ID (54), length 4: ROCKY9
Lease-Time (51), length 4: 60000
Subnet-Mask (1), length 4: 255.255.255.0
Domain-Name-Server (6), length 8: dns1.ctcdma.com,dns2.ctcdma.com
Default-Gateway (3), length 4: 172.31.0.254
rocky9上的dhcp租約檔案
[root@ROCKY9 ~]# cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.4.2b1
# authoring-byte-order entry is generated, DO NOT DELETE
authoring-byte-order little-endian;
server-duid "\000\001\000\001.\316/hRT\000\0201\001";
lease 10.31.0.100 {
starts 1 2024/11/18 17:04:26;
ends 2 2024/11/19 09:44:26;
cltt 1 2024/11/18 17:04:26;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 52:54:00:10:31:a0;
client-hostname "ubuntu16";
}
lease 172.31.0.100 {
starts 1 2024/11/18 17:07:11;
ends 2 2024/11/19 09:47:11;
cltt 1 2024/11/18 17:07:11;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 52:54:00:72:31:a0;
}
指令碼自動化
[root@centos7 ~]# cat rocky9dhcp.sh
#rocky9dhcp.sh
#Date: 2024-11-19
#!/bin/bash
#安裝dhcp服務
rpm -q dhcp-server || yum install -y dhcp-server
#備份配置檔案
cp /etc/dhcp/dhcpd.conf{,.bak}
#編寫配置檔案
cat << EOF >> /etc/dhcp/dhcpd.conf
log-facility local6;
default-lease-time 60000;
max-lease-time 600000;
option domain-name-servers 218.2.2.2, 218.4.4.4;
shared-network dmz{
subnet 172.31.0.0 netmask 255.255.255.0 {
range 172.31.0.100 172.31.0.199;
option routers 172.31.0.254;
}
}
subnet 10.31.0.0 netmask 255.255.255.0 {
range 10.31.0.100 10.31.0.199;
option routers 10.31.0.254;
}
EOF
#指定日誌路徑
echo 'local6.* /var/log/dhcp.log' > /etc/rsyslog.d/dhcp.conf
#重啟日誌服務
systemctl restart rsyslog.service
#啟動dhcp服務
systemctl enable --now dhcpd.service