DHCP服務器

DHCP服務

  • DHCP介紹
  • DHCP應用場景
  • DHCP工作原理
  • DHCP服務器部署
  • DHCP作用域
  • DHCP超級作用域

一、DHCP介紹

在LAN(局域網(wǎng))中我們常會遇到以下的情況:

1)不知道如何配置IP地址及相關信息的員工,無法上網(wǎng);

2)IP地址配置沖突,無法上網(wǎng);

3)來訪用戶因不熟悉公司網(wǎng)絡情況無法上網(wǎng);

以上這些情況都是日常最常見也是最無腦的工作,公司網(wǎng)絡管理員需要不停的去幫忙去解決這些問題,以此來保障公司網(wǎng)絡的正常使用及員工的正常用網(wǎng)需求。而這些工作對于網(wǎng)絡管理員來說實在是太低級、太無腦、太繁瑣了,會消耗網(wǎng)絡管理員的大量工作時間,也會影響公司員工的工作效能。那么如何通過其他的方法讓計算機就能直接解決了上述問題,從而解放網(wǎng)絡管理員呢?DHCP就是一個不二的選擇。

DHCP(Dynamic Host Configuration Protocol,動態(tài)主機配置協(xié)議),通常被應用在局域網(wǎng)絡環(huán)境中,主要作用是集中的管理、分配IP地址,使網(wǎng)絡環(huán)境中的主機動態(tài)的獲得IP地址、Gateway地址、DNS服務器地址等信息,并能夠提升地址的使用率。由于DHCP是一個UDP協(xié)議,所以運行起來更加高效。

DHCP協(xié)議采用客戶端/服務器模型(C/S模型),服務端可以為客戶端提供IP、掩碼、網(wǎng)關、主機名、DNS等信息??蛻舳酥恍鑼P獲得方式設置為自動獲取即可。

目前可以提供DHCP服務的設備有很多,比如:

  • DHCP服務器(windows server、linux)
  • 硬件路由器
  • 家用寬帶路由

二、DHCP應用場景

1)公司局域網(wǎng)環(huán)境

2)家庭局域網(wǎng)環(huán)境

3)公共場合的wifi環(huán)境

4)寬帶環(huán)境網(wǎng)絡

使用DHCP的優(yōu)點:

1)傻瓜式接入:用戶只需懂得插網(wǎng)線到電腦,或者輸入WiFi密碼接入網(wǎng)絡即可實現(xiàn)聯(lián)網(wǎng)

2)IP高效利用:及時回收IP機制,保證IP的高利用性,特別是對IP不足的網(wǎng)絡

3)避免IP沖突:避免IP沖突,保證網(wǎng)絡的高效利用,保證公司員工及臨時人員高效工作

4)降低了公司網(wǎng)絡管理員的工作量,提升了工作效率

三、DHCP工作原理

image
3.1)工作方式

IP獲得需要通過發(fā)廣播來實現(xiàn)客戶端和服務器的通信,所以DHCP只能工作在局域網(wǎng)。

3.2)工作原理解析

1、Client:向網(wǎng)絡中發(fā)送廣播,通過自己的UDP協(xié)議的68號端口向網(wǎng)絡中發(fā)送DHCP Discover包,用來尋找網(wǎng)絡中的DHCP Server.類似于你在你的公司大喊一聲:"誰是公司老板"一樣的道理。

2、Server:局域網(wǎng)中的所有DHCP服務器都能收到該Client發(fā)送的廣播包,然后DHCP Server會檢查自己的IP池中(也叫做作用域)是否還有可用IP可以分發(fā)。如果有的話,會直接將這個IP地址從池中拿出來,避免在發(fā)給別的客戶端,并且通過自己的UDP協(xié)議的67號端口給Client發(fā)一個響應包DHCP Offer,同樣通信是采用廣播的方式,明確告訴其可以提供哪個IP給Client使用。類似于公司的幾個老板都在公司喊了一聲:“我是X老板,我有時間在哪個辦公室接待你”。

