1. 程式人生 > >DHCP實驗

DHCP實驗

setup1 : 配置IP地址等相關的網路資訊,並保證網路的連通性(關閉防火牆和SELINUX安全機制)

//臨時關閉防火牆

(7)systemctl stop firewalld.service

(6) service iptables stop

老版本的:CentOS5、CentOS6

service xxxx [ stop | start | status | reload | restart ]

/etc/init.d/xxxx [ stop | start | status | reload | restart ]

新版本的:CentOS7

systemctl [ stop | start | status | reload | restart ] xxxx.service

//永久關閉防火牆

(7)systemctl disable firewalld.service

systemctl [ enable | disable ] firewalld.service

(6) chkconfig iptables off

//臨時關閉selinux安全機制

setenforce 0

//永久關閉防selinux安全機制

sed -i '7 s/enforcing/disabled/' /etc/selinux/config

註釋:

本地虛擬網絡卡:網段

虛擬機器:自定義,VMnet***

編輯-虛擬網路編輯器:新增新的VMnet,並取消掉自帶的DHCP服務

setup2: 搭建本地YUM倉庫,安裝實驗所需軟體包

mkdir /media/cdrom

umount /dev/sr0

mount /dev/sr0 /media/cdrom

cd /etc/yum.r*

mkdir a/

mv C* a/

echo -e "[abc]\nname=abc\nbaseurl=file:///media/cdrom/\nenabled=1\ngpgcheck=0" > local.repo

yum -y clean all

yum makecache

rpm -q dhcp

yum -y install dhcp

setup3: 根據需求修改配置檔案

cat /etc/dhcp/dhcpd.conf

cat /usr/share/doc/dhcp*/dhcpd.conf.example > /etc/dhcp/dhcpd.conf  

vi /etc/dhcp/dhcpd.conf

//指定分給客戶端的首選和備選DNS地址,中間用“,”隔開,最後用“;”結尾。

option domain-name-servers 8.8.8.8 , 202.106.0.20;

//預設租賃時間,客戶端到達這個時間會向DHCP伺服器發起續租請求。

default-lease-time 21600;

//最大租賃時間,它是預設租賃時間的2倍,單位都是:秒鐘。

max-lease-time 43200;

//dhcp伺服器日誌

log-facility local7;

 

//子網宣告: 192.168.100.0 (此處必須寫為網段號,寫成IP地址的必錯)

subnet 192.168.100.0 netmask 255.255.255.0 {

//DHCP伺服器給客戶端設定的地址池,中間用空格隔開,千萬不要加“,”;

range 192.168.100.2 192.168.100.200;

//指定給客戶端分的預設閘道器

option routers 192.168.100.1;

}

setup4: 啟動應用服務並測試

systemctl start dhcpd.service

臨時開啟DHCP服務

systemctl enable dhcpd.service

永久開啟DHCP服務

開啟另外同一個網段的客戶機,進行測試

vi /etc/sysconfig/network-scripts/ifcfg-eth0

BOOTPROT=dhcp

刪掉IPADDR、NETMASK、GATEWAY

重啟服務

systemctl restart network

service network restart

dhclient -d eth0

//重新獲取IP地址

dhclient -r eth0

//釋放IP地址

dhclient -d eth0 Ctrl+C退出

//重新獲取IP地址

(ip a; route -n ; cat /etc/resolv.sonf)

補充配置,給一個客戶機保留分配IP地址。

[[email protected] dhcp]# vi dhcpd.conf

[[email protected] dhcp]# cat dhcpd.conf

option domain-name-servers 8.8.8.8,202.106.0.20;

//預設和最大租約時間

default-lease-time 21600;

max-lease-time 43200;

//日誌

log-facility local7;     

subnet 192.168.100.0 netmask 255.255.255.0 {

range 192.168.100.2 192.168.100.200;

option routers 192.168.100.1;

}

//指定保留IP地址的主機

host PC1 {

//指定保留IP地址主機的MAC地址

hardware ethernet 00:0C:29:BD:4F:B4;

//指定保留IP地址主機分配的IP地址

fixed-address 192.168.100.88;

}

systemctl restart dhcpd.service

Windows PC1下:

ipconfig/release 釋放ip

ipconfig/renew 獲取IP

setup5: 故障排查與總結

常見出錯請檢查配置檔案

引數簡介:

dhcp

-d

Force dhclient to run as a foreground process. This is useful when running the client under a debugger, or when running it out of inittab on System V systems.

-r

Tell dhclient to release the current lease it has from the server. This is not required by the DHCP protocol, but some ISPs require their clients to notify the server if they wish to release an assigned IP address.

 

不懂的地方請留言,看到後會及時回覆