DHCP概述


1)了解DHCP服务

    DHCP(Dynimic Host Configuration Protocol,动态主机配置协议)是由IEIF(Internet工作任务小组)设计开发的,专门用于为TCP/IP网络的计算机自动分配TCP/IP参数的协议,DHCP服务避免了因手工设置IP地址所产生的错误,同时也避免了把一个IP地址分配给多台工作站所造成的地址冲突;DHCP提供了安全,可靠且简单的TCP/IP网络设置,降低了配置IP地址的负担


wKiom1gYQ5OR60X3AAB83RgCjEI423.png-wh_50


2)使用DHCP的好处

    减少管理员的工作量

    避免输入错误的可能

    避免IP冲突

    当网更改IP地址段时,不需要在重新配置每个用户的IP地址

    提高了IP地址的利用率

    方便客户端的配置


DHCP的工作原理

1)DHCP的分配方式

    DHCP服务器提供了三种IP分配方式:自动分配、手动分配和动态分配

    

    自动分配是当DHCP客户机第一次成功的从DHCP服务器获取到一个IP地址后,就永久地使用这个IP地址

    手动分配是由DHCP服务器管理员专门指定IP地址

    动态分配时当DHCP客户机第一次从DHCP服务器获取到IP地址后,并非永久地使用该地址,每次使用完后,DHCP客户机就需要释放这个IP地址,供其他客户机使用


2)DHCP的租约过程

    客户机从DHCP服务器获得IP地址的过程称为DHCP的租约过程;租约过程分为四个步骤:

    

        客户机请求IP地址(客户机发DHCP Discover广播包)

        服务器响应(服务器发DHCP Offer广播包)

        客户机选择IP地址(客户机发DHCP Request广播包)

        服务器确定租约(服务器发DHCP ACK广播包)

 

wKioL1gYRBnSoUa-AACOSPyw4Io933.png-wh_50 

 

1、客户端请求IP

DHCP Discover

wKioL1gYRCjzuZrfAADjyvzoI2s843.png-wh_50 


2、服务器响应

DHCP Offer

 

wKiom1gYRDfyLj-TAADke9n0DKw364.png-wh_50 

 

3、客户机选择IP

DHCP Request

 

wKiom1gYREPQVS3UAADhiXKDuEI938.png-wh_50 

4、服务器确定租约

DHCP ACK

 

wKioL1gYRFfCMEdyAADnxN7gb1I777.png-wh_50 

 

5、重新接入网络

    wKiom1gYRGLjEP8AAAMZ8CWM5z8792.png-wh_50 

 

    wKiom1gYRIXDi-JNAAMvjrsc1Ak229.png-wh_50 

 

dhcp软件安装

dhcp软件在系统镜像光盘里就已经提供了,只需要使用rpm或者yum软件安装即可

 

如果使用yum安装需要提前配置好yum

[root@localhost ~]# mkdir /mnt/cdrom
[root@localhost ~]# mount /dev/cdrom /mnt/cdr
[root@localhost ~]# cat /etc/yum.repos.d/base.repo 
[base]
name=centos
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever
 
 
[root@localhost ~]# rpm -ivh /mnt/cdrom/Packages/dhcp-4.2.5-42.el7.centos.x86_64.rpm
或者
[root@localhost ~]# yum install -y dhcp
 
使用下面的命令可以查看dhcp软件的介绍和安装生成文件及目录
[root@localhost ~]# yum info dhcp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name        : dhcp
Arch        : x86_64
Epoch       : 12
Version     : 4.2.5
Release     : 42.el7.centos
Size        : 1.4 M
Repo        : installed
Summary     : Dynamic host configuration protocol software
URL         : http://isc.org/products/DHCP/
License     : ISC
Description : DHCP (Dynamic Host Configuration Protocol) is a protocol which allows
            : individual devices on an IP network to get their own network
            : configuration information (IP address, subnetmask, broadcast address,
            : etc.) from a DHCP server. The overall purpose of DHCP is to make it
            : easier to administer a large network.
            : 
            : To use DHCP on your network, install a DHCP service (or relay agent),
            : and on clients run a DHCP client daemon.  The dhcp package provides
            : the ISC DHCP service and relay agent.
 
[root@localhost ~]# rpm -ql dhcp
/etc/NetworkManager
/etc/NetworkManager/dispatcher.d
/etc/NetworkManager/dispatcher.d/12-dhcpd
/etc/dhcp
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/usr/bin/omshell
/usr/lib/systemd/system/dhcpd.service
/usr/lib/systemd/system/dhcpd6.service
/usr/lib/systemd/system/dhcrelay.service
/usr/sbin/dhcpd
/usr/sbin/dhcrelay
/usr/share/doc/dhcp-4.2.5
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
/usr/share/doc/dhcp-4.2.5/dhcpd6.conf.example
/usr/share/doc/dhcp-4.2.5/ldap
/usr/share/doc/dhcp-4.2.5/ldap/README.ldap
/usr/share/doc/dhcp-4.2.5/ldap/dhcp.schema
/usr/share/doc/dhcp-4.2.5/ldap/dhcpd-conf-to-ldap
/usr/share/man/man1/omshell.1.gz
/usr/share/man/man5/dhcpd.conf.5.gz
/usr/share/man/man5/dhcpd.leases.5.gz
/usr/share/man/man8/dhcpd.8.gz
/usr/share/man/man8/dhcrelay.8.gz
/usr/share/systemtap/tapset/dhcpd.stp
/var/lib/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases


 

 

dhcp基本配置

wKiom1gYRNfwHjjuAAB2Zef_sLQ702.png-wh_50 

 

前提条件:

DHCP服务器与客户端主机已正常连通,并在同一网络中

 

