21.2dhcp服务和PXE,virbr0

介绍

vmware虚拟机的仅主机模式中,一般都会选择dhcp服务,给vmware中仅主机模式的linux网络适配器分配ip地址,如图1。现在,关闭图1中的仅主机模式的dhcp服务,在虚拟机中centos7上面搭建一个dhcp服务,以便为虚拟机中的其它主机提供dhcp服务。

另外,此处搭建的dhcp实验,是为下一个实验(从网络自动化安装centos操作系统做准备)

如图1,停止仅主机模式的dhcp服务,则vmware上的虚拟主机靠dhcp获取ip失败,只能手动配置ip,以便dhcp服务只有本地centos7即将搭建的dhcp服务
图1图1


小插曲

ifconfig命令出现的名为virbr0的虚拟网卡:

在centos7里安装 Xen 或安装 KVM 后,会发现网络接口里多了一个叫做 virbr0 的虚拟网络接口,并且会自动获得一个私有地址(virbr0网卡的IP地址都为192.168.122.1),

这是在安装和启用了libvirt 服务后生成的,libvirt 在服务器host上生成一个 virtual network switch (virbr0),host上所有的虚拟机(guests)通过这个 virbr0 连起来。

默认情况下 virbr0 使用的是 NAT 模式(采用 IP Masquerade),所以这种情况下 guest 通过 host 才能访问外部。

大多数时候我们使用虚拟机 bridge(桥接)直接连到局域网里,所以这个 virbr0 不是必须的(注:bridge 和这里的 virbr0 bridge 是互不相干的)。

关掉这个 virbr0 的步骤如下

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:3d:9a:f8  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@centos7:~]# yum search virsh
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
====================================== N/S matched: virsh ======================================
fence-agents-virsh.x86_64 : Fence agent for virtual machines based on libvirt

  Name and summary matches only, use "search all" for everything.
[root@centos7:~]# yum install fence-agents-virsh.x86_64

[root@centos7:~]# virsh net-destroy default
Network default destroyed

[root@centos7:~]# virsh net-undefine default
Network default has been undefined
[root@centos7:~]# systemctl restart libvirtd

别人博客解决方法,尚未验证

https://blog.csdn.net/radkitty/article/details/6034577


此处删除了virbr0,且所有的虚拟机上的centos均只有一个网卡且为仅主机模式,以便简化后面的搭建centos7 dhcp服务的实验

DHCP服务

      [root@centos6 dhclient ]#cd /var/lib/dhclient/
      [root@centos6 dhclient ]# ls
      dhclient-eth0.leases
      [root@centos6 dhclient ]#dhclient

      [root@centos7:~]# yum info dhcp
      [root@centos7:~]# rpm -ql dhcp
      /etc/NetworkManager
      /etc/NetworkManager/dispatcher.d
      /etc/NetworkManager/dispatcher.d/12-dhcpd
      /etc/dhcp/dhcpd.conf                        #dhcp服务配置文件
      /etc/dhcp/dhcpd6.conf
      /etc/dhcp/scripts
      /usr/lib/systemd/system/dhcpd.service
      /usr/lib/systemd/system/dhcpd6.service
      /etc/dhcp/scripts/README.scripts
      /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example   #范例文件
      /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
[root@centos7:~]# rpm -q --scripts dhcp
      preinstall scriptlet (using /bin/sh):
      # /usr/share/doc/setup/uidgid
      getent group dhcpd >/dev/null || groupadd --force --gid 177 --system dhcpd
      if ! getent passwd dhcpd >/dev/null ; then
          if ! getent passwd 177 >/dev/null ; then
            useradd --system --uid 177 --gid dhcpd --home / --shell /sbin/nologin --comment "DHCP server" dhcpd
          else
            useradd --system --gid dhcpd --home / --shell /sbin/nologin --comment "DHCP server" dhcpd
          fi

      [root@centos7:~]# cat /etc/dhcp/dhcpd.conf
      #
      # DHCP Server Configuration file.
      #   see /usr/share/doc/dhcp*/dhcpd.conf.example
      #   see dhcpd.conf(5) man page
      #
      [root@centos7:~]# 

