network namespace问题小结

场景描述:创建一个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,否则导致网卡丢失(物理网卡和虚拟网卡)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Network Namespace是Linux内核提供的功能,用于实现网络虚拟化。它可以创建多个隔离的网络空间,每个空间都有独立的网络栈信息,使得虚拟机或容器在运行时仿佛处于独立的网络环境中。\[1\]通过创建命名的network namespace文件,并将其与进程的/proc/self/ns/net文件绑定,可以实现对容器的网络命名空间进行查看和管理。\[2\]Network Namespace的作用是隔离Linux系统的网络资源,包括设备、IP地址、端口、路由表、防火墙规则等。每个network namespace都有自己独立的网络设备和网络配置信息。在跨network namespace之间进行通信时,常常使用veth pair(虚拟以太网卡对)来连接不同的namespace。\[3\] #### 引用[.reference_title] - *1* [ip netns的使用及network namespace 简介](https://blog.csdn.net/hbuxiaofei/article/details/107116064)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [理解linux network namespace](https://blog.csdn.net/u014634338/article/details/119343985)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Network Namespace与ipip](https://blog.csdn.net/weixin_43394724/article/details/121577473)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值