1. 程式人生 > >Linux常見服務器——DHCP服務器的搭建

Linux常見服務器——DHCP服務器的搭建

AC them 客戶端 ges 修改 net reload author 查看

一.基礎知識:

1.DHCP簡介:
DHCP(Dynamic Host Configuration Protocol,動態主機配置協議)通常被應用在大型的局域網絡環境中,主要作用是集中的管理、分配IP地址,使網絡環境中的主機動態的獲得IP地址、Gateway地址、DNS服務器地址等信息,並能夠提升地址的使用率。
2.DHCP服務器簡介:
DHCP服務器指的是由服務器控制一段IP地址範圍,客戶端登錄服務器時就可以自動獲得服務器分配的IP地址和子網掩碼。

二.DHCP服務器的相關配置:

今天我們就來做一個DHCP服務器:
技術分享圖片
我將虛擬機server作為dhcp服務器使用,利用destop進行測試
為了方便,將server主機名更名為dhcp.server.com,將desktop主機名更名為dhcp.test.com
下來,我們在dhcp.server.com這臺主機上進行配置:
1.查看server的ip
技術分享圖片


2.查看DHCP服務的依賴包:
技術分享圖片
3.安裝DHCP服務:
技術分享圖片

4.進行配置:

(1)開啟dhcp服務

systemctl start dhcpd

(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服務器,多臺用逗號隔開。
 30 subnet 172.25.10.0 netmask 255.255.255.0 {
      ##指定子網絡及子網掩碼
 31   range 172.25.10.10 172.25.10.20;
      ##指定IP範圍
 32   option routers 172.25.10.254;
 ##指定默認網關
 33 }
 34#### 刪除27,28行,34行及以後

下面是在dns.test.com上所做的配置:

(1)網絡參數設置:
編輯/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=dhcp
###關鍵部位為dhcp
ONBOOT=yes

(2)重啟網絡:

systemctl restart network
###在本次試驗,我遇到了如下問題:執行該命令報錯,無法通過DHCP服務器獲得ip,解決思路為:DHCP通常是用於局域網內的一個通信協議,它主要通過客戶端發送廣播數據包給整個物理網段內的所有主機,若局域網內有DHCP服務器時,才會響應客戶端的ip參數要求。所以DHCP服務器與客戶端應該在同一個物理網段內。整個DHCP數據包在服務器與客戶端間的交互情況如下圖(1):
###所以我猜測是防火墻阻止了數據包的傳送
###我采取的解決方法是:
兩臺虛擬機均執行命令:
firewall-cmd --permanent --add-service=dhcp
firewall-cmd --reload 
查看dhcp是否通行:
firewall-cmd --list-all
重啟虛擬機後問題解決。

技術分享圖片
(圖(1)DHCP數據包在服務器與客戶端間的交互情況示意)

測試機通過DHCP服務器獲得ip:
技術分享圖片
(3)將整個網絡重啟之後,如果執行的結果找到的正確的DHCP主機,那麽以下幾個文件可能會被修改。

#1.查看/etc/resolv.conf

技術分享圖片

#2.查看路由

技術分享圖片

#3.查看服務器記錄的DHCP信息
cat /var/lib/dhcpd/dhcpd.leases

技術分享圖片

三.一個配置例子

[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#

# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages (‘none‘, since DHCP v2 didn‘t
# have support for DDNS.)
ddns-update-style none;

option domain-name-servers 192.168.19.254;

default-lease-time 600;
max-lease-time 7200;

# 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.

subnet 192.168.18.0 netmask 255.255.254.0 {
range 192.168.18.20 192.168.19.200;
option routers 192.168.19.254;
option domain-name-servers 192.168.19.254;
}

Linux常見服務器——DHCP服務器的搭建