DHCP-实验实施过程


实验案例:构建DHCP服务器

需求描述

  • DHCP 服务器的主机名为 dhcpsvr.kgc.com,IP 地址为 192.168.4.11/24.
  • 用于给局域网内各主机自动分配的IP地址范围为192.168.4.20,192.168.4.200.
  • 局域网内各主机使用的默认地址为192.168.4.1.
  • 局域网内各主机使用的DNS服务器地址分别为192.168.4.2和192.168.4.3,DNS 服务器的主机名分别为ns1.kgc.com和ns2.kgc.com。
  • 网络打印机设备(可在网络内另找一台Windows主机,执行“ipconfig /all”命令查知其MAC地址)的主机名称为prtsvr,要求自动分配固定IP地址为192.168.4.9

推荐步骤


确认服务器的网络地址设置正确

(1)修改网卡eth0的配置文件ifcfg-eth0,将IP地址设为192.168.4.11/24,默认网关地址设为192.168.4.1

[root@test1 Desktop]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 
UUID=2a9f7302-e5a6-421a-bdc9-d386911222df
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=192.168.4.11
NETMASK=255.255.255.0
GATEWAY=192.168.4.1
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                                                                
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
-- INSERT --

(2)修改主机名配置文件network,将主机名设为dhcpsvr.kgc.com.

[root@test1 Desktop]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=dhcpsvr.kgc.com
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                                       
~                                                                                                                                                                            
Type  :quit  to exit Vim

重启

[root@dhcpsvr Desktop]# hostname
dhcpsvr.kgc.com
[root@dhcpsvr Desktop]#

(3)修改本地主机映射文件hosts,添加dhcpsvr,ns1,ns2,prtsvr这四台主机的IP地址映射记录,以提高主机查找效率.

[root@dhcpsvr Desktop]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.11 dhcpsvr.kgc.com
192.168.4.2 ns1.kgc.com
192.168.4.3 ns2.kgc.com
192.168.4.9 prtsvr

~                                                                                                              
~                                                                                                                 
~                                                                                                                 
:wq

(4)修改域名解析配置文件resolv.conf,添加两行配置记录,指定两台DNS服务器的 IP 地址分别为 192.168.4.2 和 192.168.4.3。

[root@dhcpsvr Desktop]# vi /etc/resolv.conf 
# Generated by NetworkManager
search kgc.com
nameserver 192.168.4.2
nameserver 192.168.4.3

# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
~                                                                                                                 
~                                                                                                             
~                                                                                                                 
~                                                                                                                 
~                                                                                                                 
~                                                                                                                 
:wq

(5)重新启动主机,以使修改的各种地址配置生效

安装并配置DHCP服务

(1)挂载 RHEL6 光盘,安装 dhcp-4.1.1-38.P1.el6.x86_64.rpm 软件包

[root@dhcpsvr Desktop]# rpm -qa | grep dhcp
dhcp-4.1.1-38.P1.el6.centos.x86_64
dhcp-common-4.1.1-38.P1.el6.centos.x86_64
[root@dhcpsvr Desktop]#

(2)编辑dhcpd.conf配置文件,根据题意要求添加 subnet声明,“option routers”与“option domain-name-servers”配置选项,range配置参数,添加host声明为prtsvr綁定固定IP地址,适当调整全局配置。

[root@Daring Desktop]# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=prtsvr
~                                                                                                                                                 
~                                                                                                                                                                                         
~                                                                                                                                                 
~                                                                                                                                                 
:wq
[root@Daring Desktop]# reboot
[root@dhcpsvr dhcp]# pwd
/etc/dhcp
[root@dhcpsvr dhcp]# vi dhcpd.conf 

      1 # dhcpd.conf
      2 #
      3 # Sample configuration file for ISC dhcpd
      4 #
      5 
      6 
      7 default-lease-time 21600;
      8 max-lease-time 43200;
      9 option domain-name "kgc.com";
     10 option domain-name-servers 192.168.4.2, 192.168.4.3;
     11 
     12 subnet 192.168.4.0 netmask 255.255.255.0 {
     13     range    192.168.4.20 192.168.4.200;
     14     option routers     192.168.4.1;
     15 }
     16 
     17 hot prtsvr {
     18     hardware ehternet 00:0C:29:3B:AF:22;
     19     fixed-address 192.168.4.9;
     20 }
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                                                                                        
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
:wq

(3)启动dhcpd服务,确认服务监听正常。

[root@dhcpsvr dhcp]# service dhcpd start
Starting dhcpd:                                            [FAILED]
[root@dhcpsvr dhcp]# ls
dhclient.d  dhcpd6.conf  dhcpd.conf
[root@dhcpsvr dhcp]# vi dhcpd.conf

# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#


default-lease-time 21600;
max-lease-time 43200;
option domain-name "kgc.com";
option domain-name-servers 192.168.4.2, 192.168.4.3;

