天天看点

如何使用两台主机实现三个网段之间互通且三个网段均可联网

    版权声明:对于本博客所有原创文章,允许个人、教育和非商业目的使用,但务必保证文章的完整性且不作任何修改地以超链接形式注明原始作者、出处及本声明。

    博客地址:http://blog.csdn.net/shuxiao9058

    原始作者:季亚

step1:搭建网络拓扑

首先,图1是我们在实验室所搭建的网络拓扑结构,其中192.168.1.*为我们实验室的内网,210.28.164.11通往外网,我们所要实现的功能就是使A、B、C三网段网络互通,且三网段可以使用210.28.164.11这个IP连接到外网上网。

如何使用两台主机实现三个网段之间互通且三个网段均可联网

图1 网络拓扑

网络拓扑注解:上面两台主机是我们欲做路由器且装有多个网卡和LInux操作系统的PC机,其中Route1装有两个网卡,其中一个网卡(设备名eth1)的IP地址为192.168.1.250,另一个网卡(设备名eth0)的IP地址为192.168.2.254;而Route2装有三个网卡,其中一个网卡(设备名em1)是为了与外网相连的,它的IP地址为210.28.164.11,其余网卡(设备名为p1p1和p2p1)的IP地址分别为192.168.3.254和192.168.2.253。下面一层的设备是三个交换机,分别连接三个不同网段的内网,且我们使用三台主机号为5的机器进行测试三个网段之间的网络通畅情况。     

注意:在进行连线的时候注意如果是PC机和交换机相连需要直连线,如果两台PC机之间相连则需要使用交叉线!另外,比较新的网卡有只能识别功能,因此直连线和交叉线均可,相关知识读者可以参考鸟哥的Linux私房菜 服务器架设篇(第二版)中Page26-27页中的内容。

网络拓扑搭建好了,下面我们来进行相关网络配置,首先我们要确认两台机器都装有Linux系统,笔者所使用的两台机器安装的操作系统分别为红帽5.5(Route1)企业版和Fedora15(Route2).详细信息如下:

Route1:

[[email protected] ~]# uname -a
Linux localhost.localdomain 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:02 EDT 2007 i686 i686 i386 GNU/Linux
[[email protected] ~]# 
           

Route2:

[[email protected] ~]# uname -a
Linux localhost.localdomain 2.6.40.6-0.fc15.i686.PAE #1 SMP Tue Oct 4 00:44:38 UTC 2011 i686 i686 i386 GNU/Linux
[[email protected] ~]# 
           

另外,还要确保两台机器都装有iptables,可使用rpm命令检查iptables组件是否安装:

Route1:

[[email protected] ~]# rpm -qa iptables
iptables-1.3.5-1.2.1
[[email protected] ~]# 
           

Route2:

[[email protected] ~]# rpm -qa iptables
iptables-1.4.10-2.fc15.i686
[[email protected] ~]# 
           

