$ sudo ip netns exec test1 ip a 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 $ sudo ip netns exec test2 ip a 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
现在可以看到我们创建的两个命名空间test1和tes2只有本地的回环端口,也没有本地 ip 127.0.0.1 之类并且状态是DOWN的状态
查看本地的 ip link
1 2 3 4 5 6 7 8 9
$ sudo ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 52:54:00:ad:3b:43 brd ff:ff:ff:ff:ff:ff 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 08:00:27:7e:86:8c brd ff:ff:ff:ff:ff:ff 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT link/ether 02:42:79:71:de:73 brd ff:ff:ff:ff:ff:ff
现在本地只有四个 ip link 下面在宿主机上添加一对 link (vethl-test1 和 veth-test2)
1
$ sudo ip link add veth-test1 type veth peer name veth-test2
接下来我们再次查看本地的 ip link
1 2 3 4 5 6 7 8 9 10 11 12 13
$ sudo ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 52:54:00:ad:3b:43 brd ff:ff:ff:ff:ff:ff 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 08:00:27:7e:86:8c brd ff:ff:ff:ff:ff:ff 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT link/ether 02:42:79:71:de:73 brd ff:ff:ff:ff:ff:ff 9: veth-test2@veth-test1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 9e:44:34:0b:41:39 brd ff:ff:ff:ff:ff:ff 10: veth-test1@veth-test2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 5a:ed:e6:53:00:fb brd ff:ff:ff:ff:ff:ff
序号:9.(veth-test2@veth-test1),10.(veth-test1@veth-test2)就是我们刚添加的一对 link
将 veth-test1 这个接口添加到网络命名空间test1上
1
$ sudo ip link set veth-test1 netns test1
这时候我们会发现本地的 ip link 序号:10 消失了,而我们创建的test1网络命名空间多了 veth-test1,我们来查看test1的 ip link
1 2 3 4 5
$ sudo ip netns exec test1 ip link 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 10: veth-test1@if9: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 5a:ed:e6:53:00:fb brd ff:ff:ff:ff:ff:ff link-netnsid 0
# test1 $ sudo ip netns exec test1 ip link 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 10: veth-test1@if9: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 5a:ed:e6:53:00:fb brd ff:ff:ff:ff:ff:ff link-netnsid 0
# test2 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 9: veth-test2@if10: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 9e:44:34:0b:41:39 brd ff:ff:ff:ff:ff:ff link-netnsid 0
分别为两个接口 veth-test1 和 veth-test2 分配ip地址,然后让其状态由DOWN变为UP (同时也可以把各自的回环端口 lo 状态也设置为 UP,不然到时候ping自己的时候ping不通)
1 2 3 4 5 6 7 8 9 10 11
# 为veth-test1和veth-test2分配IP地址 $ sudo ip netns exec test1 ip addr add 192.168.1.1/24 dev veth-test1 $ sudo ip netns exec test2 ip addr add 192.168.1.2/24 dev veth-test2
# 设置veth-test1和veth-test2的状态由DOWN变为UP $ sudo ip netns exec test1 ip link set dev veth-test1 up $ sudo ip netns exec test2 ip link set dev veth-test2 up
# 设置test1和test2的本地回环端口状态由DOWN变为UP $ sudo ip netns exec test1 ip link set dev lo up $ sudo ip netns exec test2 ip link set dev lo up