启动dhcpd服务


      [root@centos7:~]# systemctl start dhcpd
      Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details.

      [root@centos7:~]# systemctl status dhcpd
      ● dhcpd.service - DHCPv4 Server Daemon
         Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
         Active: failed (Result: exit-code) since Sun 2018-07-15 21:04:01 CST; 8s ago
           Docs: man:dhcpd(8)
                 man:dhcpd.conf(5)
        Process: 12903 ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid (code=exited, status=1/FAILURE)
       Main PID: 12903 (code=exited, status=1/FAILURE)

      Jul 15 21:04:01 centos7 dhcpd[12903]: For info, please visit https://www.isc.org/software/dhcp/
      Jul 15 21:04:01 centos7 dhcpd[12903]: Not searching LDAP since ldap-server, ldap-port and ...ile
      Jul 15 21:04:01 centos7 dhcpd[12903]: Wrote 0 leases to leases file.
      Jul 15 21:04:01 centos7 dhcpd[12903]: 
      Jul 15 21:04:01 centos7 dhcpd[12903]: No subnet declaration for ens38 (192.168.139.128).
      Jul 15 21:04:01 centos7 dhcpd[12903]: ** Ignoring requests on ens38.  If this is not what
      Jul 15 21:04:01 centos7 systemd[1]: dhcpd.service: main process exited, code=exited, statu...URE
      Jul 15 21:04:01 centos7 systemd[1]: Failed to start DHCPv4 Server Daemon.
      Jul 15 21:04:01 centos7 systemd[1]: Unit dhcpd.service entered failed state.
      Jul 15 21:04:01 centos7 systemd[1]: dhcpd.service failed.
      Hint: Some lines were ellipsized, use -l to show in full.
      [root@centos7:~]# 

报错原因:由于dhcp服务是就近为本网段服务的,如果不定义本网络的dhcp服务主机ip所在的子网ip范围,会报错。只有配置好为本网络中的主机提供dhcp服务的ip范围,再向外网主机提供dhcp,才不会报错

根据上述的dhcpd的配置文件的描述,
          [root@centos7:~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example                     /etc/dhcp/dhcpd.conf 
          [root@centos7:~]# cat /etc/dhcp/dhcpd.conf
          # dhcpd.conf
          #
          # Sample configuration file for ISC dhcpd
          #

          # option definitions common to all supported networks...
          #option domain-name "example.org";                                 #此处的同/etc/resolv.conf,示例如下
          option domain-name "baidu.com";
          #option domain-name-servers ns1.example.org, ns2.example.org;     dns解析示例,此行修改
          option domain-name-servers 114.114.114.114,1.1.1.1;  
          default-lease-time 600;                                            #dhcp的ip地址租期
          max-lease-time 7200;                                               #最大租期(单位:秒)

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

          # If this DHCP server is the official DHCP server for the local
          # network, the authoritative directive should be uncommented.
          #authoritative;

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

          # No service will be given on this subnet, but declaring it helps the 
          # DHCP server to understand the network topology.

          #subnet 10.152.187.0 netmask 255.255.255.0 {
          #}
          subnet 192.168.31.0 netmask 255.255.255.0 {
                          range 192.168.31.100 192.168.31.130;                          #配置一个本网段的dhcp服务ip范围
                          option routers 192.168.31.254;                                #可以添加网关
          }
          # This is a very basic subnet declaration.

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

          # This declaration allows BOOTP clients to get dynamic addresses,
          # which we don't really recommend.

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

          # A slightly different configuration for an internal subnet.
          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;
          }

          # Hosts which require special configuration options can be listed in
          # host statements.   If no address is specified, the address will be
          # allocated dynamically (if possible), but the host-specific information
          # will still come from the host declaration.

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

          # Fixed IP addresses can also be specified for hosts.   These addresses
          # should not also be listed as being available for dynamic assignment.
          # Hosts for which fixed IP addresses have been specified can boot using
          # BOOTP or DHCP.   Hosts for which no fixed address is specified can only
          # be booted with DHCP, unless there is an address range on the subnet
          # to which a BOOTP client is connected which has the dynamic-bootp flag
          # set.
          host fantasia {
            hardware ethernet 08:00:07:26:c0:a5;
            fixed-address fantasia.fugue.com;
          }

          # You can declare a class of clients and then do address allocation
          # based on that.   The example below shows a case where all clients
          # in a certain class get addresses on the 10.17.224/24 subnet, and all
          # other clients get addresses on the 10.0.29/24 subnet.

          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中ping www也可以,centos7中会参考如下文件中的search localdomain 自动补全

