目錄
- 一.基礎(chǔ)知識:
- 二.DHCP服務(wù)器的相關(guān)配置:
- 1.查看server的ip
- 2.查看DHCP服務(wù)的依賴包:
- 3.安裝DHCP服務(wù):
- 4.進(jìn)行配置:
- 三.一個配置例子
一.基礎(chǔ)知識:
1.DHCP簡介:
DHCP(Dynamic Host Configuration Protocol,動態(tài)主機(jī)配置協(xié)議)通常被應(yīng)用在大型的局域網(wǎng)絡(luò)環(huán)境中,主要作用是集中的管理、分配IP地址,使網(wǎng)絡(luò)環(huán)境中的主機(jī)動態(tài)的獲得IP地址、Gateway地址、DNS服務(wù)器地址等信息,并能夠提升地址的使用率。
2.DHCP服務(wù)器簡介:
DHCP服務(wù)器指的是由服務(wù)器控制一段IP地址范圍,客戶端登錄服務(wù)器時就可以自動獲得服務(wù)器分配的IP地址和子網(wǎng)掩碼。
二.DHCP服務(wù)器的相關(guān)配置:
今天我們就來做一個DHCP服務(wù)器:
我將虛擬機(jī)server作為dhcp服務(wù)器使用,利用destop進(jìn)行測試
為了方便,將server主機(jī)名更名為dhcp.server.com,將desktop主機(jī)名更名為dhcp.test.com
下來,我們在dhcp.server.com這臺主機(jī)上進(jìn)行配置:
1.查看server的ip
2.查看DHCP服務(wù)的依賴包:
3.安裝DHCP服務(wù):
![](/d/20211016/a2d217a3018f1ad419c0132bb07a87a5.gif)
4.進(jìn)行配置:
(1)開啟dhcp服務(wù)
(2)更改配置文件
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
vim /etc/dhcp/dhcpd.conf
更改以下部分:
7 option domain-name "server.com";
##域名:參見/etc/resolv.conf
8 option domain-name-servers 172.25.10.254;
##指定dns服務(wù)器,多臺用逗號隔開。
30 subnet 172.25.10.0 netmask 255.255.255.0 {
##指定子網(wǎng)絡(luò)及子網(wǎng)掩碼
31 range 172.25.10.10 172.25.10.20;
##指定IP范圍
32 option routers 172.25.10.254;
##指定默認(rèn)網(wǎng)關(guān)
33 }
34#### 刪除27,28行,34行及以后
下面是在dns.test.com上所做的配置:
(1)網(wǎng)絡(luò)參數(shù)設(shè)置:
編輯/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
###關(guān)鍵部位為dhcp
ONBOOT=yes
(2)重啟網(wǎng)絡(luò):
systemctl restart network
###在本次試驗(yàn),我遇到了如下問題:執(zhí)行該命令報錯,無法通過DHCP服務(wù)器獲得ip,解決思路為:DHCP通常是用于局域網(wǎng)內(nèi)的一個通信協(xié)議,它主要通過客戶端發(fā)送廣播數(shù)據(jù)包給整個物理網(wǎng)段內(nèi)的所有主機(jī),若局域網(wǎng)內(nèi)有DHCP服務(wù)器時,才會響應(yīng)客戶端的ip參數(shù)要求。所以DHCP服務(wù)器與客戶端應(yīng)該在同一個物理網(wǎng)段內(nèi)。整個DHCP數(shù)據(jù)包在服務(wù)器與客戶端間的交互情況如下圖(1):
###所以我猜測是防火墻阻止了數(shù)據(jù)包的傳送
###我采取的解決方法是:
兩臺虛擬機(jī)均執(zhí)行命令:
firewall-cmd --permanent --add-service=dhcp
firewall-cmd --reload
查看dhcp是否通行:
firewall-cmd --list-all
重啟虛擬機(jī)后問題解決。
(圖(1)DHCP數(shù)據(jù)包在服務(wù)器與客戶端間的交互情況示意)
測試機(jī)通過DHCP服務(wù)器獲得ip:
(3)將整個網(wǎng)絡(luò)重啟之后,如果執(zhí)行的結(jié)果找到的正確的DHCP主機(jī),那么以下幾個文件可能會被修改。
1.查看/etc/resolv.conf
![](/d/20211016/de11dc3e58c73e2842e5c98ecb1b99ff.gif)
2.查看路由
![](/d/20211016/8ce069f2353ee934b0b0378a4d2ab73a.gif)
3.查看服務(wù)器記錄的DHCP信息cat /var/lib/dhcpd/dhcpd.leases
![](/d/20211016/23ea54a7f41e0adbb11ca3a2dc68b8c9.gif)
三.一個配置例子
cat /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 192.168.0.199;
default-lease-time 600;
max-lease-time 7200;
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
# 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;
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
# This is a very basic subnet declaration.
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.20;
option routers 192.168.0.199;
}
到此這篇關(guān)于Linux實(shí)現(xiàn)DHCP服務(wù)器的搭建的文章就介紹到這了,更多相關(guān)Linux DHCP服務(wù)器搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!