📄 OS-19-LightVirt.pdf (1255 KB)(暂未托管,后续换成 Google Drive 分享链接)
使用 Network NameSpace
😉 更多IP命令的讲解?
😉 其实很简单啦
创建
$ ip netns add netns1
删除
ip netns delete netns1
列出
ip netns list
指定Network NameSpace执行命令
ip netns exec ns1 ip a a 10.1.1.2/24 dev lo
分配接口到另外的NS中
ip link set p11 netns net1
Small Lab
😉 该实验的环境你可以在这里找到 ——
Base Status
Create New NameSpaces
ip netns add netns
创建新的Network NameSpace的时候,会默认添加lo接口。
$ ip netns exec netns ip -c a1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
Assign IP Address
$ ip netns exec netns ip address add 10.1.1.1/24 dev lo
Make lo Interface UP
$ ip netns exec netns ip link set dev lo up
可能有朋友对<LOOPBACK,UP,LOWER_UP>有疑问,那些是什么含义?
# https://man7.org/linux/man-pages/man7/netdevice.7.htmlSIOCGIFFLAGS, SIOCSIFFLAGS Get or set the active flag word of the device. ifr_flags contains a bit mask of the following values: Device flags IFF_UP Interface is running. IFF_BROADCAST Valid broadcast address set. IFF_DEBUG Internal debugging flag. IFF_LOOPBACK Interface is a loopback interface. IFF_POINTOPOINT Interface is a point-to-point link. IFF_RUNNING Resources allocated. IFF_NOARP No arp protocol, L2 destination address not set. IFF_PROMISC Interface is in promiscuous mode. IFF_NOTRAILERS Avoid use of trailers. IFF_ALLMULTI Receive all multicast packets. IFF_MASTER Master of a load balancing bundle. IFF_SLAVE Slave of a load balancing bundle. IFF_MULTICAST Supports multicast IFF_PORTSEL Is able to select media type via ifmap. IFF_AUTOMEDIA Auto media selection active. IFF_DYNAMIC The addresses are lost when the interface goes down. IFF_LOWER_UP Driver signals L1 up (since Linux 2.6.17) 相当于插上网线 IFF_DORMANT Driver signals dormant (since Linux 2.6.17) IFF_ECHO Echo sent packets (since Linux 2.6.25)
然后更加有疑问,为啥我前面的已经UP了,后面还有 state UNKOWN?(偷懒=。=)
Each interface has at least two different kinds of states:
每个接口至少有两种不同的状态:
Administrative state (whether the interface is enabled). This is just up/down and is controlled by you, or by networking software; after you enable the interface with ip link set eth0 up or ifconfig eth0 up, you can see it indicated as the flag near the beginning of your example.
管理状态 (接口是否启用)。这只是向上/向下,由您或网络软件控制; 在您启用具有ip链接设置eth0 up或ifconfig eth0 up的接口后,您可以看到它在示例开始附近显示为标志 。
Operational state (whether the interface works). Just because it’s enabled, doesn’t mean it can transfer data already – Ethernet needs a cable to be connected; Wi-Fi needs to be associated with an AP; basically it needs a carrier. For physical interfaces this is obviously an external factor and cannot be controlled with commands; it only shows up in the output as <LOWER_UP> if carrier is present.
操作状态 (接口是否工作)。仅仅因为它已启用,并不意味着它已经可以传输数据-以太网需要连接电缆; Wi-fi需要与AP相关联; 基本上它需要运营商。对于物理接口,这显然是一个外部因素,无法通过命令进行控制。如果存在载波,它仅在输出中显示为 <LOWER_UP>。
These are more complex than up/down in reality, when e.g. Wi-Fi networks with WPA-Enterprise do have a carrier after associating, but cannot yet send data until the WPA handshake is performed, so there’s a third level of “operational” state: ‘dormant’.
这些在现实中比上/下更复杂,例如WPA-Enterprise的wi-fi网络在关联后确实有一个运营商,但是在执行WPA握手之前还不能发送数据,因此存在第三级 “操作” 状态: “休眠”。
(For virtual interfaces such as VLANs, there’s yet another, ‘lowerlayerdown’.)
(对于虚拟接口 (例如vlan),还有另一个 “lowerlayerdown’”。)
So “state UNKNOWN” isn’t the administrative “up/down” switch, it’s the operational state that comes from the system itself shows a whole selection of values. This state needs to be provided by the driver – and (at least according to Linux documentation) not all drivers have the necessary bits to set the actual oper state. So if your Ethernet driver hasn’t done so, you will see the default value IF_OPER_UNKNOWN (0).
因此,“状态未知” 不是管理 “上/下” 开关,而是来自系统本身的操作状态显示了整个值选择。此状态需要由驱动程序提供-并且 (至少根据Linux文档) 并非所有驱动程序都具有设置实际操作状态所需的位。因此,如果您的以太网驱动程序没有这样做,您将看到默认值IF_OPER_UNKNOWN (0)。
ip netns exec ns1 ip a a 10.1.1.2/24 dev veth0
ip netns exec ns1 ip l s veth0 up
ip netns exec ns2 ip a a 10.1.1.3/24 dev veth1
ip netns exec ns2 ip l s veth1 up
ip netns exec net1 ifconfig ipvlan1 172.12.1.5/24 up
brctl show
ip netns list
ip netns add net1
ip link add ipvlan1 link ens33 type ipvlan l2
ip link set ipvlan1 netns net1
route -n
ip netns exec net1 route add -net 0.0.0.0/0 gw 172.12.1.2