網絡服務-DCHP原理與配置

實際操作過程:

DHCP服務器配置

  1. 安裝yum 掛載光盤 關閉防火牆 關閉selinux機制(配置DHCP服務器)
  2. yum -y install dhcp       //安裝dhcp
  3. 配置DHCP服務

[[email protected] ~]# cat /etc/dhcp/dhcpd.conf

# DHCP Server Configuration file.         

#   see /usr/share/doc/dhcp*/dhcpd.conf.sample    //去這個文件寫進/etc/dhcp/dhcpd.conf

#   see 'man 5 dhcpd.conf'

[[email protected] ~]# cat /usr/share/doc/dhcp*/dhcpd.conf.sample >/etc/dhcp/dhcpd.conf

[[email protected] ~]# vim /etc/dhcp/dhcpd.conf

option domain-name-servers 202.106.0.20, 8.8.8.8;    //DNS服務器IP

 

default-lease-time 21600;                         //默認租約時間6小時

max-lease-time 43200;                           //最長租約時間 12小時

 

log-facility local7;                                日誌記錄配置

 

subnet 192.168.64.0 netmask 255.255.255.0 {        

  range 192.168.64.2 192.168.64.254;               IP地址池,該網段的IP地址範圍

  option routers 192.168.64.2;                     路由器網關

}

 

subnet 192.168.10.0 netmask 255.255.255.0 {

  range 192.168.10.2 192.168.10.254;

  option routers 192.168.10.2;

}

 

subnet 192.168.20.0 netmask 255.255.255.0 {

  range 192.168.20.2 192.168.20.254;

  option routers 192.168.20.2;

}

 

subnet 192.168.30.0 netmask 255.255.255.0 {

  range 192.168.30.2 192.168.30.254;

  option routers 192.168.30.2;

}

 

#host fantasia {

#  hardware ethernet 08:00:07:26:c0:a5;     客戶機mac地址

#  fixed-address fantasia.fugue.com;        指定分配的IP地址

#}

保存退出  (注意:此DHCP服務器必須有一個網絡設備的IP屬於所設置的網段)

[[email protected] ~]# /etc/init.d/dhcpd restart    //重啓DHCP服務

正在啓動 dhcpd:                                           [確定]

[[email protected] ~]# chkconfig dhcpd on

[[email protected] ~]# chkconfig --list dhcpd

dhcpd          0:關閉    1:關閉    2:啓用    3:啓用    4:啓用    5:啓用    6:關閉

[[email protected] ~]# ping 192.168.64.2         //查看是否ping通網關

[[email protected] ~]# tail -t /var/log/messages   //用於排錯

DHCP配置完成

 

Linux中DHCP中繼配置

1、中繼添加個網卡  分別是   eth0     192.168.64.1   (VMnet 8)

                            eth1     192.168.10.1    (VMnet 1)

                            eth2     192.168.20.1    (VMnet 2)

                            eth3     192.168.30.1    (VMnet 3)

注意:要先關閉虛擬機中本身的DHCP服務

Windows + R   輸入 service.msc 

2、網卡配置

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

HWADDR=00:0c:29:67:47:bc

TYPE=Ethernet

UUID=1b7587cd-4646-46e7-b46d-7a6983d44c8a

ONBOOT=yes                   //手動添加ONBOOT=yes (eth1 eth2 eth3 同樣添加)

NM_CONTROLLED=yes

BOOTPROTO=none

IPADDR=192.168.64.2              //此設備IP與DHCP服務器的網關互指,且在同一網段上

NETMASK=255.255.255.0

DNS1=202.106.0.20

[[email protected] ~]# ip a         //配置結果如下 中繼不配置網關

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:bc brd ff:ff:ff:ff:ff:ff

    inet 192.168.64.2/24 brd 192.168.64.255 scope global eth0

    inet6 fe80::20c:29ff:fe67:47bc/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:c6 brd ff:ff:ff:ff:ff:ff

    inet 192.168.10.1/24 brd 192.168.10.255 scope global eth1

    inet6 fe80::20c:29ff:fe67:47c6/64 scope link

       valid_lft forever preferred_lft forever

4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:d0 brd ff:ff:ff:ff:ff:ff

    inet 192.168.20.1/24 brd 192.168.20.255 scope global eth2

    inet6 fe80::20c:29ff:fe67:47d0/64 scope link

       valid_lft forever preferred_lft forever

5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:da brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.1/24 brd 192.168.30.255 scope global eth3

    inet6 fe80::20c:29ff:fe67:47da/64 scope link

       valid_lft forever preferred_lft forever

[[email protected] ~]# service network start      //啓動激活網卡

 

配置yum 關閉防火牆 關閉selinux

[[email protected] ~]# yum -y install dhcp      //DHCP 上安裝dhcp

[[email protected] ~]# vim /etc/sysconfig/dhcrelay     //編輯這個文件

# Command line options here

DHCRELAYARGS="192.168.64.101"                 //指向是DHCP服務器IP