subnet 192.168.4.0 netmask 255.255.255.0 {
    range    192.168.4.20 192.168.4.200;
    option routers     192.168.4.1;
}

host prtsvr {
    hardware ehternet 00:0C:29:3B:AF:22;
    fixed-address 192.168.4.9;
}
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
:wq
[root@dhcpsvr dhcp]# service dhcpd start
Starting dhcpd:                                            [FAILED]
[root@dhcpsvr dhcp]# vi dhcpd.conf 

# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#


default-lease-time 21600;
max-lease-time 43200;
option domain-name "kgc.com";
option domain-name-servers 192.168.4.2, 192.168.4.3;

subnet 192.168.4.0 netmask 255.255.255.0 {
    range    192.168.4.20 192.168.4.200;
    option routers     192.168.4.1;


    host prtsvr {
        hardware ehternet 00:0C:29:3B:AF:22;
        fixed-address 192.168.4.9;
    }
}
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                            
~                                                                                                                                                                        
~                                                                                                                                                                            
~                                                                                                                                                                            
:wq
[root@dhcpsvr dhcp]# vi dhcpd.conf 
[root@dhcpsvr dhcp]# cat dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name kgc.com;
option domain-name-servers 192.168.4.2, 192.168.4.3;

default-lease-time 600;
max-lease-time 7200;

# Use this to enble / disable dynamic dns updates globally.
ddns-update-style none;


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


# This is a very basic subnet declaration.

subnet 192.168.4.0 netmask 255.255.255.0 {
  range 192.168.4.20 192.168.4.200;
  option routers 192.168.4.1;
}

host prtsvr {
  hardware ethernet 00:0C:29:3B:AF;22;
  fixed-address 192.168.4.9;
}

[root@dhcpsvr dhcp]# service dhcpd start
Starting dhcpd:                                            [FAILED]
[root@dhcpsvr dhcp]#
找不到原因,用CentOS 7mini尝试

内核版本

[root@dhcpsvr home]# uname -r
3.10.0-327.el7.x86_64

查看hostname

[root@dhcpsvr home]# cat /etc/sysconfig/network
# Created by anaconda
HOSTNAME=dhcpsvr.kgc.com

查看hosts

[root@dhcpsvr home]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.11 dhcpsvr.kgc.com
192.168.4.2 ns1.kgc.com
192.168.4.3 ns2.kgc.com
192.168.4.9 prtsvr

查看网卡配置文件

[root@dhcpsvr home]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736 
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=5619b5f1-8c7b-4d53-b78f-f4160783dcf0
DEVICE=eno16777736
ONBOOT=yes
IPADDR=192.168.4.11
NETMASK=255.255.255.0
GATEWAY=192.168.4.1

查看dns配置

[root@dhcpsvr home]# cat /etc/resolv.conf 
# Generated by NetworkManager
search kgc.com


# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com

重新设置dns

[root@dhcpsvr home]# vi /etc/resolv.conf
[root@dhcpsvr home]# cat /etc/resolv.conf 
# Generated by NetworkManager
search kgc.com
nameserver 192.168.4.2
nameserver 192.168.4.3

# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
[root@dhcpsvr home]#

查看/etc/dhcp/dhcpd.conf



>>测试DHCP自动获取地址

(1)在客户机中,将网卡设置为自动获取地址,验证是否成功。

```bash
[root@prtsvr Desktop]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
HWADDR=00:0C:29:3B:AF:22
TYPE=Ethernet
UUID=e8defc61-064f-44a5-8772-b1bf4cc3cec8
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=dhcp
IPADDR=192.168.0.103
GATEWAY=192.168.4.1
~                                                                                                                                                 
~                                                                                                                                                                               
~                                                                                                                                                 
~                                                                                                                                                 
:wq

(2)在网络打印机prtsvr中,验证是否能够成功获取保留的IP地址192.168.4.9

[root@prtsvr Desktop]# service network restart
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  
Determining IP information for eth0... done.
                                                           [  OK  ]
[root@prtsvr Desktop]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:3B:AF:22  
          inet addr:192.168.4.9  Bcast:192.168.4.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe3b:af22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:5679 (5.5 KiB)  TX bytes:2022 (1.9 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:164 errors:0 dropped:0 overruns:0 frame:0
          TX packets:164 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:11368 (11.1 KiB)  TX bytes:11368 (11.1 KiB)

[root@prtsvr Desktop]#

网络打印机当中能够获取到指定的IP地址。

TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=18a130f9-c170-4d78-a03a-57761761a369
DEVICE=eno16777736
ONBOOT=yes
IPADDR=192.168.0.107

~                                                                                                
~                                                                                                
~                                                                                                
~                                                                                                
~                                                                                                
~                                                                                                
:wq

results matching ""

    No results matching ""