[root@centos7:~]# cat /etc/resolv.conf 
            # Generated by NetworkManager
            search localdomain
            nameserver 192.168.139.2
            [root@centos7:~]# 
            此处给centos7一个桥接网址,以便可以访问外网
            [root@centos7:~]# vim /etc/resolv.conf 
            [root@centos7:~]# cat /etc/resolv.conf 
            # Generated by NetworkManager
            search baidu.com
            nameserver 192.168.139.2
            [root@centos7:~]# 
            [root@centos7:~]# ping www
            PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
            64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=35.3 ms
            64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=61.2 ms
            ^C
            [root@centos7:~]# ping www.baidu.com
            PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
            64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=128 time=72.4 ms
            64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=128 time=119 ms
            ^C

重启服务,不会再报错

[root@centos7:~]# systemctl start dhcpd
            [root@centos7:~]# systemctl status dhcpd
            ● dhcpd.service - DHCPv4 Server Daemon
               Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
               Active: active (running) since Sun 2018-07-15 21:40:15 CST; 41s ago
                 Docs: man:dhcpd(8)

开启虚拟机中的centos6,并且只有一个仅主机模式的网络适配器,获取ip如下

[root@centos6 ~]# ifconfig
            eth0      Link encap:Ethernet  HWaddr 00:0C:29:E2:9A:30  
                      inet addr:192.168.31.100  Bcast:192.168.31.255  Mask:255.255.255.0
                      inet6 addr: fe80::20c:29ff:fee2:9a30/64 Scope:Link
                      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                      RX packets:68 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000 
                      RX bytes:8657 (8.4 KiB)  TX bytes:7605 (7.4 KiB)

            [root@centos6 ~]# route -n
            Kernel IP routing table
            Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
            192.168.31.0    0.0.0.0         255.255.255.0   U     1      0        0 eth0
            0.0.0.0         192.168.31.254  0.0.0.0         UG    0      0        0 eth0


            [root@centos7:/var/lib/dhcpd ]# ls
            dhcpd6.leases  dhcpd.leases  dhcpd.leases~
            [root@centos7:/var/lib/dhcpd ]# less 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

            server-duid "\000\001\000\001\"\336\353W\000\014)\020\276\272";

            lease 192.168.31.100 {
              starts 1 2018/07/16 05:38:10;
              ends 1 2018/07/16 05:48:10;
              cltt 1 2018/07/16 05:38:10;
              binding state active;
              next binding state free;
              rewind binding state free;
              hardware ethernet 00:0c:29:e2:9a:30;
              client-hostname "centos6";
            }
            lease 192.168.31.101 {
              starts 1 2018/07/16 05:40:42;
              ends 1 2018/07/16 05:50:42;
              cltt 1 2018/07/16 05:40:42;
              binding state active;
              next binding state free;
              rewind binding state free;
              hardware ethernet 00:0c:29:c2:3e:12;
              client-hostname "centos7-2";
            }
            (END)

centos6再添加一个仅主机模式的网络适配器,


            [root@centos6 ~]# ifconfig
            eth0      Link encap:Ethernet  HWaddr 00:0C:29:E2:9A:30  
                      inet addr:192.168.31.100  Bcast:192.168.31.255  Mask:255.255.255.0
                      inet6 addr: fe80::20c:29ff:fee2:9a30/64 Scope:Link
                      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                      RX packets:266 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:285 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000 
                      RX bytes:25296 (24.7 KiB)  TX bytes:28521 (27.8 KiB)

            eth1      Link encap:Ethernet  HWaddr 00:0C:29:E2:9A:3A  
                      inet6 addr: fe80::20c:29ff:fee2:9a3a/64 Scope:Link
                      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                      RX packets:4 errors:0 dropped:0 overruns:0 frame:0
                      TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
                      collisions:0 txqueuelen:1000 
                      RX bytes:240 (240.0 b)  TX bytes:258 (258.0 b)
            若要eth1的mac地址绑定一个固定的ip地址,可以在dhcp服务器上的配置文件中,绑定ip

