(function() { 'use strict'; var eles = document.getElementsByTagName('*'); for (var i = 0; i < eles.length; i++) { eles[i].style.userSelect = 'text'; } })();
# 新增三个netns ip netns add ns0 ip netns add ns1 ip netns add ns2
# 新增两对veth ip link add veth0-ns type veth peer name veth0-br ip link add veth1-ns type veth peer name veth1-br ip link add veth2-ns type veth peer name veth2-br
# 将veth的一端移动到netns中 ip link set veth0-ns netns ns0 ip link set veth1-ns netns ns1 ip link set veth2-ns netns ns2
# 将netns中的本地环回和veth启动并配置IP ip netns exec ns0 ip link set lo up ip netns exec ns0 ip link set veth0-ns up ip netns exec ns0 ip addr add 10.0.0.1/24 dev veth0-ns
ip netns exec ns1 ip link set lo up ip netns exec ns1 ip link set veth1-ns up ip netns exec ns1 ip addr add 10.0.0.2/24 dev veth1-ns
ip netns exec ns2 ip link set lo up ip netns exec ns2 ip link set veth2-ns up ip netns exec ns2 ip addr add 10.0.0.3/24 dev veth2-ns
# 查看网卡 ip netns exec ns0 ip a ip netns exec ns1 ip a ip netns exec ns2 ip a
# 查看路由表 ip netns exec ns0 route -n ip netns exec ns1 route -n ip netns exec ns2 route -n
# 将veth的另一端启动并挂载到网桥上 ip link set veth0-br up ip link set veth1-br up ip link set veth2-br up brctl addif br0 veth0-br brctl addif br0 veth1-br brctl addif br0 veth2-br
# 查询网桥 brctl show
# 添加iptables规则 iptables -A FORWARD -i br0 -j ACCEPT
# 测试网络连通性 ip netns exec ns0 ping 10.0.0.1 ip netns exec ns0 ping 10.0.0.2 ip netns exec ns0 ping 10.0.0.3