# ip netns exec net1 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 # ip netns exec net1 bash // 在 net1 中打开一个shell终端 # ip addr // 在net1中的shell终端 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 # exit // 退出net1
每个 namespace 在创建的时候会自动创建一个回环接口 lo ,默认不启用,可以通过 ip link set lo up 启用。
3. network namespace 之间的通信
新创建的 namespace 默认不能和主机网络,以及其他 namespace 通信。
可以使用 Linux 提供的 veth pair 来完成通信。下面显示两个 namespace 之间通信的网络拓扑:
3.1 ip link add type veth 创建 veth pair
1 2 3 4 5 6
# ip link add type veth # ip link 3: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 1a:53:39:5a:26:12 brd ff:ff:ff:ff:ff:ff 4: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 46:df:46:1f:bf:d6 brd ff:ff:ff:ff:ff:ff
使用命令 ip link add xxx type veth peer name yyy 指定 veth pair 的名字。
3.2 ip link set xx netns yy 将 veth xx 加入到 namespace yy 中
1 2 3 4 5 6 7 8
# ip link set veth0 netns net0 # ip link set veth1 netns net1 # # ip netns exec net0 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 10: veth0@if11: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 1a:53:39:5a:26:12 brd ff:ff:ff:ff:ff:ff link-netnsid 1
3.3 给 veth pair 配上 ip 地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# ip netns exec net0 ip link set veth0 up # ip netns exec net0 ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 10: veth0@if11: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000 link/ether 1a:53:39:5a:26:12 brd ff:ff:ff:ff:ff:ff link-netnsid 1 # ip netns exec net0 ip addr add 10.1.1.1/24 dev veth0 # ip netns exec net0 ip route 10.1.1.0/24 dev veth0 proto kernel scope link src 10.1.1.1 linkdown # # ip netns exec net1 ip link set veth1 up # ip netns exec net1 ip addr add 10.1.1.2/24 dev veth1
可以看到,在配完 ip 之后,还自动生成了对应的路由表信息。
3.4. ping 测试两个 namespace 的连通性
1 2 3 4 5 6
# ip netns exec net0 ping 10.1.1.2 PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data. 64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=0.069 ms 64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.054 ms 64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=0.053 ms 64 bytes from 10.1.1.2: icmp_seq=4 ttl=64 time=0.053 ms
通常 Linux 中和 bridge 有关的操作是使用命令 brctl (yum install -y bridge-utils ) 。但为了前后照应,这里都用 ip 相关的命令来操作。
1 2 3 4 5 6 7
// 建立一个 bridge # ip link add br0 type bridge # ip link set dev br0 up 9: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000 link/ether 42:55:ed:eb:a0:07 brd ff:ff:ff:ff:ff:ff inet6 fe80::4055:edff:feeb:a007/64 scope link valid_lft forever preferred_lft forever
4.2 创建 veth pair
1 2 3 4
//(1)创建 3 个 veth pair # ip link add type veth # ip link add type veth # ip link add type veth
// (1)配置第 1 个 net0 # ip link set dev veth1 netns net0 # ip netns exec net0 ip link set dev veth1 name eth0 # ip netns exec net0 ip addr add 10.0.1.1/24 dev eth0 # ip netns exec net0 ip link set dev eth0 up # # ip link set dev veth0 master br0 # ip link set dev veth0 up
// (2)配置第 2 个 net1 # ip link set dev veth3 netns net1 # ip netns exec net1 ip link set dev veth3 name eth0 # ip netns exec net1 ip addr add 10.0.1.2/24 dev eth0 # ip netns exec net1 ip link set dev eth0 up # # ip link set dev veth2 master br0 # ip link set dev veth2 up
// (3)配置第 3 个 net2 # ip link set dev veth5 netns net2 # ip netns exec net2 ip link set dev veth5 name eth0 # ip netns exec net2 ip addr add 10.0.1.3/24 dev eth0 # ip netns exec net2 ip link set dev eth0 up # # ip link set dev veth4 master br0 # ip link set dev veth4 up