3、Client:Client會收到局域網(wǎng)中的所有DHCP服務器發(fā)給自己的DHCP Offer包,默認選一個最優(yōu)的DHCP Server進行IP獲取(在這里就是第一個發(fā)送給他DHCP Offer的服務器算作最優(yōu))。然后繼續(xù)向網(wǎng)絡中通過UDP的68號端口發(fā)廣播DHCP Resquest,明確指定DHCP Server IP地址和需要租用的IP地址,告訴它要從他這里獲得IP信息。自然其他DHCP Server也能收到廣播,確認不從自己這里拿IP信息后,會將上步從IP池中拿出來的IP在釋放到池中,以便別人使用。類似于你在公司大喊一聲:“李老板,我找你接待”,那么其他老板剛才計劃接待你的時間就會被釋放出來,用于接待別的客戶。

4、Server:被確認的DHCP Server就會通過其UDP協(xié)議的67號端口發(fā)送DHCP ACK確認包,采用廣播將IP、掩碼、網(wǎng)關、DNS等信息還有IP租約一起發(fā)送給DHCP Client,Client確認IP可用后,根據(jù)IP租約開始計算使用時間。類似于李老板把你請進他的辦公室,開始和你聊天,并計算聊天時間為30分鐘,開始倒計時。

3.3)計算機獲得IP的時間點

a、計算機開機

b、網(wǎng)卡接通網(wǎng)絡

c、重啟網(wǎng)卡服務

3.4)租約更新階段

a、租約完成1/2

b、租約完成7/8

c、租約到期

四、DHCP服務器部署

約定:本實驗中使用過的機器為centos7.5_x86_64系統(tǒng),計算機名稱:baism.ayitula.com,IP地址192.168.11.16/24.請關閉防火墻和SELINUX。

4.1)DHCP安裝

[root@baism ~]# yum -y install dhcp

4.2)DHCP配置文件詳解

默認情況下,dhcp服務并沒有提供配置文件,只是給提供了一個demo,存放在/usr/share/doc/dhcp*/目錄下.我們將demo文件拷貝到/etc/dhcp目錄下,并且命名為dhcpd.conf。