如果客户端主机使用了DHCP方式动态获取IP地址,而网络中没有DHCP服务器,或者服务器出现问题,则客户端就不能正常取得地址了;如果是Windows,它将从B类网段192.254.0.0中挑选一个IP地址作为自己的IP地址,并继续每隔5分钟尝试与DHCP服务器进行通信,一旦与DHCP取得通信,客户机将放弃自己配置的IP地址,使用DHCP服务器分配的IP地址;

    wKiom1gYROPzwFJ9AAAl9BNWVp4182.png-wh_50 

 

1、DHCP服务器配置

DHCP服务器需要向客户端分配IP地址,服务器自身需要使用一个静态的IP地址

 

    [root@localhost ~]# ifconfig eno16777736

    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

            inet 172.16.1.10  netmask 255.255.0.0  broadcast 172.16.255.255

            inet6 fe80::20c:29ff:fe65:8475  prefixlen 64  scopeid 0x20<link>

            ether 00:0c:29:65:84:75  txqueuelen 1000  (Ethernet)

            RX packets 7051  bytes 6763047 (6.4 MiB)

            RX errors 0  dropped 0  overruns 0  frame 0

            TX packets 1808  bytes 225665 (220.3 KiB)

            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

     

     

    [root@localhost ~]# vi /etc/dhcp/dhcpd.conf

    ...

    option domain-name "bsg.com";

    option domain-name-servers 202.106.0.20, 8.8.8.8,172.16.1.254;

     

    default-lease-time 3600;

    max-lease-time 7200;

    ...

    subnet 172.16.1.0 netmask 255.255.0.0 {

            range 172.16.1.100 172.16.1.200

    }

    ...

     

    [root@localhost ~]# systemctl start dhcpd.service

     

    [root@localhost ~]# ss -tunl

    Netid  State      Recv-Q Send-Q    Local Address:Port       Peer Address:Port              

    udp    UNCONN     0      0                     *:40620                             *:*                  

    udp    UNCONN     0      0                     *:67                                *:*                  

    udp    UNCONN     0      0                    :::18696                            :::*                  

    tcp    LISTEN     0      128                   *:22                                *:*                  

    tcp    LISTEN     0      100           127.0.0.1:25                                *:*                  

    tcp    LISTEN     0      128                  :::22                               :::*                  

    tcp    LISTEN     0      100                 ::1:25                               :::*   

 

 

2、客户端主机配置

如果客户端之前使用静态的IP地址,需要将获取方式改为dhcp自动获取并删除IP地址,修改完配置文件后重新启动网卡

 

Linux客户端

wKiom1gYRU6C3GAJAAAo0glc6oQ572.png-wh_50 

 

wKioL1gYRVqSdgp2AAAUgbyWXhE147.png-wh_50 

 

wKioL1gYRWWiBCRdAAArGpbGheI350.png-wh_50 

 

wKioL1gYRZXA0uqaAAAhqfwGv9k308.png-wh_50 

 

 

Windows客户端

 

wKioL1gYRd7jHk2pAAEYrtf6Hkg629.png-wh_50  

 

 

在DHCP服务器上查看IP地址分配及租约情况

    

[root@localhost ~]# cat /var/lib/dhcpd/dhcpd.leases
    # The format of this file is documented in the dhcpd.leases(5) manual page.
    # This lease file was written by isc-dhcp-4.2.5
     
    lease 172.16.1.102 {
      starts 2 2016/11/01 13:55:33;
      ends 2 2016/11/01 13:57:51;
      tstp 2 2016/11/01 13:57:51;
      cltt 2 2016/11/01 13:55:33;
      binding state free;
      hardware ethernet 00:0c:29:96:ea:a9;
      uid "\001\000\014)\226\352\251";
    }
    lease 172.16.1.100 {
      starts 2 2016/11/01 13:56:35;
      ends 2 2016/11/01 14:06:35;
      tstp 2 2016/11/01 14:06:35;
      cltt 2 2016/11/01 13:56:35;
      binding state active;
      next binding state free;
      rewind binding state free;
      hardware ethernet 00:0c:29:9d:3a:2a;
    }
    server-duid "\000\001\000\001\037\253K\017\000\014)e\204u";


 

 

为客户机保留IP地址


1、获取客户机的mac地址

    wKiom1gYRgyhuhNzAAArpvPkX6s814.png-wh_50 

    wKiom1gYRh7wbOBYAAAzBM7joKQ000.png-wh_50 

 

 

2、修改DHCP的配置文件

mac地址不区分大小写,windows客户端的mac地址中的"-"需要换成":"

    

[root@master ~]# vi /etc/dhcp/dhcpd.conf
    ...
    host ftpsrv {
      hardware ethernet 00:0c:29:9d:3a:2a;
      fixed-address 172.16.1.253;
    }
    host winc1 {
      hardware ethernet 00:0c:29:96:ea:a9;
      fixed-address 172.16.1.50;
    }
    ...
     
    [root@localhost ~]# systemctl restart dhcpd.service
    [root@localhost ~]# ss -tnul
    Netid  State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
    udp    UNCONN     0      0                     *:67                                *:*                  
    udp    UNCONN     0      0                     *:31306                             *:*                  
    udp    UNCONN     0      0                    :::45651                            :::*                  
    tcp    LISTEN     0      128                   *:22                                *:*                  
    tcp    LISTEN     0      100           127.0.0.1:25                                *:*                  
    tcp    LISTEN     0      128                  :::22                               :::*                  
    tcp    LISTEN     0      100                 ::1:25                               :::*


 

 

3、客户端重新获取IP地址

    wKiom1gYRkPQ2nOaAAArwwbsII8386.png-wh_50 

 

    wKiom1gYRkyynWA-AAB9qx1DM5U548.png-wh_50