场景描述:创建一个namespace test,并分配一个veth 口到这个namespace里,veth的另一个口在默认namespace里,直接执行ip netns delete test
情况1:当这个ns中没有运行进程(此进程也没有使用这个ns里的veth口),删除后,ns删除,veth 两个口都会删除
情况2:当这个ns中有进程使用ns的veth口,删除后,默认ns中的veth还在,ns删除,ns里面的veth口删除(丢失了)。
通过man ip-netns信息可知
ip [-all] netns delete [ NAME ] - delete the name of a network namespace(s)
If NAME is present in /var/run/netns it is umounted and the mount point is removed. If this is the last user of the network namespace the network namespace will be freed and all physi‐
cal devices will be moved to the default one, otherwise the network namespace persists until it has no more users. ip netns delete may fail if the mount point is in use in another mount
namespace.
If -all option was specified then all the network namespace names will be removed.
It is possible to lose the physical device when it was moved to netns and then this netns was deleted with a running process:
$ ip netns add net0
$ ip link set dev eth0 netns net0
$ ip netns exec net0 SOME_PROCESS_IN_BACKGROUND
$ ip netns del net0
and eth0 will appear in the default netns only after SOME_PROCESS_IN_BACKGROUND will exit or will be killed. To prevent this the processes running in net0 should be killed before delet‐
ing the netns:
$ ip netns pids net0 | xargs kill
$ ip netns del net0
上面描述的是物理网卡的情况,意思是删除ns之前,要先杀掉运行在这个ns中的进程(这个进程可能正在使用ns的网络设备),然后删除ns,否则导致网卡丢失(物理网卡和虚拟网卡)。