[root@baism ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
cp:是否覆蓋"/etc/dhcp/dhcpd.conf" y

配置文件詳解

[root@baism ~]# cat /etc/dhcp/dhcpd.conf
# #號代表注釋
# DHCP服務配置文件分為全局配置和作用域配置,很好區(qū)分:subnet的就是作用域 不在subnet里面的就是全局設置。
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

#DNS全局選項,指定DNS服務器的地址,可以是IP,也可以是域名。
# option definitions common to all supported networks...
# DNS的域名
option domain-name "example.org";

#具體的DNS服務器
option domain-name-servers ns1.example.org, ns2.example.org;

#租約設置,默認租約為600s
default-lease-time 600;
#租約設置,最大租約為7200s,當客戶端未請求明確的租約時間。
max-lease-time 7200;

#動態(tài)DNS更新方式(none:不支持;interim:互動更新模式;ad-hoc:特殊更新模式)
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

#如果該DHCP服務器是本地官方DHCP就將此選項打開,避免其他DHCP服務器的干擾。
#當一個客戶端試圖獲得一個不是該DHCP服務器分配的IP信息,DHCP將發(fā)送一個拒絕消息,而不會等待請求超時。
#當請求被拒絕,客戶端會重新向當前DHCP發(fā)送IP請求獲得新地址。
#保證IP是自己發(fā)出去的
#
# 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 定義一個作用域
#netmask 定義作用域的掩碼
#range 允許發(fā)放的IP范圍
#option routers 指定網(wǎng)關地址
#option domain-name-servers 指定DNS服務器地址
#option broadcast-address 廣播地址
#
#
#案例:定義一個作用域 網(wǎng)段為10.152.187.0 掩碼為255.255.255.0
#此作用域不提供任何服務
subnet 10.152.187.0 netmask 255.255.255.0 {
}

# This is a very basic subnet declaration.

#案例:定義一個基本的作用域
#網(wǎng)段10.254.239.0 掩碼255.255.255.224
#分發(fā)范圍10.254.239.10-20
#網(wǎng)關為rtr-239-0-1.example.org, rtr-239-0-2.example.org
subnet 10.254.239.0 netmask 255.255.255.224 {
  range 10.254.239.10 10.254.239.20;
  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}

# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
#案例:允許采用bootp協(xié)議的客戶端動態(tài)獲得地址
#bootp DHCP的前身
#BOOTP用于無盤工作站的局域網(wǎng)中,可以讓無盤工作站從一個中心服務器上獲得IP地址。通過BOOTP協(xié)議可以為局域網(wǎng)中的無盤工作站分配動態(tài)IP地址,
#這樣就不需要管理員去為每個用戶去設置靜態(tài)IP地址。
subnet 10.254.239.32 netmask 255.255.255.224 {
  range dynamic-bootp 10.254.239.40 10.254.239.60;
  option broadcast-address 10.254.239.31;
  option routers rtr-239-32-1.example.org;
}

#案例:一個簡單的作用域案例
# A slightly different configuration for an internal subnet.
subnet 10.5.5.0 netmask 255.255.255.224 {
  range 10.5.5.26 10.5.5.30;
  option domain-name-servers ns1.internal.example.org;
  option domain-name "internal.example.org";
  option routers 10.5.5.1;
  option broadcast-address 10.5.5.31;
  default-lease-time 600;
  max-lease-time 7200;
}

# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.
#
#保留地址:可以將指定的IP分發(fā)給指定的機器,根據(jù)網(wǎng)卡的MAC地址來做觸發(fā)
#host: 啟用保留。
#hardware:指定客戶端的mac地址
#filename:指定文件名
#server-name:指定下一跳服務器地址
#fixed-address: 指定保留IP地址
#
#
#案例:這個案例中分發(fā)給客戶端的不是IP地址信息,而是告訴客戶端去找toccata.fugue.com服務器,并且下載vmunix.passacaglia文件
host passacaglia {
  hardware ethernet 0:0:c0:5d:bd:95;
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}

# Fixed IP addresses can also be specified for hosts.   These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP.   Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
# 案例:保留地址,將指定IP(fantasia.fugue.com對應的IP)分給指定客戶端網(wǎng)卡(MAC:08:00:07:26:c0:a5)
host fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;
}

#超級作用域
#超級作用域是DHCP服務中的一種管理功能,使用超級作用域,可以將多個作用域組合為單個管理實體。
# You can declare a class of clients and then do address allocation
# based on that.   The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.

#在局域網(wǎng)中,可以配置策略根據(jù)各個機器的具體信息分配IP地址和其他的網(wǎng)絡參數(shù),客戶機的具體信息:客戶機能夠給dhcp服務提供的信息由兩個,
#第一個就是網(wǎng)卡的dhcp-client-identifier(mac地址),
#第二個就是設備的vendor-class-identifier。
#管理員可以根據(jù)這兩個信息給不同的機器分組。

#案例:
#按client某種類型分組DHCP,而不是按物理接口網(wǎng)段
#例子: SUNW 分配地址段10.17.224.0/24
#     非SUNW的主機,分配地址段10.0.29.0/24
#定義一個dhcp類:foo
#request廣播中vendor-class-identifier字段對應的值前四個字節(jié)如果是"SUNW",則視合法客戶端.
class "foo" {
  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}

#定義一個超級作用域: 224-29
shared-network 224-29 {
#定義第一個作用域
  subnet 10.17.224.0 netmask 255.255.255.0 {
    option routers rtr-224.example.org;
  }
#定義第二個作用域
  subnet 10.0.29.0 netmask 255.255.255.0 {
    option routers rtr-29.example.org;
  }

#關連池,如果客戶端匹配foo類,將獲得該池地址
  pool {
    allow members of "foo";
    range 10.17.224.10 10.17.224.250;
  }
#關連池,如果客戶端配置foo類,則拒絕獲得該段地址
  pool {
    deny members of "foo";
    range 10.0.29.10 10.0.29.230;
  }
}

4.3)DHCP啟動

[root@baism ~]# systemctl enable dhcpd
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
[root@baism ~]# systemctl start dhcpd