# DHCPv4 only                         

INTERFACES="eth0 eth1 eth2 eth3"                

# DHCPv4 only

DHCPSERVERS=""

[[email protected] ~]# /etc/init.d/dhcrelay start      //啓動中繼

[[email protected] ~]# chkconfig dhcrelay on

[[email protected] ~]# chkconfig dhcrelay   --list

dhcrelay          0:關閉    1:關閉    2:啓用    3:啓用    4:啓用    5:啓用    6:關閉

[[email protected] ~]# ping 192.168.64.101

[[email protected] ~]# dhcrelay 192.168.64.101

DHCP配置完成

 

測試

Windows :Windows 7

首先虛擬網絡編輯器中使用本地DHCP服務器中的去電鉤

Windows +R  輸入  cmd

 

 

Linux:CentOS 6.8

[[email protected] ~]# cd /etc/sysconfig/network-scripts/

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth1

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth2

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth3

[[email protected] network-scripts]# cat ifcfg-eth0

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes          //如果沒有手動添加

NM_CONTROLLED=yes

BOOTPROTO=dhcp

多塊網卡下開啓路由功能

[[email protected] ~]# vim /etc/sysctl.conf

#net.ipv4.ip_forward = 0

net.ipv4.ip_forward = 1

[[email protected] network-scripts]# iptables -F      //清空網卡策略

[[email protected] network-scripts]# dhclient -d   //後面也可以加設備名   

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:e5 brd ff:ff:ff:ff:ff:ff

    inet 192.168.64.3/24 brd 192.168.64.255 scope global eth0

    inet 192.168.64.4/24 brd 192.168.64.255 scope global secondary eth0

    inet6 fe80::20c:29ff:fec5:6be5/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:ef brd ff:ff:ff:ff:ff:ff

    inet 192.168.10.2/24 brd 192.168.10.255 scope global eth1

    inet6 fe80::20c:29ff:fec5:6bef/64 scope link

       valid_lft forever preferred_lft forever

4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:f9 brd ff:ff:ff:ff:ff:ff

    inet 192.168.20.2/24 brd 192.168.20.255 scope global eth2

    inet6 fe80::20c:29ff:fec5:6bf9/64 scope link

       valid_lft forever preferred_lft forever

5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:03 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.2/24 brd 192.168.30.255 scope global eth3

    inet6 fe80::20c:29ff:fec5:6b03/64 scope link

       valid_lft forever preferred_lft forever

實際操作過程:

DHCP服務器配置

  1. 安裝yum 掛載光盤 關閉防火牆 關閉selinux機制(配置DHCP服務器)
  2. yum -y install dhcp       //安裝dhcp
  3. 配置DHCP服務

[[email protected] ~]# cat /etc/dhcp/dhcpd.conf

# DHCP Server Configuration file.         

#   see /usr/share/doc/dhcp*/dhcpd.conf.sample    //去這個文件寫進/etc/dhcp/dhcpd.conf

#   see 'man 5 dhcpd.conf'

[[email protected] ~]# cat /usr/share/doc/dhcp*/dhcpd.conf.sample >/etc/dhcp/dhcpd.conf

[[email protected] ~]# vim /etc/dhcp/dhcpd.conf

option domain-name-servers 202.106.0.20, 8.8.8.8;    //DNS服務器IP

 

default-lease-time 21600;                         //默認租約時間6小時

max-lease-time 43200;                           //最長租約時間 12小時

 

log-facility local7;                                日誌記錄配置

 

subnet 192.168.64.0 netmask 255.255.255.0 {        

  range 192.168.64.2 192.168.64.254;               IP地址池,該網段的IP地址範圍

  option routers 192.168.64.2;                     路由器網關

}

 

subnet 192.168.10.0 netmask 255.255.255.0 {

  range 192.168.10.2 192.168.10.254;

  option routers 192.168.10.2;

}

 

subnet 192.168.20.0 netmask 255.255.255.0 {

  range 192.168.20.2 192.168.20.254;

  option routers 192.168.20.2;

}

 

subnet 192.168.30.0 netmask 255.255.255.0 {

  range 192.168.30.2 192.168.30.254;

  option routers 192.168.30.2;

}

 

#host fantasia {

#  hardware ethernet 08:00:07:26:c0:a5;     客戶機mac地址

#  fixed-address fantasia.fugue.com;        指定分配的IP地址

#}

保存退出  (注意:此DHCP服務器必須有一個網絡設備的IP屬於所設置的網段)

[[email protected] ~]# /etc/init.d/dhcpd restart    //重啓DHCP服務

正在啓動 dhcpd:                                           [確定]

[[email protected] ~]# chkconfig dhcpd on

[[email protected] ~]# chkconfig --list dhcpd

dhcpd          0:關閉    1:關閉    2:啓用    3:啓用    4:啓用    5:啓用    6:關閉

[[email protected] ~]# ping 192.168.64.2         //查看是否ping通網關

