Linux DHCP搭建與維護

來源:互聯網
上載者:User

標籤:linux   伺服器   ip地址   網路管理   設定檔   

如何快速為100多台主機快速分配IP地址?

如何為隨處工作的員工分配主機地址?


DHCP的概念:動態主機設定通訊協定 Dynamic Host Configuration Protocol


我們可以實現的自動分配包括:

--IP地址、子網路遮罩、廣播位址

--預設閘道地址

--DNS伺服器位址


DHCP的工作過程


配置地址租約的四次回話

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/48/08/wKioL1QFYTWDJ3RSAAFUy1MCNzU373.jpg" title="DHCP工作過程.jpg" alt="wKioL1QFYTWDJ3RSAAFUy1MCNzU373.jpg" />

dhcp軟體包的主要檔案

主設定檔:/etc/dhcpd.conf

執行程式:/usr/sbin/dhcpd

服務指令碼:/etc/init.d/dhcpd

執行van書配置:/etc/sysconfig/dhcpd


實驗拓撲:

                         RHEL5.9_A  DHCP Server

-------Server1(VM1)---------(VM1) RHEL5.9_B  Client

      192.168.8.254                          Win7  Client


實驗要求:


將一台Linux 主機配置為DHCP 伺服器,以便為區域網路內的辦公用機提供自動分配IP地址的服務,以提高網路管理和維護的效率。需要滿足的基本要求如下所述。


1.為192.168.8.0/24網段的客戶機自動設定網路參數。


用來給客戶機自動分配的IP位址範圍是:192.168.8.50-192.168.8.100、192.168.8.120-192.168.8.200。

客戶機的預設閘道地址設為192.168.8.254。

客戶機所使用的DNS伺服器設為192.168.8.254、202.96.209.133,預設搜尋域尾碼為pengpeng.com。

將預設租約時間設為8小時,最大租約時間24小時


2.為列印伺服器分配保留地址(Win7)

這台印表機每次開啟電源後獲得的IP地址都應該是192.168.8.8。


3.驗證DHCP伺服器的IP分配情況、客戶機的租約資訊



一、首先安裝dhcp


1、配置YUM

[email protected] ~]# cd /etc/yum.repos.d/

[[email protected] yum.repos.d]# cp rhel-debuginfo.repo rhel5.9.repo

[[email protected] yum.repos.d]# vim rhel5.9.repo 

[[email protected] yum.repos.d]# yum clean all

Loaded plugins: product-id, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

Cleaning up Everything

[[email protected] yum.repos.d]# yum list | wc -l

3347


2、安裝dhcp

[[email protected] yum.repos.d]# yum -y install dhcp

檢查已安裝

[[email protected] yum.repos.d]# rpm -q dhcp

dhcp-3.0.5-31.el5_8.1


二、修改配置

[[email protected] ~]# vim /etc/dhcpd.conf

開啟配置我們只能看到兩行注釋,其他什麼也沒有:

...

# DHCP Server Configuration file.

#   see /usr/share/doc/dhcp*/dhcpd.conf.sample  

...

由第二行注釋我們可以看出,我們需要通過dhcpd.conf.sample模板,直接來修改

執行末行模式

:r /usr/share/doc/dhcp*/dhcpd.conf.sample 

將沒有用的備忘資訊刪掉:


ddns-update-style interim;                                //全域配置參數,一定要保留

subnet 192.168.8.0 netmask 255.255.255.0 {                //網段聲明

        option routers                  192.168.8.254;    //配置參數

        option subnet-mask              255.255.255.0;

        option domain-name              "pengpeng.com";         

        option domain-name-servers      192.168.8.254,202.96.209.133;

        range dynamic-bootp 192.168.8.50 192.168.8.100;

        range dynamic-bootp 192.168.8.120 192.168.8.200;

        default-lease-time 28800;

        max-lease-time 86400;

        host win7 {                                        //主機聲明 (保留IP地址給win7)

                hardware ethernet 00:0C:29:38:7A:91;

                fixed-address 192.168.8.8;

        }

}


三、啟動服務

啟動服務之前,先檢查一下配置有無語法錯誤

[[email protected] ~]# service dhcpd configtest

Syntax: OK

 文法沒錯,但是還是不能啟動服務,只有一種可能了,server沒配IP地址

[[email protected] ~]# service dhcpd restart

啟動 dhcpd:                                               [失敗]

檢查一下IP地址,果然沒有配置

[[email protected] ~]# ifconfig eth0 | grep "inet addr:"

[[email protected] ~]# 

配置靜態IP地址:

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)

DEVICE=eth0

BOOTPROTO=static

HWADDR=00:0C:29:F4:AF:6E

ONBOOT=yes