注意:可能發(fā)現(xiàn)無法啟動DHCP服務,原因是DHCP在啟動的時候檢查配置文件,發(fā)現(xiàn)并沒有有效作用域(和服務器同網(wǎng)段的作用域)。

五、DHCP作用域

教學案例一、配置一個作用域,用于為本地局域網(wǎng)中的計算機發(fā)放IP信息。要求:

本地網(wǎng)段:192.168.11.0/24

發(fā)放IP地址:192.168.11.153--252

網(wǎng)關:192.168.11.254

DNS1:202.106.0.20

DNS2:114.114.114.114

默認租約為兩個小時

最大租約為3個小時

本DHCP服務器為本地權威DHCP,要求可以本地所有計算機獲得IP都是由本DHCP發(fā)放

5.1)DHCP服務配置

[root@baism dhcp]# cat /etc/dhcp/dhcpd.conf
option domain-name-servers 4.2.2.2, 4.2.2.1;

default-lease-time 28800;
max-lease-time 43200;
authoritative;
log-facility local7;

subnet 192.168.11.0 netmask 255.255.255.0 {
  range 192.168.11.153 192.168.11.252;
  option domain-name-servers 202.106.0.20, 114.114.114.114;
  option routers 192.168.11.254;
  option broadcast-address 192.168.11.255;
  default-lease-time 7200;
  max-lease-time 10800;
}

請根據(jù)4.2中的講解理解配置文件內(nèi)容。

5.2)重啟DHCP服務,生效配置

#重啟dhcpd服務
[root@baism dhcp]# systemctl restart dhcpd

#查看啟動情況,同時也驗證了客戶端使用的是68端口,服務端使用的是67端口
[root@baism dhcp]# lsof -i :68
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
dhclient 55234 root    6u  IPv4 110700      0t0  UDP *:bootpc 
[root@baism dhcp]# lsof -i :67
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
dnsmasq  1507 nobody    3u  IPv4  27149      0t0  UDP *:bootps 
dhcpd   56570  dhcpd    7u  IPv4 129157      0t0  UDP *:bootps

5.3)測試IP分發(fā)

打開一個客戶端機器,IP獲得方式為自動獲取,測試是否獲得到了自己這個DHCP服務器發(fā)放的IP地址。本例子中測試機使用了centos 6.5系統(tǒng)。來看下測試結果吧!

1) 查看一下當前eth0的IP地址、MAC地址,并保證其IP獲得方式為:DHCP
[root@bogon 桌面]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:BD  
          inet addr:172.16.44.132  Bcast:172.16.44.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1a:f8bd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:86 errors:0 dropped:0 overruns:0 frame:0
          TX packets:63 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:15294 (14.9 KiB)  TX bytes:6769 (6.6 KiB)

2)使用dhclient命令來獲得IP,看一下重要輸出
[root@bogon 桌面]# dhclient -d eth0
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/00:0c:29:1a:f8:bd
Sending on   LPF/eth0/00:0c:29:1a:f8:bd
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6 (xid=0x316768c3)      發(fā)廣播尋找DHCP服務器
DHCPOFFER from 192.168.11.16                                                     192.168.11.16DHCP服務器應答
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x316768c3)                  client向服務器請求IP地址
DHCPACK from 192.168.11.16 (xid=0x316768c3)                                      確認租賃關系
bound to 192.168.11.156 -- renewal in 2983 seconds.                              client分得IP:192.168.11.156
^C

注意:看到這些信息后,按CTRL+C退出。
dhclient是一個DHCP協(xié)議客戶端,它使用DHCP協(xié)議或者BOOTP協(xié)議或在這兩個協(xié)議都不可用時使用靜態(tài)地址來配置一個或多個網(wǎng)絡接口
dhclient -r 釋放IP地址
dhclient -d 強制dhclient作為前臺進程運行。 通常情況下,DHCP客戶端將在前臺運行,直到配置了一個接口,此時它將恢復為在后
            臺運行。               