[root@centos7:~]# cat /etc/dhcp/dhcpd.conf

        # set.
        host fantasia {
          hardware ethernet 08:00:07:26:c0:a5;
          fixed-address fantasia.fugue.com;
        }
        找到此行,在其下面添加如下内容
        host testcentos6 {
          hardware ethernet 00:0C:29:E2:9A:3A;
          fixed-address 192.168.31.222;
        }

    重启centos7中的dhcp服务
    [root@centos7:/var/lib/dhcpd ]# systemctl restart dhcpd

    重启一下centos6的网络服务
    [root@centos6 ~]# service network restart
    Shutting down interface eth0:  Device state: 3 (disconnected)
                                                               [  OK  ]
    Shutting down loopback interface:                          [  OK  ]
    Bringing up loopback interface:                            [  OK  ]
    Bringing up interface eth0:  Active connection state: activating
    Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/2
    state: activated
    Connection activated

    此处eth1并没有获取到ip地址,忘记添加eth1的配置文件
    [root@centos6 network-scripts]# ls
    ifcfg-eth0   #没有eht1的网卡配置文件
    [root@centos6 network-scripts]# cp ifcfg-eth0 ifcfg-eth1
    [root@centos6 network-scripts]# ls
    ifcfg-eth0   ifdown-ib    ifdown-routes  ifup-eth   ifup-plusb   ifup-wireless
    ifcfg-eth1
    [root@centos6 network-scripts]# vim ifcfg-eth1
    DEVICE=eth0
    TYPE=Ethernet
    NM_CONTROLLED=yes
    BOOTPROTO=dhcp
    上述添加完成后,

    [root@centos6 ~]# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:0C:29:E2:9A:30  
              inet addr:192.168.31.100  Bcast:192.168.31.255  Mask:255.255.255.0
              inet6 addr: fe80::20c:29ff:fee2:9a30/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:680 errors:0 dropped:0 overruns:0 frame:0
              TX packets:761 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:62811 (61.3 KiB)  TX bytes:81395 (79.4 KiB)

    eth1      Link encap:Ethernet  HWaddr 00:0C:29:E2:9A:3A  
              inet addr:192.168.31.222  Bcast:192.168.31.255  Mask:255.255.255.0
              inet6 addr: fe80::20c:29ff:fee2:9a3a/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:284 errors:0 dropped:0 overruns:0 frame:0
              TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:19967 (19.4 KiB)  TX bytes:4214 (4.1 KiB)

    [root@centos6 ~]# cd /var/lib/dhclient/
    [root@centos6 dhclient]# ls
    dhclient-5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03-eth0.lease  dhclient-eth0.leases
    dhclient-9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04-eth1.lease
    [root@centos6 dhclient]# cat dhclient-9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04-eth1.lease
    lease {
      interface "eth1";
      fixed-address 192.168.31.222;
      option subnet-mask 255.255.255.0;
      option routers 192.168.31.254;
      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.31.7;
      option domain-name "baidu.com";
      renew 1 2018/07/16 14:11:02;
      rebind 1 2018/07/16 14:14:50;
      expire 1 2018/07/16 14:16:05;
    }
    lease {
      interface "eth1";
      fixed-address 192.168.31.222;
      option subnet-mask 255.255.255.0;
      option routers 192.168.31.254;
      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.31.7;
      option domain-name "baidu.com";
      renew 1 2018/07/16 14:15:54;
      rebind 1 2018/07/16 14:19:47;
      expire 1 2018/07/16 14:21:02;
    }
    [root@centos6 dhclient]# 

pxe与dhcp服务

一台主机的网卡从dhcp服务器获取ip地址后,还可以获取下一个tftp服务器的一个文件,而这个文件可以用来实现从网络引导系统自动化安装

[root@centos7:~]# cat /etc/dhcp/dhcpd.conf
# will still come from the host declaration.

host passacaglia {
hardware ethernet 0:0:c0:5d:bd:95;
filename “vmunix.passacaglia”;
next-server tftp #添加此行,下一个服务器就是可以提供此文件的tftp服务器
server-name “toccata.fugue.com”;
}

tftp轻量级服务器,文件小。而如果要引导这台主机实现从网络自动安装系统,那么这台主机的硬件网卡只能从dhcp获取一个ip地址,而没有操作系统,没有软件tftp客户端软件,如何下载文件呢?
解决方法是:一般我们现在购买电脑主机的网卡里面都集成有tpft轻量级客户端软件,只要网卡获取ip地址,网卡里的tftp客户端软件就可以下载文件了
以前的网卡集成有tftp轻量级的网卡比较贵,现在已经普及了

Preboot Excution Environment 预启动执行环境 PXE(网络自动化安装操作系统)

猜你喜欢

转载自blog.csdn.net/csdn_immortal/article/details/81068259