Linux虚拟网络基础——Router

一 介绍
Linux创建Router并没有像创建Bridge那样,有一个直接的命令brctl,而且它间接的命令也没有,不能创建虚拟路由器,因为它就是路由器(Router)!
不过linux默认没有打开路由转发功能。可以用这个命令验证一下:
[root@centos ~]# cat /proc/sys/net/ipv4/ip_forward
1
如果内容是1:表示打开了Linux的路由转发功能。
如果内容是0:表示没有打开路由转发功能。
这种打开方法,在机器重启后就会失效了。一劳永逸的方法是修改配置文件:“/etc/sysctl.conf”,将
net.ipv4.ip_forward=0修改为1,保存后退出。
下面通过一个测试用例来直观感受一下Router的功能。

二 测试组网
说明
在这个图中,NS1/tap1与NS2/tap2不是在同一个网段,中间需要经过一个路由器进行转发才能互通。图中的Router是一个示意,其实就是Linux开通了路由转发功能。
当我们添加了tap并绑定了IP地址时,Linux会自动生成直连路由

三 实战
# 创建veth pair
[root@centos ~]# ip link add tap1 type veth peer name tap1_peer
[root@centos ~]# ip link add tap2 type veth peer name tap2_peer
#创建namespace
[root@centos ~]# ip netns add ns1
[root@centos ~]# ip netns add ns2
#将tap迁移到namespace
[root@centos ~]# ip link set tap1 netns ns1
[root@centos ~]# ip link set tap2 netns ns2
#配置tap的IP地址
[root@centos ~]# ip addr add local 192.168.100.1/24 dev tap1_peer
[root@centos ~]# ip addr add local 192.168.200.1/24 dev tap2_peer
[root@centos ~]# ip netns exec ns1 ip addr add local 192.168.100.2/24 dev tap1
[root@centos ~]# ip netns exec ns2 ip addr add local 192.168.200.2/24 dev tap2
#将tap设置为up
[root@centos ~]# ip link set tap1_peer up
[root@centos ~]# ip link set tap2_peer up
[root@centos ~]# ip netns exec ns1 ip link set tap1 up
[root@centos ~]# ip netns exec ns2 ip link set tap2 up
#ping测试
[root@centos ~]# ip netns exec ns1 ping 192.168.200.2
connect: Network is unreachable
#查看路由表,ns1并没有到达192.168.200.0/24的路由表项,因此我们需要手动添加
[root@centos ~]# ip netns exec ns1 route -nee
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface    MSS   Window irtt
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 tap1     0     0      0
#为ns1和ns2都添加静态路由,分别到达对方的网段
[root@centos ~]# ip netns exec ns1 route add -net 192.168.200.0 netmask 255.255.255.0 gw 192.168.100.1
[root@centos ~]# ip netns exec ns2 route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.200.1
#再次查看路由表
[root@centos ~]# ip netns exec ns1 route -nee
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface    MSS   Window irtt
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 tap1     0     0      0
192.168.200.0   192.168.100.1   255.255.255.0   UG    0      0        0 tap1     0     0      0
#ping测试
[root@centos ~]# ip netns exec ns1 ping 192.168.200.2
PING 192.168.200.2 (192.168.200.2) 56(84) bytes of data.
64 bytes from 192.168.200.2: icmp_seq=1 ttl=63 time=0.048 ms
64 bytes from 192.168.200.2: icmp_seq=2 ttl=63 time=0.085 ms
^C
--- 192.168.200.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.048/0.066/0.085/0.020 ms



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值