標籤:爛泥 centos 安裝 配置 dhcp
本文首發於爛泥行天下。
有關DHCP伺服器的配置一直打算學習,這幾天終於抽出時間來專門學習這個知識點。
DHCP:動態主機設定通訊協定,在此就不多做介紹。不清楚的童鞋,可以去百度下。
本次實驗OS:centos6.5 64bit
雙網卡:eth0為外網網卡,eth1為內網網卡
eth0網卡外網可以正常串連互連網,如下:
650) this.width=650;" title="clip_image001" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image001" src="http://img1.51cto.com/attachment/201408/26/526870_140901917844AE.png" width="467" height="264" />
要想配置DHCP服務,我們要先來安裝DHCP軟體包,如下:
yum -y install dhcp
650) this.width=650;" title="clip_image002" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image002" src="http://img1.51cto.com/attachment/201408/26/526870_1409019178sCDF.png" width="666" height="216" />
650) this.width=650;" title="clip_image003" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image003" src="http://img1.51cto.com/attachment/201408/26/526870_14090191799PcK.png" width="521" height="256" />
檢查安裝DHCP軟體包,所產生的檔案。如下:
rpm -ql |dhcp
650) this.width=650;" title="clip_image004" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image004" src="http://img1.51cto.com/attachment/201408/26/526870_1409019179Z5Rx.png" width="431" height="490" />
通過我們可以看到有DHCP服務的設定檔以及DHCP服務的開機檔案。
通過我們可以知道,DHCP的設定檔為/etc/dhcp/dhcpd.conf。但該設定檔內容預設是空的,我們可以從dhcp安裝目錄下複製一個到/etc/dhcp/下。如下:
vi /etc/dhcp/dhcpd.conf
650) this.width=650;" title="clip_image005" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image005" src="http://img1.51cto.com/attachment/201408/26/526870_1409019180uRM7.png" width="366" height="133" />
cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
650) this.width=650;" title="clip_image006" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image006" src="http://img1.51cto.com/attachment/201408/26/526870_14090191807QNX.png" width="641" height="119" />
650) this.width=650;" title="clip_image007" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image007" src="http://img1.51cto.com/attachment/201408/26/526870_1409019180ERIP.png" width="525" height="483" />
dhcpd.conf檔案有兩大部分,分別是全域配置和局部配置:
1、當全域配置與局部配置發生衝突時,局部配置優先順序更高。
2、設定檔中每一行必須以分號結尾,因為在啟動dhcpd時是不會報錯的,只能通過查看記錄檔/var/log/message得知設定檔是否有問題。
3、局部配置必須包含在一對中括弧之間。
●常用參數:
●ddns-update-style (none|interim|ad-hoc):定義所支援的DNS動態更新類型,該參數必選且必須放在第一行且只能在全域配置中使用。
●default-lease-time:用戶端IP預設租約時間,單位秒,該參數可以在全域配置、局部配置均可使用。
●max-lesase-time:用戶端IP租約時間的最大值,單位秒,該參數可以在全域配置、局部配置均可使用。
●subnet 網路號 netmask 子網路遮罩 {…..}:定義範圍。
●range 起始IP 結束IP:動態IP位址範圍。
●option routes IP地址:預設閘道,該選項可以在全域配置、局部配置均可使用。
●option subnet-mask 子網路遮罩:預設子網路遮罩,該選項可以在全域配置、局部配置均可使用。
●option domain-name-servers:DNS伺服器位址,該選項可以在全域配置、局部配置均可使用。
●option domain-name:DNS尾碼,該選項可以在全域配置、局部配置均可使用。
●host 名稱 {…..} 為特殊機器保留IP地址。
相關的案例說明如下:
ddns-update-style none;
subnet 10.5.5.0 netmask 255.255.255.224 {設定子網聲明
range 10.5.5.26 10.5.5.30;設定DHCP的IP位址集區
option domain-name-servers ns1.internal.example.org;設定DNS伺服器位址
option domain-name "internal.example.org";為用戶端設定DNS尾碼
option routers 10.5.5.1;設定預設網關為192.168.2.1
option broadcast-address 10.5.5.31;設定DHCP的廣播位址
default-lease-time 600;設定用戶端預設的地址租期
max-lease-time 7200;設定用戶端最長的地址租期
}
host ubuntu {為MAC地址00:0c:29:ee:38:80的機器保留IP地址10.5.5.3
hardware ethernet 00:0c:29:ee:38:80;
fixed-address 10.5.5.3;
}
本次實驗的配置如下:
more /etc/dhcp/dhcpd.conf
650) this.width=650;" title="clip_image008" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image008" src="http://img1.51cto.com/attachment/201408/26/526870_1409019181Jogv.png" width="419" height="336" />
option domain-name "ilanni.com";
option domain-name-servers 192.168.1.1,223.5.5.5;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
subnet 10.5.5.0 netmask 255.255.255.0 {
range 10.5.5.2 10.5.5.254;
option domain-name-servers 192.168.1.1;
option routers 10.5.5.1;
default-lease-time 600;
max-lease-time 7200;
}
host ubuntu{
hardware ethernet 00:0c:29:ee:38:80;
fixed-address 10.5.5.3;
}
注意有關範圍的配置,其實模版設定檔中。是有幾個版本的,如下:
650) this.width=650;" title="clip_image009" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image009" src="http://img1.51cto.com/attachment/201408/26/526870_14090191811szg.png" width="494" height="509" />
我們使用的最後一個配置選項,以上設定檔儲存後,我們現在來設定DHCP伺服器兩張網卡的網路設定。
我們來設定eth0外網網卡,具體配置如下:
more /etc/sysconfig/network-scripts/ifcfg-eth0
650) this.width=650;" title="clip_image010" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image010" src="http://img1.51cto.com/attachment/201408/26/526870_1409019181v8dw.png" width="488" height="286" />
eth1內網網卡,具體配置如下:
more /etc/sysconfig/network-scripts/ifcfg-eth1
650) this.width=650;" title="clip_image011" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image011" src="http://img1.51cto.com/attachment/201408/26/526870_1409019182PafL.png" width="508" height="188" />
同時因為本機是多網卡機器,所以我們在此還要配置DHCP監聽的網卡。如下:
vi /etc/sysconfig/dhcpd
650) this.width=650;" title="clip_image012" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image012" src="http://img1.51cto.com/attachment/201408/26/526870_1409019182Aa1A.png" width="409" height="136" />
我們再前邊已經提到過eth1網卡時內網網卡,所以在DHCP監聽的網卡中我們填寫的是eth1.
以上配置完畢後,我們現在來啟動DHCP服務,如下:
650) this.width=650;" title="clip_image013" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image013" src="http://img1.51cto.com/attachment/201408/26/526870_1409019182AQXS.png" width="552" height="131" />
上述設定檔中,我們特別為MAC地址00:0c:29:ee:38:80機器預留10.5.5.3這個IP地址,其他的機器我們沒有做此設定。
我們來先看其他機器,如下:
650) this.width=650;" title="clip_image014" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image014" src="http://img1.51cto.com/attachment/201408/26/526870_1409019183tIur.png" width="793" height="535" />
通過,我們可以看到此機器已經正常擷取IP地址及相關的資訊。
再來查看那台我們做過保留的IP地址的機器,如下:
650) this.width=650;" title="clip_image015" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image015" src="http://img1.51cto.com/attachment/201408/26/526870_1409019184JhJ4.png" width="1123" height="537" />
通過,我們可以看到該機器通過手工設定IP地址的,而且IP地址就是我們預留的。
按道理說,到此我們的DHCP伺服器已經配置完畢。但是我們會發現,已經獲得IP地址的機器是不能上網的,如下:
650) this.width=650;" title="clip_image016" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image016" src="http://img1.51cto.com/attachment/201408/26/526870_1409019185NueK.png" width="581" height="309" />
這個已經和DHCP服務本身沒有關係了,而是需要我們再DHCP所在的伺服器開啟NAT服務。
為了操作方便,我們先暫時關閉DHCP所在伺服器的防火牆,如下:
/etc/init.d/iptables stop
650) this.width=650;" title="clip_image017" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image017" src="http://img1.51cto.com/attachment/201408/26/526870_1409019185qgOc.png" width="668" height="400" />
開啟IP轉寄功能,如下:
650) this.width=650;" title="clip_image018" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image018" src="http://img1.51cto.com/attachment/201408/26/526870_1409019185KQfH.png" width="493" height="295" />
然後在執行sysctl –p命令:
650) this.width=650;" title="clip_image019" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image019" src="http://img1.51cto.com/attachment/201408/26/526870_1409019186rjKv.png" width="467" height="247" />
注意圖中的報錯資訊,這個是因為沒有載入bridge模組導致。現在我們來手工載入該模組,如下:
modprobe bridge
lsmod |grep bridge
650) this.width=650;" title="clip_image020" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image020" src="http://img1.51cto.com/attachment/201408/26/526870_1409019186lS6q.png" width="363" height="131" />
然後再次執行sysctl –p命令,如下:
650) this.width=650;" title="clip_image021" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image021" src="http://img1.51cto.com/attachment/201408/26/526870_1409019186kyqk.png" width="337" height="257" />
以上操作完畢後,再執行如下iptables命令,開啟iptables的NAT網路位址轉譯功能。如下:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
650) this.width=650;" title="clip_image022" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image022" src="http://img1.51cto.com/attachment/201408/26/526870_1409019187BjGE.png" width="526" height="106" />
其中eth0為外網網卡。
通過這條命令就可以開啟iptables的NAT地址轉換的功能,這條命令的意思是當有資料包要轉寄出去時,iptables就會將IP地址轉換為eth0介面的IP地址。上邊這條命令的好處在於不必理會介面的IP地址是什麼,轉換會自動進行。
或者以下命令:
iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE
iptables --append FORWARD --in-interface eth1 -j ACCEPT
現在我們再來看看,用戶端是否可以正常串連互連網。如下:
650) this.width=650;" title="clip_image023" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image023" src="http://img1.51cto.com/attachment/201408/26/526870_1409019187hn89.png" width="722" height="623" />
以上對防火牆的操作是暫時的,下次重啟DHCP伺服器用戶端還是無法正常串連互連網。
我們可以通過/etc/init.d/iptables save,把該規則進行儲存。如下:
/etc/init.d/iptables save
more /etc/sysconfig/iptables
650) this.width=650;" title="clip_image024" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image024" src="http://img1.51cto.com/attachment/201408/26/526870_14090191887xEn.png" width="541" height="223" />
最後我們來查看租約的資料庫檔案/var/lib/dhcpd/dhcpd.leases,如下:
650) this.width=650;" title="clip_image025" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px;" border="0" alt="clip_image025" src="http://img1.51cto.com/attachment/201408/26/526870_1409019188R0kx.png" width="300" height="316" />
到此我們有關DHCP伺服器的相關配置已經全部完成。在這還有一部分知識沒有介紹,是和PXE有關的。這部分,我們會在kickstart無人值守安裝linux中做介紹。
本文出自 “爛泥行天下” 部落格,請務必保留此出處http://ilanni.blog.51cto.com/526870/1545030
爛泥:centos安裝及配置DHCP伺服器