1. 创建命名空间
ip netns add test_a
ip netns add test_b
2. 开启网络设备
ip netns exec test_a ip link set dev lo up
ip netns exec test_b ip link set dev lo up
3. 创建网卡
ip link add veth0 type veth peer name veth1
ip link set veth1 netns test_a
ip link add veth2 type veth peer name veth1
ip link set veth1 netns test_b
4) 设置IP地址
[root@localhost ~]# ip netns exec test_a ifconfig veth1 10.1.1.1/24 up
[root@localhost ~]# ifconfig veth0 10.1.1.2/24 up
[root@localhost ~]# ping 10.1.1.1
[root@localhost ~]# ip netns exec test_b ifconfig veth1 20.1.1.1/24 up
[root@localhost ~]# ifconfig veth2 20.1.1.2/24 up
5) 为其设置网关
ip netns exec test_a route add default gw 10.1.1.2
ip netns exec test_b route add default gw 20.1.1.2
6) 开启路由转发
[root@localhost ~]# vim /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
开启路由转发
[root@localhost ~]# vim /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1