前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「Linux路由实践」之实现物理隔断的两个网段通信【默认路由篇】

「Linux路由实践」之实现物理隔断的两个网段通信【默认路由篇】

作者头像
用户1456517
发布2019-03-05 16:14:56
1.7K0
发布2019-03-05 16:14:56
举报
文章被收录于专栏:芝麻实验室芝麻实验室

环境说明: 同一交互机物理隔断的2个私有网络,每个网络中分别为CentOS 6 * 1, CentOS 7 * 1

好的,现在我们开始~

1.首先我们先看一下2台主机的网络配置信息 先看CentOS 6的:

代码语言:javascript
复制
[root@Centos6 ~]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:40:43:BC
          inet addr:192.168.10.10  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe40:43bc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:84 errors:0 dropped:0 overruns:0 frame:0
          TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:14932 (14.5 KiB)  TX bytes:9438 (9.2 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
[root@Centos6 ~]# ping 192.168.20.20
connect: Network is unreachable
[root@Centos6 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

然后是CentOS 7的:

代码语言:javascript
复制
[root@Centos7 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.20  netmask 255.255.255.0  broadcast 192.168.20.255
        inet6 fe80::20c:29ff:fe03:b85f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:03:b8:5f  txqueuelen 1000  (Ethernet)
        RX packets 61  bytes 6951 (6.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 100  bytes 15953 (15.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 268  bytes 21616 (21.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 268  bytes 21616 (21.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@Centos7 ~]# ping 192.168.10.10
connect: Network is unreachable
[root@Centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33

2.那么,如果要让这2台不同网段的主机能够通信该怎么办呢?

嗯,当然有办法,而且有3种:

  • 新增一台拥有2张网卡的服务器(或在交换机上进行相关设置),分别指向192.168.10.0/24和192.168.20.0/24的网络;
  • 为环境中的Centos 6或7中任意一台服务器添加一张网卡,指向对方网络,然后开启内核转发功能;
  • 使用默认路由;

这里我们以方法三为例 另2种方法参见本博其他文章 此处不赘述

3.由于10.0和20.0这2个私有网络是交换机物理隔断的,所以我们可以通过添加默认网关的方法实现通信(当然还有第二步中提到的其他方法),但不同的是指向的网关为自己同交换机的直连网口地址

代码语言:javascript
复制
[root@Centos6 ~]# route add default gw 192.168.10.10                #Centos 6上
[root@Centos6 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
0.0.0.0         192.168.10.10   0.0.0.0         UG    0      0        0 eth1

[root@Centos7 ~]# route add default gw 192.168.20.20                #Centos 7上
[root@Centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.20.20   0.0.0.0         UG    0      0        0 ens33
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33

4.接下来,我们进行PING测试并在Centos 7上抓包

代码语言:javascript
复制
[root@Centos6 ~]# ping 192.168.20.20                #Centos 6上
PING 192.168.20.20 (192.168.20.20) 56(84) bytes of data.
64 bytes from 192.168.20.20: icmp_seq=1 ttl=64 time=0.646 ms
64 bytes from 192.168.20.20: icmp_seq=2 ttl=64 time=0.251 ms
64 bytes from 192.168.20.20: icmp_seq=3 ttl=64 time=1.37 ms
64 bytes from 192.168.20.20: icmp_seq=4 ttl=64 time=1.19 ms
64 bytes from 192.168.20.20: icmp_seq=5 ttl=64 time=0.269 ms
64 bytes from 192.168.20.20: icmp_seq=6 ttl=64 time=2.11 ms
64 bytes from 192.168.20.20: icmp_seq=7 ttl=64 time=0.652 ms

[root@Centos7 ~]# tcpdump -i ens33 -nn icmp                #Centos 7上
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes
05:16:44.057527 IP 192.168.10.10 > 192.168.20.20: ICMP echo request, id 33029
, seq 149, length 64
05:16:44.057580 IP 192.168.20.20 > 192.168.10.10: ICMP echo reply, id 33029,
seq 149, length 64
05:16:45.058726 IP 192.168.10.10 > 192.168.20.20: ICMP echo request, id 33029
, seq 150, length 64
05:16:45.058806 IP 192.168.20.20 > 192.168.10.10: ICMP echo reply, id 33029,
seq 150, length 64
05:16:46.059739 IP 192.168.10.10 > 192.168.20.20: ICMP echo request, id 33029
, seq 151, length 64
05:16:46.059886 IP 192.168.20.20 > 192.168.10.10: ICMP echo reply, id 33029,
seq 151, length 64
05:16:47.060638 IP 192.168.10.10 > 192.168.20.20: ICMP echo request, id 33029
, seq 152, length 64

5.至此,环境中的两个不同网段的主机已成功互通。


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/08/20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
私有网络
私有网络(Virtual Private Cloud,VPC)是基于腾讯云构建的专属云上网络空间,为您在腾讯云上的资源提供网络服务,不同私有网络间完全逻辑隔离。作为您在云上的专属网络空间,您可以通过软件定义网络的方式管理您的私有网络 VPC,实现 IP 地址、子网、路由表、网络 ACL 、流日志等功能的配置管理。私有网络还支持多种方式连接 Internet,如弹性 IP 、NAT 网关等。同时,您也可以通过 VPN 连接或专线接入连通腾讯云与您本地的数据中心,灵活构建混合云。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档