[[email protected] ~]# tail -t /var/log/messages   //用於排錯

DHCP配置完成

 

Linux中DHCP中繼配置

1、中繼添加個網卡  分別是   eth0     192.168.64.1   (VMnet 8)

                            eth1     192.168.10.1    (VMnet 1)

                            eth2     192.168.20.1    (VMnet 2)

                            eth3     192.168.30.1    (VMnet 3)

注意:要先關閉虛擬機中本身的DHCP服務

Windows + R   輸入 service.msc 

2、網卡配置

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

HWADDR=00:0c:29:67:47:bc

TYPE=Ethernet

UUID=1b7587cd-4646-46e7-b46d-7a6983d44c8a

ONBOOT=yes                   //手動添加ONBOOT=yes (eth1 eth2 eth3 同樣添加)

NM_CONTROLLED=yes

BOOTPROTO=none

IPADDR=192.168.64.2              //此設備IP與DHCP服務器的網關互指,且在同一網段上

NETMASK=255.255.255.0

DNS1=202.106.0.20

[[email protected] ~]# ip a         //配置結果如下 中繼不配置網關

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:bc brd ff:ff:ff:ff:ff:ff

    inet 192.168.64.2/24 brd 192.168.64.255 scope global eth0

    inet6 fe80::20c:29ff:fe67:47bc/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:c6 brd ff:ff:ff:ff:ff:ff

    inet 192.168.10.1/24 brd 192.168.10.255 scope global eth1

    inet6 fe80::20c:29ff:fe67:47c6/64 scope link

       valid_lft forever preferred_lft forever

4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:d0 brd ff:ff:ff:ff:ff:ff

    inet 192.168.20.1/24 brd 192.168.20.255 scope global eth2

    inet6 fe80::20c:29ff:fe67:47d0/64 scope link

       valid_lft forever preferred_lft forever

5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNK

NOWN qlen 1000    link/ether 00:0c:29:67:47:da brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.1/24 brd 192.168.30.255 scope global eth3

    inet6 fe80::20c:29ff:fe67:47da/64 scope link

       valid_lft forever preferred_lft forever

[[email protected] ~]# service network start      //啓動激活網卡

 

配置yum 關閉防火牆 關閉selinux

[[email protected] ~]# yum -y install dhcp      //DHCP 上安裝dhcp

[[email protected] ~]# vim /etc/sysconfig/dhcrelay     //編輯這個文件

# Command line options here

DHCRELAYARGS="192.168.64.101"                 //指向是DHCP服務器IP

# DHCPv4 only                         

INTERFACES="eth0 eth1 eth2 eth3"                

# DHCPv4 only

DHCPSERVERS=""

[[email protected] ~]# /etc/init.d/dhcrelay start      //啓動中繼

[[email protected] ~]# chkconfig dhcrelay on

[[email protected] ~]# chkconfig dhcrelay   --list

dhcrelay          0:關閉    1:關閉    2:啓用    3:啓用    4:啓用    5:啓用    6:關閉

[[email protected] ~]# ping 192.168.64.101

[[email protected] ~]# dhcrelay 192.168.64.101

DHCP配置完成

 

測試

Windows :Windows 7

首先虛擬網絡編輯器中使用本地DHCP服務器中的去電鉤

Windows +R  輸入  cmd

 

 

Linux:CentOS 6.8

[[email protected] ~]# cd /etc/sysconfig/network-scripts/

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth1

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth2

[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth3

[[email protected] network-scripts]# cat ifcfg-eth0

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes          //如果沒有手動添加

NM_CONTROLLED=yes

BOOTPROTO=dhcp

多塊網卡下開啓路由功能

[[email protected] ~]# vim /etc/sysctl.conf

#net.ipv4.ip_forward = 0

net.ipv4.ip_forward = 1

[[email protected] network-scripts]# iptables -F      //清空網卡策略

[[email protected] network-scripts]# dhclient -d   //後面也可以加設備名   

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:e5 brd ff:ff:ff:ff:ff:ff

    inet 192.168.64.3/24 brd 192.168.64.255 scope global eth0

    inet 192.168.64.4/24 brd 192.168.64.255 scope global secondary eth0

    inet6 fe80::20c:29ff:fec5:6be5/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:ef brd ff:ff:ff:ff:ff:ff

    inet 192.168.10.2/24 brd 192.168.10.255 scope global eth1

    inet6 fe80::20c:29ff:fec5:6bef/64 scope link

       valid_lft forever preferred_lft forever

4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:f9 brd ff:ff:ff:ff:ff:ff

    inet 192.168.20.2/24 brd 192.168.20.255 scope global eth2

    inet6 fe80::20c:29ff:fec5:6bf9/64 scope link

       valid_lft forever preferred_lft forever

5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000

    link/ether 00:0c:29:c5:6b:03 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.2/24 brd 192.168.30.255 scope global eth3

    inet6 fe80::20c:29ff:fec5:6b03/64 scope link

       valid_lft forever preferred_lft forever