3) 服務器日志查看驗證獲取信息
[root@baism ~]# tailf /var/log/messages
Feb 21 13:40:44 baism dhcpd: DHCPDISCOVER from 00:0c:29:1a:f8:bd via ens33
Feb 21 13:40:45 baism dhcpd: DHCPOFFER on 192.168.11.156 to 00:0c:29:1a:f8:bd via ens33
Feb 21 13:40:45 baism dhcpd: DHCPREQUEST for 192.168.11.156 (192.168.11.16) from 00:0c:29:1a:f8:bd via ens33
Feb 21 13:40:45 baism dhcpd: DHCPACK on 192.168.11.156 to 00:0c:29:1a:f8:bd via ens33

4) 在client上通過ifconfig命令再次查看eth0 IP地址,驗證是否為192.168.11.156
[root@bogon 桌面]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:BD  
          inet addr:192.168.11.156  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1a:f8bd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:97 errors:0 dropped:0 overruns:0 frame:0
          TX packets:67 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17364 (16.9 KiB)  TX bytes:7537 (7.3 KiB)

5)查看網(wǎng)關,確定網(wǎng)關為192.168.11.254
[root@bogon 桌面]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.11.254  0.0.0.0         UG    0      0        0 eth0

6)查看DNS配置文件,看DNS是否為DHCP服務器發(fā)放的DNS服務器IP
[root@bogon 桌面]# cat /etc/resolv.conf 
; generated by /sbin/dhclient-script
nameserver 202.106.0.20
nameserver 114.114.114.114

5.4)通過DHCP服務器租約文件查看具體租約

租約文件的路徑: /var/lib/dhcpd/dhcpd.leases

[root@baism ~]# 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.2.5

lease 192.168.11.154 {
  starts 4 2019/02/21 03:41:12;
  ends 4 2019/02/21 03:43:12;
  tstp 4 2019/02/21 03:43:12;
  cltt 4 2019/02/21 03:41:12;
  binding state free;
  hardware ethernet ac:87:a3:02:8d:5d;
  uid "\001\254\207\243\002\215]";
  client-hostname "baismdeMBP";
}
lease 192.168.11.153 {
  starts 4 2019/02/21 03:54:27;
  ends 4 2019/02/21 03:56:27;
  tstp 4 2019/02/21 03:56:27;
  cltt 4 2019/02/21 03:54:27;
  binding state free;
  hardware ethernet 00:0c:29:97:97:11;
  uid "\001\000\014)\227\227\021";
  client-hostname "cool-PC";
}
server-duid "\000\001\000\001$\000\315\373\000\014) u\271";

lease 192.168.11.155 {
  starts 4 2019/02/21 13:37:42;
  ends 4 2019/02/21 15:37:42;
  cltt 4 2019/02/21 13:37:42;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:1a:f8:c7;
}

#發(fā)現(xiàn)租約
lease 192.168.11.156 {
  starts 4 2019/02/21 13:40:45;   租約開始時間
  ends 4 2019/02/21 15:40:45;     租約結束時間,正好是兩個小時
  cltt 4 2019/02/21 13:40:45;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:1a:f8:bd;
}

注意:當你發(fā)現(xiàn)這里的時間和你的服務器時間不一致的時候,建議你修改時區(qū)解決問題,一般是差8個小時,大家明白就好。

5.5)保留IP

在IP租約到期后,如果無法續(xù)訂租約,client只能乖乖交出IP地址,重新獲得一個其他IP使用。但是在公司有些服務器的IP地址是不能變化的,因為變了用戶就無法連接到服務器了,比如公司文件服務器、打印服務器等等。那么在這種環(huán)境中我們既想使用DHCP管理公司IP,又想實現(xiàn)部分機器的IP永久不變,那么怎么實現(xiàn)呢。

DHCP的作者在寫DHCP的時候也想到了這個問題,提出了保留IP的概念,就是將某些IP保留,然后服務器來獲得IP的時候,根據(jù)其MAC地址做匹配,將對應的IP分給它即可。

教學案例:希望這個MAC地址為00:0C:29:1A:F8:C7的網(wǎng)卡能永久獲得IP 192.168.11.252,實現(xiàn)方式如下:

 a、在配置文件/etc/dhcp/dhcpd.conf末尾添加以下內(nèi)容
 host print {         
  hardware ethernet 00:0C:29:1A:F8:C7;
  fixed-address 192.168.11.252;
}

