7、二种 为二个不同的子网配置DHCP服务器,dhcp服务器


 

环境如下:        (参考之前,保证二个子网可以互相ping通)

虚拟机vm1        192.168.170.3                    VMnet8 (NAT模式)

虚拟机vm2        192.168.155.3                    VMnet1 (仅主机模式)

虚拟机gate        192.168.170.4 (eth1)       VMnet8 (NAT模式)

                       192.168.155.4 (eth0)       VMnet1 (仅主机模式)

第一种:在网关gate配置DHCP服务器

(eth0)   为   192.168.155.0/24   子网提供IP地址分配服务

(eth1)   为   192.168.170.0/24   子网提供IP地址分配服务

 

1、putty连接192.168.170.4


[root@mail root]#  vi    /etc/dhcpd.conf

ddns-update-style interim;
ignore client-updates;

subnet 192.168.155.0 netmask 255.255.255.0 {

        option routers                  192.168.155.4;
        option subnet-mask              255.255.255.0;
        option domain-name-servers      192.168.155.4;
        option time-offset              28800;  # Eastern Standard Time

        range  192.168.155.10  192.168.155.198;
        default-lease-time 86400;
        max-lease-time 172800;

 host www {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;

            }

}

subnet 192.168.170.0 netmask 255.255.255.0 {

        option routers                  192.168.170.4;
        option subnet-mask              255.255.255.0;
        option domain-name-servers      192.168.170.4;
        option time-offset              28800;  # Eastern Standard Time


        range  192.168.170.6  192.168.170.230;
        default-lease-time 86400;
        max-lease-time 172800;

 host nd {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;

            }
}

 

 

 

设置eth0网卡的IP地址,直接编辑

[root@mail root]#  vi    /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.155.4
NETMASK=255.255.255.0
GATEWAY=192.168.155.4

设置eth1网卡的IP地址,直接编辑

[root@mail root]#  vi    /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.170.4
NETMASK=255.255.255.0
GATEWAY=192.168.170.4

 

重启network,DHCP服务

[root@mail sysconfig]#  service    network    restart

service    dhcpd   restart

VMnet1 和 VMnet8 的(属性-》TCP/IPv4-》把里面的“自动获取IP”打上勾,设置DNS服务器

网络几)

ipconfig /all

(看 ip 是不是在 刚才设置的ip范围之内,和下面红色部分与设置的一致)


注意:

1、因为会有租期的影响,如果某步设置出错,重启DHCP服务后,IP地址不变,可以把系统时间调快几天,让租约失效,重新获取。

 

2、

看看 VMnet1 和 VMnet8 的IP在刚才设置文件里面的范围?如果不是,可能就是其他虚拟机分配的IP。去把DHCP服务关闭就好了。
到主界面 “编辑”-》分别把“使用本地DHCP服务将IP地址分配给虚拟机”的勾 去掉。

禁用,然后启动 这二个网卡,重新获取

 

3、可能子网其他的DHCP服务器分配了,这时候只能看看是那个关掉其DHCP服务

 

如果在Linux端也可以用  dhclient 命令来测试DHCP服务

 

第二种:配置DHCP中继代理服务器

(如果做了第一种,先把gate的DHCP服务关闭,避免影响今次实验)

     DHCP请求广播包是不能通过路由器的,因为路由器是具有隔离广播风暴的功能

所以在VM1建立DHCP服务器,192.168.155.0/24子网是没法获取到的,这时候就需要在VM2建立DHCP中继代理服务器。

(二个子网要ping通,这里VM1和VM2ping通)

 

1、在VM1(170.3)建立DHCP服务器,基本步骤 跟上一篇基本一样,就是/etc/dhcpd.conf文件多了个子网

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

ddns-update-style interim;
ignore client-updates;
subnet 192.168.170.0 netmask 255.255.255.0 {
        option routers                  192.168.170.4;
        option subnet-mask              255.255.255.0;
        option domain-name              "gr.org";
        option domain-name-servers       192.168.170.3;
        option time-offset              28800;
        range dynamic-bootp  192.168.170.30   192.168.170.110;
        default-lease-time 21600;
        max-lease-time 43200;

        # we want the nameserver to appear at a fixed address
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;

            }
}

subnet 192.168.155.0 netmask 255.255.255.0 {

        option routers                  192.168.155.4;
        option subnet-mask              255.255.255.0;
        option domain-name-servers      192.168.155.3;
        option time-offset              28800;  # Eastern Standard Time
        range  192.168.155.81   192.168.155.200;
        default-lease-time 86400;
        max-lease-time 172800;

}

2、gate端

保证开启转发   ( 以前做过可以跳过 )

[root@localhost root]#  vi    /etc/sysctl.conf

net.ipv4.ip_forward = 1

[root@localhost root]# sysctl    -p   (更改生效)

关闭防火墙,或者以后学到防火墙时,直接放行66和67端口

[root@localhost root]#  service    iptables    stop

 

3、VM2(155.3)开始配置DHCP中继代理服务器

先安装DHCP软件,参考上一篇

[root@localhost root]#  vi     /etc/sysconfig/dhcrelay  (设置DHCP服务器)

# Command line options here
INTERFACES="eth0"
DHCPSERVERS="192.168.170.3"

 

[root@localhost root]#   service   dhcrelay    start      (启动中继服务)
启动 dhcrelay:                                            [  确定  ]

4、去Windows端测试

ipconfig/all

去170.3机查看分配的IP网络信息
[root@localhost root]#   cat    /var/lib/dhcp/dhcpd.leases

image

相关内容