网络服务-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