组网
两台主机: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