host print    host為指令,print是個名字,隨便起,但是最好有意義,要不過一段你也記不住了。
hardware ethernet 指定以太網(wǎng)網(wǎng)卡MAC地址
fixed-address 指定要綁定的IP

b、重啟DHCP服務
[root@baism ~]# systemctl restart dhcpd

c、測試,登陸測試機,釋放擋墻IP,重新獲得新的IP,查看IP地址是否正確分發(fā)
[root@bogon 桌面]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:C7  
          inet addr:192.168.11.155  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1a:f8c7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4071 errors:0 dropped:0 overruns:0 frame:0
          TX packets:187 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:433880 (423.7 KiB)  TX bytes:16888 (16.4 KiB)

[root@bogon 桌面]# dhclient -r eth1

[root@bogon 桌面]# dhclient -d eth1
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth1/00:0c:29:1a:f8:c7
Sending on   LPF/eth1/00:0c:29:1a:f8:c7
Sending on   Socket/fallback
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 4 (xid=0x45c162c2)
DHCPOFFER from 192.168.11.16
DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0x45c162c2)
DHCPACK from 192.168.11.16 (xid=0x45c162c2)
bound to 192.168.11.252 -- renewal in 2881 seconds.
^C

[root@bogon 桌面]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:C7  
          inet addr:192.168.11.252  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1a:f8c7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4081 errors:0 dropped:0 overruns:0 frame:0
          TX packets:191 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:436337 (426.1 KiB)  TX bytes:17656 (17.2 KiB)

完美!

六、DHCP超級作用域

體驗了DHCP服務器給大家?guī)淼姆奖愫螅覀児ぷ鬏p松了很多,但是隨著時間的推移,突然有這么一個問題急需你解決,由于公司的發(fā)展壯大,公司人員數(shù)量越來越多,公司一個網(wǎng)段的IP無法滿足日常使用,所以又加了一個網(wǎng)段。但是默認情況下,DHCP服務器只能發(fā)放和自己網(wǎng)卡在同一網(wǎng)段的IP地址,目前我們DHCP的網(wǎng)卡IP地址為192.168.11.0段,我們新加的網(wǎng)段為192.168.12.0,那么怎么能讓DHCP服務器既能發(fā)11網(wǎng)段,又能發(fā)放12網(wǎng)段呢?學會超級作用域就可以解決這個問題。

超級作用域:將兩個或以上的不同網(wǎng)段的作用域合成一個作用域既是超級作用域。

案例:部署一個超級作用域,作用域是192.168.11.0/24網(wǎng)段,192。168.12.0/24網(wǎng)段。

a、編輯配置文件
[root@baism ~]# cat /etc/dhcp/dhcpd.conf
option domain-name-servers 4.2.2.2, 4.2.2.1;

default-lease-time 28800;
max-lease-time 43200;
#authoritative;
log-facility local7;

#share-network 部署一個超級作用域
#supper 超級作用域名稱,隨便起,但是建議有意義。
shared-network supper {
#192.168.11.0作用域
subnet 192.168.11.0 netmask 255.255.255.0 {
range 192.168.11.150 192.168.11.150;
option domain-name-servers 202.106.0.20, 114.114.114.114;
option routers 192.168.11.254;
default-lease-time 7200;
max-lease-time 10800;
}

#192.168.12.0作用域
subnet 192.168.12.0 netmask 255.255.255.0 {
range 192.168.12.150 192.168.12.150;
option domain-name-servers 202.106.0.20, 114.114.114.114;
option routers 192.168.12.254;
default-lease-time 7200;
max-lease-time 10800;
}

}

注意:案例中為了方便驗證,我每個作用域只發(fā)布一個IP,否者測試無法保證能100%分到兩個網(wǎng)段。

b、重啟DHCP服務,生效配置文件
[root@baism dhcp]# systemctl restart dhcpd

c、驗證
#釋放兩網(wǎng)卡IP
[root@bogon 桌面]# dhclient -r eth0
[root@bogon 桌面]# dhclient -r eth1

