我以前使用过ip netns exec
命令来修改netns接口。但是,如果没有与 netns 关联的名称(例如:docker),我如何使用命令nsid
结果ip netns list-id
来修改 netns 接口。
答案1
您必须找到所有 nsid 的父进程 ID,我知道的唯一方法是检查/proc/[0-9]*/ns/net
此处提到的所有内容https://serverfault.com/a/900972/205043
# Create netns directory if not exist
mkdir -p /var/run/netns
# Add all process namespaces to netns
for i in /proc/[0-9]*/ns/net; do
ln -s $i /var/run/netns/proc$(echo $i | cut -d/ -f3)
done
# Remove unassociated network namespaces
for i in $(ip netns | grep ^proc | grep -v id); do
rm -f /var/run/netns/${i}
done
# Remove children processes
for i in $(ip netns | grep ^proc | awk 'a[$3]++ {print $1}'); do
rm -f /var/run/netns/${i}
done
# Do some action for each ns (eg. check if there is 1.2.3.4 address exists)
for i in $(ip netns | grep ^proc | awk '{print $1}'); do
echo -n "$i "
ip netns exec $i ip addr | grep -c '1.2.3.4'
done
# Remove added namespaces
rm -f /var/run/netns/proc*
就我而言,新添加的 nsids 如下所示:
ip netns list-id
nsid 0 (iproute2 netns name: proc26847)
nsid 1 (iproute2 netns name: proc29068)
nsid 2 (iproute2 netns name: proc27483)
nsid 3 (iproute2 netns name: proc27644)
nsid 7 (iproute2 netns name: proc30377)
nsid 9 (iproute2 netns name: proc9606)
nsid 10 (iproute2 netns name: proc15587)
nsid 11 (iproute2 netns name: proc28395)
nsid 12 (iproute2 netns name: proc30729)
nsid 13 (iproute2 netns name: proc29702)
nsid 14 (iproute2 netns name: proc30060)
nsid 15 (iproute2 netns name: proc31698)
nsid 16 (iproute2 netns name: proc3309)
nsid 17 (iproute2 netns name: proc28934)
nsid 18 (iproute2 netns name: proc21272)
nsid 19 (iproute2 netns name: proc921)
nsid 21 (iproute2 netns name: proc32718)
nsid 26 (iproute2 netns name: proc5864)