1. 程式人生 > >DHCP服務搭建

DHCP服務搭建

domain 火墻 固定ip 3.5 ram log lena ber 原因

DHCP實驗

實驗規劃及環境準備
1、DHCP服務器應該配置成一個靜態IP(最好使用僅主機模式做實驗)
2、VMware內置DHCP服務器,應禁止僅主機模式VMnet1的DHCP服務
3、關閉selinux和防火墻
實驗器材準備
2臺虛擬機(CentOS7、CentOS7C)。7作為客戶機(IP為192.168.137.200),7C作為客戶機
.

1、為CentOS7主機添加一塊僅主機網卡,並配置成靜態IP=192.168.137.200(這個主機還有一塊橋接網卡)

bash# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
UUID="af78583d-e2d9-42ba-83f1-42659c869aed"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.137.200"
NETMASK="255.255.255.0"

.
2、將CentOS7C主機添加一塊僅主機網卡,並配置成通過dhcp服務獲得IP地址

bash# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="dhcp"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"

.
3、重啟CentOS7C之後可以發現,主機CentOS7C的網卡上是沒有IP地址的

bash# ifconfig 

.
4、開始在CentOS7主機上配置DHCP服務
①、在CentOS7上安裝dhcp包,並嘗試啟動dhcp服務

bash# yum install -y dhcp
bash# systemctl start dhcpd    //發現啟動不了,原因是主配置文件無內容

②、查看主配置文件之後,發現是空的,文件中提示有配置模板文件/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example,復制該文件至/etc/dhcp/dhcpd.conf,添加子網和地址池

bash# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
bash# vim /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";        //從DHCP服務器獲取的域後綴,/etc/resolv.conf中的search,用於補全後綴
option domain-name-servers 114.114.114.114,1.1.1.1    //指明DNS服務器。放在此處所有子網有效

default-lease-time 600; //默認租期600s。租期未到,即使機器關機,IP地址也會被占用,租期過長,主機過多,新主機可能拿不到地址
max-lease-time 7200;    //最長租期,客戶端可以向服務器申請租期時間,這個是客戶端可以申請的最大租期時間

log-facility local7;

subnet 192.168.137.0 netmask 255.255.255.0 {     //subnet子網聲明,說明將來要為哪個子網分配地址
  range 192.168.137.100 192.168.137.199;         //地址池為192.168.137.100到192.168.137.199 
  option routers 192.168.137.1;                   //為客戶端分配網關
  option domain-name-servers 114.114.114.114,1.1.1.1    //指明DNS服務器,子網中定義的優先級更高
  next-server 192.168.137.199    //指定tftp服務器,DHCP客戶機可以向此主機獲取引導文件
  filename "pxelinux.0";    //tftp服務器上引導文件的名字,next-server和filename用於實現自動安裝
}

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;
}

subnet 10.254.239.32 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;
}

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;
}

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;
}

host passacaglia {
  hardware ethernet 0:0:c0:5d:bd:95;
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}

# 以下操作可以做MAC綁定固定IP
host fantasia {
  #hardware ethernet 00:0c:29:f4:0a:fd;    //客戶機網卡的MAC地址,可以做靜態地址綁定
  #fixed-address 192.168.30.100;    //為此MAC地址分配的固定IP,也可以在此處配置網關DNS等信息
  #option routers 192.168.137.1    //為客戶機指定網關
  #option domain-name-servers 223.5.5.5;  //為客戶機指定阿裏的DNS服務器
}

class "foo" {
  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}

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;
  }
  pool {
    allow members of "foo";
    range 10.17.224.10 10.17.224.250;
  }
  pool {
    deny members of "foo";
    range 10.0.29.10 10.0.29.230;
  }
}

③、CentOS7啟動dhcpd服務

bash# systemctl start dhcpd.service

.
5、查看CentOS7C是否獲得IP地址

bash# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.137.129  netmask 255.255.255.0  broadcast 192.168.137.255
        inet6 fe80::777d:b963:b4c7:8408  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:f4:0a:fd  txqueuelen 1000  (Ethernet)
        RX packets 726  bytes 191927 (187.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 374  bytes 67729 (66.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

.
6、查看CentOS7中的租約信息文件/var/lib/dhcpd/dhcpd.leases

bash# vim /var/lib/dhcpd/dhcpd.leases
lease 192.168.137.129 {
  starts 5 2018/10/05 14:55:30;    //租約開始時間,UTC時間,比北京時間早8個小時
  ends 5 2018/10/05 15:05:30;      //租約結束時間,和開始時間正好差600s
  cltt 5 2018/10/05 14:55:30;      
  binding state active;            
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:f4:0a:fd;
  client-hostname "VCentOS75";
}

.
7、查看客戶端租約文件

bash# dhclient -d
bash# vim /var/lib/dhclient/dhclient.leases
lease {
  interface "eth0";
  fixed-address 192.168.137.129;   //確實獲得了地址池中的地址
  option subnet-mask 255.255.255.0;
  option routers 192.168.137.1;
  option dhcp-lease-time 600;
  option dhcp-message-type 5;
  option domain-name-servers 114.114.114.114,1.1.1.1;
  option dhcp-server-identifier 192.168.137.200;  //從DHCP服務器192.168.137.200上獲取的IP地址
  option domain-name "example.org";
  renew 5 2018/10/05 15:14:47;
  rebind 5 2018/10/05 15:19:45;
  expire 5 2018/10/05 15:21:00;
}

DHCP服務搭建