IPADDR=192.168.8.5

NETMASK=255.255.255.0

重啟network服務,再次檢查IP地址

[[email protected] ~]# service network restart

正在關閉介面 eth0:                                        [確定]

關閉環回介面:                                             [確定]

彈出環回介面:                                             [確定]

彈出介面 eth0:                                            [確定]

[[email protected] ~]# ifconfig eth0 | grep "inet addr:"

          inet addr:192.168.8.5  Bcast:192.168.8.255  Mask:255.255.255.0

此時,再開啟dhcp服務

[[email protected] ~]# service dhcpd restart

啟動 dhcpd:                                               [確定]

[[email protected] ~]# service dhcpd restart

關閉 dhcpd:                                               [確定]

啟動 dhcpd:                                               [確定]

正常監聽UDP67連接埠:

[[email protected] ~]# netstat -anpu | grep :67

udp        0      0 0.0.0.0:67                  0.0.0.0:*                               26404/dhcpd 

確保dhcp服務開機啟動

[[email protected] ~]# chkconfig dhcpd on

[[email protected] ~]# 


四、用戶端測試:

因為整個實驗都在虛擬機器中進行,為了避免實驗幹擾,需要將虛擬機器網路VMnet1 dhcp功能關閉

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/48/08/wKioL1QFYWyivfS2AAJgGuqwnqA518.jpg" title="虛擬機器網路DHCP關閉.jpg" alt="wKioL1QFYWyivfS2AAJgGuqwnqA518.jpg" />

linux:


將用戶端網卡設定為dhcp自動擷取IP地址

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)

DEVICE=eth0

BOOTPROTO=dhcp

ONBOOT=yes

HWADDR=00:0c:29:45:a3:22

重啟網路服務

[[email protected] ~]# service network restart

正在關閉介面 eth0:                                        [確定]

關閉環回介面:                                             [確定]

彈出環回介面:                                             [確定]

彈出介面 eth0: 

正在決定 eth0 的 IP 資訊...完成。

                                                           [確定]

檢查ip地址擷取情況

[[email protected] ~]# ifconfig eth0 | grep "inet addr"

          inet addr:192.168.8.200  Bcast:192.168.8.255  Mask:255.255.255.0

檢查DNS擷取情況


[[email protected] ~]# vim /etc/resolv.conf

; generated by /sbin/dhclient-script

search pengpeng.com

nameserver 192.168.8.254

nameserver 202.96.209.133


windows:

按照我們在設定檔中的定義,該win7系統擷取到的IP地址應該是保留的192.168.8.8

才算達到我們的預期:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/48/06/wKiom1QFYVfCTWp_AAG1KsJQ0vk155.jpg" title="win7自動擷取地址.jpg" alt="wKiom1QFYVfCTWp_AAG1KsJQ0vk155.jpg" />

完全符合預期。

五、檢查服務端和用戶端租約資訊

服務端:

[[email protected] ~]# grep -vE "^#|^$" /var/lib/dhcpd/dhcpd.leases

lease 192.168.8.200 {

  starts 2 2014/09/02 05:43:12;

  ends 2 2014/09/02 13:43:12;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:45:a3:22;

}

用戶端(linux):

[[email protected] ~]# grep -vE "^#|^$" /var/lib/dhclient/dhclient-eth0.leases 

lease {

  interface "eth0";

  fixed-address 192.168.15.132;

  option subnet-mask 255.255.255.0;

  option dhcp-lease-time 1800;

  option routers 192.168.15.2;

  option dhcp-message-type 5;

  option dhcp-server-identifier 192.168.15.254;

  option domain-name-servers 192.168.15.2;

  option broadcast-address 192.168.15.255;

  option domain-name "localdomain";

  renew 3 2014/8/27 03:34:45;

  rebind 3 2014/8/27 03:49:38;

  expire 3 2014/8/27 03:53:23;

}

lease {

  interface "eth0";

  fixed-address 192.168.8.200;

  option subnet-mask 255.255.255.0;

  option routers 192.168.8.254;

  option dhcp-lease-time 28800;

  option dhcp-message-type 5;

  option domain-name-servers 192.168.8.254,202.96.209.133;

  option dhcp-server-identifier 192.168.8.5;

  option domain-name "pengpeng.com";

  renew 2 2014/9/2 09:28:19;

  rebind 2 2014/9/2 12:43:12;

  expire 2 2014/9/2 13:43:12;

}


本人初學linux,以上為學習中所做的 搭建dhcp服務 的實驗,請各位大師指正,謝謝。

本文出自 “學無止境 Crazy Linux” 部落格,請務必保留此出處http://dpzcf.blog.51cto.com/4604404/1547845

Linux DHCP搭建與維護

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.