linux ip_forward简单测试(两台主机,包括namespace的使用)

组网

两台主机:PC0 以及 PC1
PC0以及PC1各有两个网口eth0及eth1,PC0的eth0与PC1的eth0直连,PC1的eth1与PC1的eth1直连。PC1做转发使用。
PC0 eth0 ip: 1.1.1.100/24
eth1 ip: 2.2.2.100/24
PC1 eth0 ip: 1.1.1.200/24
eth1 ip: 2.2.2.200/24

PC0配置如下:

首先PC0的eth0与eth1添加到命令空间中
eth0配置如下:
ip netns add ns1                //添加一个namespace ns1
ip link set eth0 netns ns1      //将eth0加入到ns1 namespace
ip netns exec ns1 ifconfig -a
ip netns exec ns1 ifconfig eth0 1.1.1.100/24
ip netns exec ns1 route add -net 2.2.2.0/24 gw 1.1.1.200 //eth0添加2.2.2.0网段路由,并添加gw,gw为网线直连ip

namespace删除操作如下:
ip netns exec ns1 ip link set eth0 netns 1
ip netns del ns1

eth1配置如下:
ip netns add ns2                //添加一个namespace ns2
ip link set eth1 netns ns2      //将eth0加入到ns2 namespace
ip netns exec ns2 ifconfig -a
ip netns exec ns2 ifconfig eth1 2.2.2.100/24
ip netns exec ns2 route add -net 1.1.1.0/24 gw 2.2.2.200 //eth0添加1.1.1.0网段路由,并添加gw,gw为网线直连ip

PC1配置如下:

echo 1 > /proc/sys/net/ipv4/ip_forward
ifconfig eth0 1.1.1.200/24
ifconeth eth1 2.2.2.200/24

全部准备工作完成,可以测试了,
PC0的eth0去ping eth1看是否ping通。

ip netns exec ns1 ping 2.2.2.100
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值