如果机器中没有安装iptables,请通过Linux系统安装光盘/镜像文件或在网上搜索(http://rpm.pbone.net)下载获取安装iptables所需的软件包,并将该iptables安装软件拷贝至机器上进行安装。

step2:IP配置

根据图一中的拓扑结构依次配置两台主机的IP地址,如下所示:

Route1:

+-------+ Devernet Configuration +--------+                   
                  |                                         |                   
                  |                                         |                   
                  | Name               eth1________________ |                   
                  | Device             eth1________________ |                   
                  | Use DHCP           [ ]                  |                   
                  | Static IP          192.168.1.250_______ |                   
                  | Netmask            255.255.255.0_______ |                   
                  | Default gateway IP ____________________ |                   
                  |                                         |                   
                  |      +----+            +--------+       |                   
                  |      | Ok |            | Cancel |       |                   
                  |      +----+            +--------+       |                   
                  |                                         |                   
                  |                                         |                   
                  +-----------------------------------------+                   
                                                                                
                                                                                                                                                                
                                                                                
                  +-------+ Devernet Configuration +--------+                   
                  |                                         |                   
                  |                                         |                   
                  | Name               eth0________________ |                   
                  | Device             eth0________________ |                   
                  | Use DHCP           [ ]                  |                   
                  | Static IP          192.168.2.254_______ |                   
                  | Netmask            255.255.255.0_______ |                   
                  | Default gateway IP 192.168.2.253_______ |                   
                  |                                         |                   
                  |      +----+            +--------+       |                   
                  |      | Ok |            | Cancel |       |                   
                  |      +----+            +--------+       |                   
                  |                                         |                   
                  |                                         |                   
                  +-----------------------------------------+                   
                                                                                
                                                                                
           

请注意Route1中eth0的默认网关的设置,不然A、B两个网段将不能正常上网。

Route2:

┌─────────────┤ 网络配置 ├─────────────┐
                    │                                      │
                    │                                      │
                    │ 名称            em1_________________ │
                    │ 设备            em1_________________ │
                    │ 使用 DHCP       [ ]                  │
                    │ 静态 IP         210.28.164.11_______ │
                    │ 子网掩码        255.255.255.0_______ │
                    │ 默认网关 IP     210.28.164.254______ │
                    │ 主 DNS 服务器   61.155.18.30________ │
                    │ 第二 DNS 服务器 208.67.222.222______ │
                    │                                      │
                    │     ┌──────┐          ┌──────┐       │
                    │     │ 确定 │          │ 取消 │       │
                    │     └──────┘          └──────┘       │
                    │                                      │
                    │                                      │
                    └──────────────────────────────────────┘
                    ┌─────────────┤ 网络配置 ├─────────────┐
                    │                                      │
                    │                                      │
                    │ 名称            p1p1________________ │
                    │ 设备            p1p1________________ │
                    │ 使用 DHCP       [ ]                  │
                    │ 静态 IP         192.168.3.254_______ │
                    │ 子网掩码        255.255.255.0_______ │
                    │ 默认网关 IP     ____________________ │
                    │ 主 DNS 服务器   61.155.18.30________ │
                    │ 第二 DNS 服务器 208.67.222.222______ │
                    │                                      │
                    │     ┌──────┐          ┌──────┐       │
                    │     │ 确定 │          │ 取消 │       │
                    │     └──────┘          └──────┘       │
                    │                                      │
                    │                                      │
                    └──────────────────────────────────────┘

                    ┌─────────────┤ 网络配置 ├─────────────┐
                    │                                      │
                    │                                      │
                    │ 名称            p2p1________________ │
                    │ 设备            p2p1________________ │
                    │ 使用 DHCP       [ ]                  │
                    │ 静态 IP         192.168.2.253_______ │
                    │ 子网掩码        255.255.255.0_______ │
                    │ 默认网关 IP     ____________________ │
                    │ 主 DNS 服务器   61.155.18.30________ │
                    │ 第二 DNS 服务器 208.67.222.222______ │
                    │                                      │
                    │     ┌──────┐          ┌──────┐       │
                    │     │ 确定 │          │ 取消 │       │
                    │     └──────┘          └──────┘       │
                    │                                      │
                    │                                      │
                    └──────────────────────────────────────┘
           

备注:上面部分配置信息无意义,请注意使用的均是静态IP,不要勾选DHCP选项。

step3:重启网络

配置完成之后,请输入下面命令重启网络:

Route1:

[[email protected] ~]# service network restart
正在关闭接口 eth0: [确定]
正在关闭接口 eth1: [确定]
关闭环回接口: [确定]
禁用 IPv4 包转送: net.ipv4.ip_forward = 0
[确定]
弹出环回接口: [确定]
弹出界面 eth0: [确定]
弹出界面 eth1: [确定]
[[email protected] ~]# 
           

Route2:

[[email protected] ~]# service network restart
Restarting network (via systemctl):  [确定]
[[email protected] ~]# 
           

注意:请注意网络是否正常开启,如有问题,请查阅相关资料,解决后继续下面工作。

重启网络后可以查看系统中默认的路由信息:

Route1:

[[email protected] ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
[[email protected] ~]# 
           

Route2:

[[email protected] ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         210.28.164.254  0.0.0.0         UG    0      0        0 em1
192.168.2.0     0.0.0.0         255.255.255.0   U     1      0        0 p2p1
192.168.3.0     0.0.0.0         255.255.255.0   U     1      0        0 p1p1
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 p1p1
210.28.164.0    0.0.0.0         255.255.255.0   U     1      0        0 em1
[[email protected] ~]# 
           

从上面的路由表信息中可以知道,系统会自动根据网卡的IP地址信息给网卡添加默认路由信息,如在Route1中设备eth0的IP地址为192.168.2.254,系统会自动给它添加192.168.2.*网段的路由信息;在Route2中设备p2p1的IP地址为192.168.2.253,系统会自动给它添加192.168.2.*网段的路由信息。由以上信息可知由Route1可以通过192.168.2.253直接ping通Route2,但是不能通过192.168.3.254ping通Route2,因为没有到从192.168.2.*或者192.168.1.*到192.168.3.*的路由路径,反向也是如此。

step4:启动系统IP转发(这一步非常重要,其他操作都完成了,但是网络还是不通畅大多都是这个问题)

Linux系统缺省并没有打开IP转发功能,由于进出内网的数据都必须经过防火墙,所以必须启动防火墙的IP数据包转发功能。首先编辑/etc/sysctl.conf文件,并设置参数net.ipv4.ip_forward的值为1,操作命令如下所示:

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

# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

# Controls source route verification
net.ipv4.conf.default.rp_filter = 1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename
# Useful for debugging multi-threaded applications
kernel.core_uses_pid = 1

# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
"/etc/sysctl.conf" 35L, 994C written
[[email protected] ~]# service network restart
正在关闭接口 eth0: [确定]
正在关闭接口 eth1: [确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
弹出界面 eth0: [确定]
弹出界面 eth1: [确定]
[[email protected] ~]# 
           

操作完成后,记得重新启动下网络,最后检查确认IP转发功能的状态,可以查看/proc文件系统中的/proc/sys/net/ipv4/ip_forward状态值,如果该值为0,说明当前系统禁止进行IP转发;如果是1,则说明系统的IP转发功能已经打开。操作命令如下所示:

[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward 
1
[[email protected] ~]# 
           

step5:添加路由信息

终于到了最最核心的部分了,其实就是一个命令的多次使用,以使IP能够路由。由于在第四步中我们知道系统已经为我们自动添加已有网卡所对应IP地址的IP网段的路由信息,因此无需添加本网段的路由信息了。即 无需在Route1添加如下路由信息了:

[[email protected] ~]# route add -net 192.168.2.0/24 eth0
[[email protected] ~]# route add -net 192.168.3.0/24 eth0
           

同样,在Route2也 无需添加如下路由信息:

[[email protected] ~]# route add -net 192.168.1.0/24 p2p1
[[email protected] ~]# route add -net 192.168.2.0/24 p2p1
[[email protected] ~]# route add -net 192.168.3.0/24 p1p1
           

即只需在路由表中添加非0.0.0.0网关的路由信息,添加命令操作如下。 Route1中的操作命令如下所示:

[[email protected] ~]# route add -net 192.168.3.0/24 gw 192.168.2.253 eth0
[[email protected] ~]# route add -net 192.168.2.0/24 gw 192.168.2.253 eth0
[[email protected] ~]# 
           

下面是我在A网段中的一台IP地址为192.168.1.5,网关为192.168.1.250的PC机中的操作:

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\jiya>ping 192.168.1.250

Pinging 192.168.1.250 with 32 bytes of data:

Reply from 192.168.1.250: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.250:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
Control-C
^C
C:\Documents and Settings\jiya>ping 192.168.3.254

Pinging 192.168.3.254 with 32 bytes of data:

Reply from 192.168.3.254: bytes=32 time<1ms TTL=63

Ping statistics for 192.168.3.254:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
Control-C
^C
C:\Documents and Settings\jiya>ping 192.168.3.5

Pinging 192.168.3.5 with 32 bytes of data:

Reply from 192.168.3.5: bytes=32 time<1ms TTL=62
Reply from 192.168.3.5: bytes=32 time<1ms TTL=62

Ping statistics for 192.168.3.5:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
Control-C
^C
C:\Documents and Settings\jiya>tracert 192.168.3.5

Tracing route to 192.168.3.5 over a maximum of 30 hops

  1    <1 ms     *        *     192.168.1.250
  2    <1 ms    <1 ms    <1 ms  192.168.2.253
  3    <1 ms    <1 ms    <1 ms  192.168.3.5

Trace complete.

C:\Documents and Settings\jiya>
           

Route2中的操作命令如下所示:

[[email protected] ~]# route add -net 192.168.1.0/24 gw 192.168.2.254 p2p1
[[email protected] ~]# route add -net 192.168.2.0/24 gw 192.168.2.254 p2p1
[[email protected] ~]# 
           

这样便可以实现由Route1通过192.168.3.254访问Route2了,操作命令如下所示:

[[email protected] ~]# ping 192.168.3.254
PING 192.168.3.254 (192.168.3.254) 56(84) bytes of data.
64 bytes from 192.168.3.254: icmp_seq=1 ttl=64 time=1.11 ms
64 bytes from 192.168.3.254: icmp_seq=2 ttl=64 time=0.127 ms
64 bytes from 192.168.3.254: icmp_seq=3 ttl=64 time=0.145 ms
64 bytes from 192.168.3.254: icmp_seq=4 ttl=64 time=0.144 ms

--- 192.168.3.254 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.127/0.381/1.111/0.421 ms
[[email protected] ~]# 
           

Step6:IP伪装配置(为内网IP做端口映射)

如此,虽然我们这三个内网可以互通网络,但是我们还是不能在这A、B、C网段内上网,操作命令如下所示: Route1:

[[email protected] ~]# ping www.baidu.com
ping: unknown host www.baidu.com
[[email protected] ~]# 
           

这是为什么呢?因为如果我们内网的IP发送数据到外网,但是我们的IP并不合法,一旦进入外网,IP信息会被过滤掉,自然不能上网了,如此,我们需要为内网的IP地址做个端口映射,即使A网段的IP使用192.168.2.254这个IP上网,而使B网段和C网段内的IP使用210.28.164.11这个IP上网,其实192.168.2.254也属于B网段,因此根本上是使A、B、C这三个网段可以使用210.28.164.11这个IP端口上网。 在Route1中,我们可以通过NAT防火墙进行IP地址伪装,操作命令如下所示:

[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
[[email protected] ~]# service iptables save
将当前规则保存到 /etc/sysconfig/iptables:[确定]
[[email protected] ~]# service iptables restart
清除防火墙规则:[确定]
把 chains 设置为 ACCEPT 策略:nat filter [确定]
正在卸载 Iiptables 模块:[确定]
应用 iptables 防火墙规则:[确定]
载入额外 iptables 模块:ip_conntrack_netbios_ns [确定]
[[email protected] ~]# 
           

类似的,在Route2中,我们也可以通过NAT防火墙进行IP地址伪装,操作命令如下所示:

[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o em1 -j MASQUERADE
[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.3.0/24 -o em1 -j MASQUERADE
[[email protected] ~]# service iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:[确定]
[[email protected] ~]# service iptables restart
Restarting iptables (via systemctl):  [确定]
[[email protected] ~]# 
           

这些步骤操作完成之后,我们就可以在客户端192.168.1.5、192.168.2.5和192.168.3.5三台机器上上网了,而且A、B、C这三个网段也可以互通了。

step7:测试

下面是在Route1(利用B网段中192.168.2.254这个IP上网,而B网段又利用210.28.164.11这个IP上网)中的操作:

[[email protected] ~]# ping www.baidu.com
PING www.a.shifen.com (119.75.218.45) 56(84) bytes of data.
64 bytes from 119.75.218.45: icmp_seq=1 ttl=49 time=26.0 ms
64 bytes from 119.75.218.45: icmp_seq=2 ttl=49 time=26.0 ms
64 bytes from 119.75.218.45: icmp_seq=3 ttl=49 time=26.2 ms
64 bytes from 119.75.218.45: icmp_seq=4 ttl=49 time=25.9 ms
64 bytes from 119.75.218.45: icmp_seq=5 ttl=49 time=26.0 ms
64 bytes from 119.75.218.45: icmp_seq=6 ttl=49 time=26.2 ms
64 bytes from 119.75.218.45: icmp_seq=7 ttl=49 time=26.0 ms

--- www.a.shifen.com ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 5999ms
rtt min/avg/max/mdev = 25.966/26.084/26.228/0.214 ms
[[email protected] ~]# traceroute www.baidu.com
traceroute to www.baidu.com (119.75.217.56), 30 hops max, 40 byte packets
 1  192.168.2.253 (192.168.2.253)  0.146 ms  0.085 ms  0.082 ms
 2   (210.28.164.254)  1.111 ms  1.108 ms  1.112 ms
 3  172.18.32.254 (172.18.32.254)  2.851 ms  2.834 ms  2.809 ms
 4  10.29.0.201 (10.29.0.201)  0.784 ms  0.781 ms  0.831 ms
 5  * * *
 6  202.119.128.229 (202.119.128.229)  8.992 ms  9.452 ms  9.933 ms
 7  202.119.128.2 (202.119.128.2)  8.089 ms  8.113 ms  8.153 ms
 8  zhz0.cernet.net (202.112.38.113)  8.257 ms  8.264 ms  8.278 ms
 9  202.112.53.25 (202.112.53.25)  8.397 ms  8.733 ms  8.794 ms
10  202.112.62.57 (202.112.62.57)  31.151 ms  31.138 ms  31.244 ms
11  202.112.62.242 (202.112.62.242)  25.516 ms  25.557 ms  25.639 ms
12  202.112.6.54 (202.112.6.54)  26.034 ms !X * *
[[email protected] ~]# 
           

由以上的信息可以知道Route1可以上网。

下面是我在A网段中的一台IP地址为192.168.1.5,网关为 192.168.1.250的PC机中的操作:

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\jiya>ipconfig

Windows IP Configuration


Ethernet adapter VMware Network Adapter VMnet8:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.11.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :

Ethernet adapter VMware Network Adapter VMnet1:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.110.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.1.5
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.1.250

C:\Documents and Settings\jiya>ping www.qq.com

Pinging www.qq.com [125.39.127.22] with 32 bytes of data:

Reply from 125.39.127.22: bytes=32 time=134ms TTL=46
Reply from 125.39.127.22: bytes=32 time=139ms TTL=46
Reply from 125.39.127.22: bytes=32 time=137ms TTL=46

Ping statistics for 125.39.127.22:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 134ms, Maximum = 139ms, Average = 136ms
Control-C
^C
C:\Documents and Settings\jiya>tracert www.qq.com

Tracing route to www.qq.com [125.39.127.22]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.1.250
  2    <1 ms    <1 ms    <1 ms  192.168.2.253
  3     1 ms     2 ms     1 ms  210.28.164.254
  4    <1 ms    <1 ms    <1 ms  172.18.32.254
  5     7 ms    <1 ms    <1 ms  10.29.0.201
  6     *        *        *     Request timed out.
  7     8 ms     8 ms     8 ms  202.119.128.229
  8     8 ms     8 ms     8 ms  202.119.128.2
  9     8 ms     8 ms     8 ms  zhz0.cernet.net [202.112.38.113]
 10     8 ms     8 ms     8 ms  202.112.53.25
 11    14 ms    19 ms    14 ms  202.112.36.38
 12    14 ms    14 ms    13 ms  202.112.53.202
 13   139 ms   141 ms   142 ms  219.158.34.121
 14   141 ms   139 ms   139 ms  219.158.5.229
 15    31 ms    31 ms    31 ms  219.158.96.49
 16   152 ms   151 ms   155 ms  219.158.13.250
 17   157 ms   157 ms   156 ms  202.99.66.106
 18   132 ms   134 ms   133 ms  125.39.81.134
 19   138 ms   134 ms   134 ms  125.39.127.22

Trace complete.

C:\Documents and Settings\jiya>
           

由以上的信息可以知道A网段中的PC可以正常上网。

下面是我在B网段中的一台IP地址为192.168.2.5,网关为 192.168.2.253的PC机中的操作:

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Admin>ipconfig

Windows IP Configuration


Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.2.5
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.2.253

C:\Documents and Settings\Admin>ping www.baidu.com

Pinging www.a.shifen.com [119.75.218.45] with 32 bytes of data:

Reply from 119.75.218.45: bytes=32 time=26ms TTL=49
Reply from 119.75.218.45: bytes=32 time=26ms TTL=49
Reply from 119.75.218.45: bytes=32 time=26ms TTL=49

Ping statistics for 119.75.218.45:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 26ms, Maximum = 26ms, Average = 26ms
Control-C
^C
C:\Documents and Settings\Admin>tracert www.baidu.com

Tracing route to www.a.shifen.com [119.75.218.45]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.2.253
  2     1 ms     1 ms     1 ms  210.28.164.254
  3    <1 ms    <1 ms    <1 ms  172.18.32.254
  4    <1 ms    <1 ms    <1 ms  10.29.0.201
  5     *        *        *     Request timed out.
  6     8 ms     8 ms     8 ms  202.119.128.229
  7     8 ms     8 ms     8 ms  202.119.128.2
  8     8 ms     8 ms     8 ms  202.112.38.109
  9     8 ms     8 ms     8 ms  202.112.53.25
 10    30 ms    30 ms    30 ms  bj-11-p11-1.cernet.net [202.112.46.5]
 11     *       25 ms     *     cd1.cernet.net [202.112.53.74]
 12    25 ms    26 ms    25 ms  202.112.6.58
 13    31 ms    31 ms    31 ms  192.168.0.5
 14    26 ms    26 ms    26 ms  10.65.190.131
 15    26 ms    26 ms    26 ms  119.75.218.45

Trace complete.

C:\Documents and Settings\Admin>
           

下面是我在B网段中的一台IP地址为192.168.2.5,网关为 192.168.2.254的PC机中的操作:

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Admin>ipconfig

Windows IP Configuration


Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.2.5
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.2.254

C:\Documents and Settings\Admin>ping www.baidu.com

Pinging www.a.shifen.com [119.75.218.45] with 32 bytes of data:

Reply from 119.75.218.45: bytes=32 time=26ms TTL=49
Reply from 119.75.218.45: bytes=32 time=26ms TTL=49
Reply from 119.75.218.45: bytes=32 time=26ms TTL=49

Ping statistics for 119.75.218.45:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 26ms, Maximum = 26ms, Average = 26ms
Control-C
^C
C:\Documents and Settings\Admin>tracert www.baidu.com

Tracing route to www.a.shifen.com [119.75.218.45]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.2.254
  2    <1 ms    <1 ms     *     192.168.2.253
  3   450 ms     *        *     210.28.164.254
  4   191 ms    96 ms   374 ms  172.18.32.254
  5     *      113 ms    35 ms  10.29.0.201
  6     *        *        *     Request timed out.
  7   347 ms   471 ms   638 ms  202.119.128.229
  8   278 ms   568 ms    71 ms  202.119.128.2
  9   427 ms   343 ms     *     202.112.38.109
 10     *        *        *     Request timed out.
 11   708 ms     *      617 ms  bj-11-p11-1.cernet.net [202.112.46.5]
 12     *        *        *     Request timed out.
 13   797 ms   509 ms   308 ms  202.112.6.58
 14   569 ms   339 ms   376 ms  192.168.0.5
 15   952 ms   464 ms   874 ms  10.65.190.131
 16   214 ms   579 ms   550 ms  119.75.218.45

Trace complete.

C:\Documents and Settings\Admin>
           

由以上的信息我们可以知道在B网段中,无论我们使用192.168.2.253做网关,还是用192.168.2.254做网关,这个网段内的PC机均可以正常上网。只是用192.168.2.254做网段的机器上网的话需要多一次路由。

下面是我在C网段中的一台IP地址为192.168.3.5,网关为 192.168.3.254的PC机中的操作:

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Admin>ipconfig

Windows IP Configuration


Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.3.5
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.3.254

C:\Documents and Settings\Admin>ping www.baidu.com

Pinging www.a.shifen.com [119.75.218.45] with 32 bytes of data:

Reply from 119.75.218.45: bytes=32 time=395ms TTL=49
Reply from 119.75.218.45: bytes=32 time=389ms TTL=49
Reply from 119.75.218.45: bytes=32 time=520ms TTL=49

Ping statistics for 119.75.218.45:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 389ms, Maximum = 520ms, Average = 434ms
Control-C
^C
C:\Documents and Settings\Admin>tracert www.baidu.com

Tracing route to www.a.shifen.com [119.75.218.45]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  192.168.3.254
  2   141 ms   416 ms   828 ms  210.28.164.254
  3   326 ms   314 ms   196 ms  172.18.32.254
  4   235 ms   145 ms     *     10.29.0.201
  5     *        *        *     Request timed out.
  6   414 ms   551 ms     *     202.119.128.229
  7   289 ms   246 ms   216 ms  202.119.128.2
  8     *      404 ms   705 ms  202.112.38.109
  9     *      407 ms   419 ms  202.112.53.25
 10   180 ms   209 ms   281 ms  bj-11-p11-1.cernet.net [202.112.46.5]
 11     *        *      354 ms  cd1.cernet.net [202.112.53.74]
 12     *      298 ms   555 ms  202.112.6.58
 13    35 ms    37 ms    31 ms  192.168.0.5
 14   153 ms   144 ms   181 ms  10.65.190.131
 15     *        *       27 ms  119.75.218.45

Trace complete.

C:\Documents and Settings\Admin>
           

由以上的信息可以知道C网段中的PC可以正常上网。 至此,我们发现A、B、C这三个网段之间的机器可以互通网络,且可以通过指定网关上网。 下面是本人配置成功后的路由表信息,仅供参考: Route1:

[[email protected] ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     192.168.2.253   255.255.255.0   UG    0      0        0 eth0
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     192.168.2.253   255.255.255.0   UG    0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         192.168.2.253   0.0.0.0         UG    0      0        0 eth0
[[email protected] ~]# 
           

Route2:

[[email protected] ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         210.28.164.254  0.0.0.0         UG    0      0        0 em1
192.168.1.0     192.168.2.254   255.255.255.0   UG    0      0        0 p2p1
192.168.2.0     0.0.0.0         255.255.255.0   U     1      0        0 p2p1
192.168.3.0     0.0.0.0         255.255.255.0   U     1      0        0 p1p1
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 p1p1
210.28.164.0    0.0.0.0         255.255.255.0   U     1      0        0 em1
[[email protected] ~]# 
           

【版权声明】

如有疑问请咨询本人: http://t.qq.com/shuxiao9058 如需转载本文请注明出处: http://blog.csdn.net/shuxiao9058/article/details/6897955

继续阅读