#釋放成功
[root@bogon 桌面]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:BD  
          inet6 addr: fe80::20c:29ff:fe1a:f8bd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1591 errors:0 dropped:0 overruns:0 frame:0
          TX packets:162 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:282324 (275.7 KiB)  TX bytes:30619 (29.9 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:C7  
          inet6 addr: fe80::20c:29ff:fe1a:f8c7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4719 errors:0 dropped:0 overruns:0 frame:0
          TX packets:216 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:559954 (546.8 KiB)  TX bytes:19582 (19.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:720 (720.0 b)  TX bytes:720 (720.0 b)

#分別獲取IP地址 
[root@bogon 桌面]# dhclient -d eth0
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/00:0c:29:1a:f8:bd
Sending on   LPF/eth0/00:0c:29:1a:f8:bd
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6 (xid=0x2574199a)
DHCPOFFER from 192.168.11.16
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x2574199a)
DHCPACK from 192.168.11.16 (xid=0x2574199a)
bound to 192.168.11.150 -- renewal in 3026 seconds.
^C
#確保都是來自我們實驗中的DHCP服務器
[root@bogon 桌面]# dhclient -d eth1
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth1/00:0c:29:1a:f8:c7
Sending on   LPF/eth1/00:0c:29:1a:f8:c7
Sending on   Socket/fallback
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 7 (xid=0x2cebde11)
DHCPOFFER from 192.168.11.16
DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0x2cebde11)
DHCPACK from 192.168.11.16 (xid=0x2cebde11)
bound to 192.168.12.150 -- renewal in 3102 seconds.
^C
#確保都是來自我們實驗中的DHCP服務器

#查看IP情況,發(fā)現(xiàn)實驗成功了,分別獲得到了不同網(wǎng)段IP
[root@bogon 桌面]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:BD  
          inet addr:192.168.11.150  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1a:f8bd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1613 errors:0 dropped:0 overruns:0 frame:0
          TX packets:166 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:287076 (280.3 KiB)  TX bytes:31387 (30.6 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:1A:F8:C7  
          inet addr:192.168.12.150  Bcast:192.168.12.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1a:f8c7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4741 errors:0 dropped:0 overruns:0 frame:0
          TX packets:220 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:564706 (551.4 KiB)  TX bytes:20350 (19.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:720 (720.0 b)  TX bytes:720 (720.0 b)

七、補充

7.1) 如果客戶端獲得不到 IP會怎么辦

當客戶端獲得不到IP地址,會得到一個169.254開頭的臨時IP,此IP不能和其他網(wǎng)段通信,但是Client會繼續(xù)向網(wǎng)絡中發(fā)DHCP廣播,繼續(xù)堅持不懈申請IP。

7.2)DHCP會面臨單機故障,如何解決

兩臺設備互相分發(fā)對方網(wǎng)段一段IP,將作用域采用8/2原則,彼此互相冗余,當一臺服務器出現(xiàn)問題,不至于整個網(wǎng)段故障。

7.3)抓包驗證方法

[root@baism dhcp]# tcpdump -nn -vvv -s 1500 -i ens33 host 192.168.11.16 and udp port 67 or udp port 68

7.4)如何發(fā)放計算機名稱

發(fā)放計算機名稱只能在保留中完成,要求Client的計算機名配置文件中將對應字段刪除。

option domain-name-servers 4.2.2.2, 4.2.2.1;

default-lease-time 28800;
max-lease-time 43200;
authoritative;
log-facility local7;

subnet 192.168.11.0 netmask 255.255.255.0 {
  range 192.168.11.153 192.168.11.252;
  option domain-name-servers 202.106.0.20, 114.114.114.114;
  option routers 192.168.11.254;
  option broadcast-address 192.168.11.255;
  default-lease-time 7200;
  max-lease-time 10800;
}

host print {
  #指定計算機名稱
  option host-name "test.ayitula.com";
  hardware ethernet 00:0c:29:af:f1:84;
  fixed-address 192.168.11.252;
}

注意:請把/etc/hostname 中的計算機名稱清除
      /etc/sysconfig/network中的hostname字段清除
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容