動態主機配置協議(DHCP)是一種基於UDP協議且僅限於在局域網內部使用的網絡協議,主要用於大型的局域網環境或者存在較多移動辦公設備的局域網環境中,其主要用途是爲局域網內部的設備或網絡供應商自動分配IP地址等參數。linux
DHCP概念拓撲圖:vim
DHCP(Dynamic Host Configuration Protocol,動態主機配置協議)是一個局域網的網絡協議,使用UDP協議工做。它是一種流行的Client/Server協議,通常用於爲主機或者爲路由器等指定相關的配置信息。DHCP服務在企業和家庭中獲得了大量的應用,它可以自動分配ip地址以及一些其餘的相關信息,整個過程對客戶透明。centos
DHCP分配方式bash
自動分配方式(Automatic Allocation),DHCP服務器爲主機指定一個永久性的IP地址,一旦DHCP客戶端第一次成功從DHCP服務器端租用到IP地址後,就能夠永久性的使用該地址。
動態分配方式(Dynamic Allocation),DHCP服務器給主機指定一個具備時間限制的IP地址,時間到期或主機明確表示放棄該地址時,該地址能夠被其餘主機使用。
手工分配方式(Manual Allocation),客戶端的IP地址是由網絡管理員指定的,DHCP服務器只是將指定的IP地址告訴客戶端主機。服務器
DHCP工做過程
DHCP客戶機在啓動時,會搜尋網絡中是否存在DHCP服務器。若是找到,則給DHCP服務器發送一個請求。DHCP服務器接到請求後,爲DHCP客戶機選擇TCP/IP配置的參數,並把這些參數發送給客戶端。 若是已配置衝突檢測設置,則DHCP服務器在將租約中的地址提供給客戶機以前會使用Ping測試做用域中每一個可用地址的連通性。這可確保提供給客戶的每一個IP地址都沒有被使用手動TCP/IP配置的另外一臺非DHCP計算機使用。網絡
根據客戶端是否第一次登陸網絡,DHCP的工做形式會有所不一樣。less
初次登陸dom
初次登陸時DHCP工做包括四個步驟
一、尋找DHCP服務器
當DHCP客戶端第一次登陸網絡的時候,計算機發現本機上沒有任何IP地址設定,將以廣播方式發送DHCP discover發現信息來尋找DHCP服務器,即向255.255.255.255發送特定的廣播信息。網絡上每一臺安裝了TCP/IP協議的主機都會接收這個廣播信息,但只有DHCP服務器纔會作出響應。
二、分配IP地址
在網絡中接收到DHCP discover發現信息的DHCP服務器就會作出響應,它從還沒有分配的IP地址池中挑選一個分配給DHCP客戶機,並向DHCP客戶機發送一個包含分配的IP地址和其餘設置的DHCP offer提供信息。
三、接受IP地址
DHCP客戶端接受到DHCP offer提供信息以後,選擇第一個接收到的提供信息,而後以廣播的方式回答一個DHCP request請求信息,該信息包含向它所選定的DHCP服務器請求IP地址的內容。
四、IP地址分配確認
當DHCP服務器收到DHCP客戶端回答的DHCP request請求信息以後,便向DHCP客戶端發送一個包含它所提供的IP地址和其餘設置的DHCP ack確認信息,告訴DHCP客戶端可使用它提供的IP地址。而後,DHCP客戶機便將其TCP/IP協議與網卡綁定,另外,除了DHCP客戶機選中的DHCP服務器外,其餘的DHCP服務器將收回曾經提供的IP地址。ide
網絡配置測試
DHCP: (Dynamic Host Configuration Protocol)
主要用途:
使用場景
DHCP共有八種報文
DHCP DISCOVER:客戶端到服務器
DHCP OFFER :服務器到客戶端
DHCP REQUEST:客戶端到服務器
DHCP ACK :服務器到客戶端
DHCP NAK:服務器到客戶端,通知用戶沒法分配合適的IP地址
DHCP DECLINE :客戶端到服務器,指示地址已被使用
DHCP RELEASE:客戶端到服務器,放棄網絡地址和取消剩餘的租約時間
DHCP INFORM:客戶端到服務器, 客戶端若是須要從DHCP服務器端獲取更爲詳細的配置信息,則發送Inform報文向服務器進行請求,極少用到
DHCP相關文件信息:
[root@centos6x86_64]#cd /var/lib/dhclient [root@centos6dhclient]#cat dhclient.leases lease { interface "eth0"; fixed-address 192.168.34.105; option subnet-mask 255.255.255.0; option routers 192.168.34.2; option dhcp-lease-time 1800; option dhcp-message-type 5; option domain-name-servers 192.168.34.2; 獲取的DNS option dhcp-server-identifier 192.168.34.254; 獲取的DHCP地址 option broadcast-address 192.168.34.255; option domain-name "localdomain"; renew 2 2019/10/29 12:15:35; 續租時間 rebind 2 2019/10/29 12:27:37; expire 2 2019/10/29 12:31:22; 能夠看到當前dhcp的有效期 }
續租
1)50% :租賃時間達到50%時來續租,剛向DHCP服務器發向新的DHCPREQUEST請求。若是dhcp服務沒有拒絕的理由,則迴應DHCPACK信息。當DHCP客戶端收到該應答信息後,就從新開始新的租用週期 2)87.5%:若是以前DHCP Server沒有迴應續租請求,等到租約期的7/8時,主機會再發送一次廣播請求
同網段多DHCP服務
1)DHCP服務必須基於本地 2)先到先得的原則
跨網段
1)RFC 1542 Compliant Routers 2)dhcrelay: 中繼 用戶經過ARP廣播到達DHCP時,若是中間有路由器阻擋,能夠經過中繼進行傳遞,返回時也能夠經過中繼到達用戶的過程
相關協議
Arp 用戶經過ARP廣播到達DHCP rarp
DHCP服務器會自動把IP地址、子網掩碼、網關、DNS地址等網絡信息分配給有須要的客戶端,並且當客戶端的租約時間到期後還能夠自動回收所分配的IP地址,以便交給新加入的客戶端。
手動搭建一臺DHCP服務器,使其達到該網段內的全部主機,都能經過該服務器自動獲取ip地址,而且獲取到的ip地址是本身在DHCP服務器上所指定的網段
DHCP服務程序配置:
1)在A主機(服務器搭建DHCP服務)網卡選擇僅主機模式,充當服務端端,搭建DHCP服務地址。 服務器的DHCP端口是67
B主機充當客戶端,也選擇僅主機模式,將vmnet1的使用本地DHCP服務去掉。 客戶端的DHCP端口是68
2)將VMnet1僅主機模式的使用本地DHCP服務那欄去掉對勾
在A主機(服務器)上安裝DHCP包:
[root@centos6~]#yum install dhcp -y Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Repository 'development' is missing name in configuration, using id Repository 'ELPL' is missing name in configuration, using id Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package dhcp.x86_64 12:4.1.1-60.P1.el6.centos will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================================== Package Arch Version Repository Size ====================================================================================================================== Installing: dhcp x86_64 12:4.1.1-60.P1.el6.centos development 824 k Transaction Summary ====================================================================================================================== Install 1 Package(s) Total download size: 824 k Installed size: 1.9 M Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : 12:dhcp-4.1.1-60.P1.el6.centos.x86_64 1/1 Verifying : 12:dhcp-4.1.1-60.P1.el6.centos.x86_64 1/1 Installed: dhcp.x86_64 12:4.1.1-60.P1.el6.centos Complete!
DHCP的相關配置文件:
[root@centos6~]#rpm -ql dhcp /etc/dhcp/dhcpd.conf DHCP配置文件 /var/lib/dhcpd DHCP主程序 /var/lib/dhcpd/dhcpd.leases 租出去的DHCP地址信息
啓動DHCP服務:
[root@centos7~]#systemctl start dhcpd 沒法啓動 Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details. [root@centos7~]#systemctl status dhcpd 查看爲什麼不能啓動 ● dhcpd.service - DHCPv4 Server Daemon Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2019-10-29 20:42:01 CST; 1s ago Docs: man:dhcpd(8) man:dhcpd.conf(5) Process: 7572 ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid (code=exited, status=1/FAILURE) Main PID: 7572 (code=exited, status=1/FAILURE) Oct 29 20:42:01 centos7.localdomain dhcpd[7572]: Internet Systems Consortium DHCP Server 4.2.5 Oct 29 20:42:01 centos7.localdomain dhcpd[7572]: Copyright 2004-2013 Internet Systems Consortium. Oct 29 20:42:01 centos7.localdomain dhcpd[7572]: All rights reserved. Oct 29 20:42:01 centos7.localdomain dhcpd[7572]: For info, please visit https://www.isc.org/software/dhcp/ Oct 29 20:42:01 centos7.localdomain dhcpd[7572]: Not searching LDAP since ldap-server, ldap-port and ldap-base-...file Oct 29 20:42:01 centos7.localdomain dhcpd[7572]: Wrote 0 leases to leases file. Oct 29 20:42:01 centos7.localdomain systemd[1]: dhcpd.service: main process exited, code=exited, status=1/FAILURE Oct 29 20:42:01 centos7.localdomain systemd[1]: Failed to start DHCPv4 Server Daemon. 有一個錯誤提示 Oct 29 20:42:01 centos7.localdomain systemd[1]: Unit dhcpd.service entered failed state. Oct 29 20:42:01 centos7.localdomain systemd[1]: dhcpd.service failed. Hint: Some lines were ellipsized, use -l to show in full.
剛開始寫入DHCP配置文件信息,沒有任何內容,能夠將DHCP服務按提示從新複製一下:
vim /etc/dhcp/dhcpd.conf 查看DHCP裏邊的配置文件 # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.example # see dhcpd.conf(5) man page [root@centos7~]#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf 複製裏邊的提示覆蓋以前的dhcp配置信息,再次打開/etc/dhcp/dhcpd.conf裏邊的配置就會有數據 cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
修改DHCP裏邊的配置文件:
[root@centos7~]#vim /etc/dhcp/dhcpd.conf # option definitions common to all supported networks... option domain-name "pexliinux.0"; option domain-name-servers 8.8.8.8; default-lease-time 86400; 設置當前的IP地址有效期,單位s max-lease-time 864000; 設置申請最大有效期 # 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.34.0 netmask 255.255.255.0 { 聲明IP地址段和子網掩碼 range 192.168.34.20 192.168.34.200; 地址池:設置一個地址段192.168.34.20-200 option routers 192.168.34.1; 指定網關:192.168.34.1 option domain-name-servers 8.8.8.8,1.1.1.1; 獲取DNS:8.8.8.8和1.1.1.1 next-server 192.168.34.101; tftp服務IP地址,搭建DHCP服務可不寫 filename "pexlinux.0"; 安裝系統的引導計算機的文件,搭建DHCP服務可不寫 } # This is a very basic subnet declaration. 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. 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. host passacaglia { hardware ethernet 0:0:c0:5d:bd:95; 輸入對方的MAC地址 filename-adress 192.168.34.66 固定給用戶一個IP地址,當用戶鏈接時給一個固定的IP地址,固定地址不能使用地址池分配的地址 } # 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. host fantasia { hardware ethernet 08:00:07:26:c0:a5; fixed-address fantasia.fugue.com; }
修改完後的DHCP服務器要進行重啓:
[root@centos7~]#systemctl restart dhcpd 重啓服務 Failed to restart dhcpd\xe3\x80\x80.service: Unit not found. [root@centos7~]#systemctl restart dhcpd [root@centos7~]#ss -nulp State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:45993 *:* users:(("rpc.statd",pid=6633,fd=8)) UNCONN 0 0 192.168.122.1:53 *:* users:(("dnsmasq",pid=7002,fd=5)) UNCONN 0 0 *:67 (咱們能夠看到當前的67端口已監聽) *:* users:(("dhcpd",pid=12000,fd=7)) UNCONN 0 0 *%virbr0:67 *:* users:(("dnsmasq",pid=7002,fd=3)) UNCONN 0 1280 *:68 *:* users:(("dhclient",pid=11811,fd=6)) UNCONN 0 0 *:111 *:* users:(("rpcbind",pid=6230,fd=5),("systemd",pid=1,fd=48)) UNCONN 0 0 *:5353 *:* users:(("avahi-daemon",pid=6280,fd=12)) UNCONN 0 0 127.0.0.1:874 *:* users:(("rpc.statd",pid=6633,fd=7)) UNCONN 0 0 *:892 *:* users:(("rpcbind",pid=6230,fd=10)) UNCONN 0 0 *:55172 *:* users:(("avahi-daemon",pid=6280,fd=13)) UNCONN 0 0 :::111 :::* users:(("rpcbind",pid=6230,fd=7),("systemd",pid=1,fd=59)) UNCONN 0 0 :::45489 :::* users:(("rpc.statd",pid=6633,fd=10)) UNCONN 0 0 :::892 :::* users:(("rpcbind",pid=6230,fd=11))
此時咱們經過dhclient -d命令能夠查看當前獲取的IP地址來自於誰分配的從下面的能夠看出,若是不加-d 就是後臺運行。
DHCPACK from 192.168.34.30 獲取的IP地址來自哪裏 bound to 192.168.34.32 renewal in 276 seconds 客戶端獲取的IP地址是192.168.34.32,有效期的一半是276s
啓動客戶端的網卡:
service network start
能夠看到當前的IP地址和咱們本身獲取跟蹤的IP地址192.168.34.32同樣,DHCP服務搭建完畢。