默认情况下,使用 ip netns 是没有网络 namespace 的,所以 ip netns ls 命令看不到任何输出。
1 2 3 4 5 6 7 8 9 10
vagrant@ubuntu-xenial:~$ ip netns help Usage: ip netns list ip netns add NAME ip netns set NAME NETNSID ip [-all] netns delete [NAME] ip netns identify [PID] ip netns pids NAME ip [-all] netns exec [NAME] cmd ... ip netns monitor ip netns list-id
新创建的 netns 会在/var/run/netns/ 目录中生存对应名称的文件
1 2 3 4 5 6 7 8 9 10 11
vagrant@ubuntu-xenial:~$ sudo ip netns add xiemx1 vagrant@ubuntu-xenial:~$ sudo ip netns add xiemx2 vagrant@ubuntu-xenial:~$ sudo ip netns ls xiemx2 xiemx1 vagrant@ubuntu-xenial:~$ ll /var/run/netns/ total 0 drwxr-xr-x 2 root root 80 Jan 21 03:19 ./ drwxr-xr-x 28 root root 1140 Jan 21 03:19 ../ -r--r--r-- 1 root root 0 Jan 21 03:19 xiemx1 -r--r--r-- 1 root root 0 Jan 21 03:19 xiemx2
由于netns 之间互相都是隔离的,因此要查看对应命名空间的网络设备、路由表就需要使用 ip netns exec <netns name> bash 开启子bash进入对应的命名空间,也可以直接执行命令
vagrant@ubuntu-xenial:~$ sudo ip a 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 inet127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 02:97:71:8a:f0:d8 brd ff:ff:ff:ff:ff:ff inet10.0.2.15/24 brd 10.0.2.255 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::97:71ff:fe8a:f0d8/64 scope link valid_lft forever preferred_lft forever 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:f1:22:f6 brd ff:ff:ff:ff:ff:ff inet10.110.120.65/24 brd 10.110.120.255 scope global enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fef1:22f6/64 scope link valid_lft forever preferred_lft forever 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:da:5a:39:42 brd ff:ff:ff:ff:ff:ff inet172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever vagrant@ubuntu-xenial:~$ sudo ip net exec xiemx1 ip a 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 vagrant@ubuntu-xenial:~$ sudo ip net exec xiemx2 ip a 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
每个 namespace 在创建的时候会自动创建一个 lo ,默认时DOWN状态,如果需要启用记得UP一下:
1 2 3 4 5 6 7 8 9 10 11
vagrant@ubuntu-xenial:~$ sudo ip net exec xiemx1 ip a 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 vagrant@ubuntu-xenial:~$ sudo ip netns exec xiemx1 ip linkset lo up vagrant@ubuntu-xenial:~$ sudo ip netns exec xiemx1 ip a 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
vagrant@ubuntu-xenial:~$ sudo ip netns exec xiemx1 bash root@ubuntu-xenial:~# ip add 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 inet127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever root@ubuntu-xenial:~# ip link add xiemx-veth1 type veth peer name xiemx-veth2 root@ubuntu-xenial:~# ip add 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 inet127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 4: xiemx-veth2@xiemx-veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether a6:76:6f:47:e1:f9 brd ff:ff:ff:ff:ff:ff 5: xiemx-veth1@xiemx-veth2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 42:6a:cb:19:0d:2a brd ff:ff:ff:ff:ff:ff ####如果对名称没有特别要求可以使用默认命令创建,会默认生存veth0/veth1 的设备对 root@ubuntu-xenial:~# ip link add type veth root@ubuntu-xenial:~# ip add 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 inet127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 4: xiemx-veth2@xiemx-veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether a6:76:6f:47:e1:f9 brd ff:ff:ff:ff:ff:ff 5: xiemx-veth1@xiemx-veth2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 42:6a:cb:19:0d:2a brd ff:ff:ff:ff:ff:ff 6: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 3e:72:e3:48:25:69 brd ff:ff:ff:ff:ff:ff 7: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether ea:37:ea:92:a5:c5 brd ff:ff:ff:ff:ff:ff
root@ubuntu-xenial:~# ip netns exec xiemx1 bash root@ubuntu-xenial:~# ip add add 10.0.0.1/24 dev xiemx-veth1 root@ubuntu-xenial:~# ip add add 10.0.0.2/24 dev xiemx-veth2 root@ubuntu-xenial:~# ip add show dev xiemx-veth1 up root@ubuntu-xenial:~# ip add show dev xiemx-veth2 up root@ubuntu-xenial:~# ip add 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 inet127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 4: xiemx-veth2@xiemx-veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether a6:76:6f:47:e1:f9 brd ff:ff:ff:ff:ff:ff inet10.0.0.2/24 scope global xiemx-veth2 valid_lft forever preferred_lft forever inet6 fe80::a476:6fff:fe47:e1f9/64 scope link valid_lft forever preferred_lft forever 5: xiemx-veth1@xiemx-veth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 42:6a:cb:19:0d:2a brd ff:ff:ff:ff:ff:ff inet10.0.0.1/24 scope global xiemx-veth1 valid_lft forever preferred_lft forever inet6 fe80::406a:cbff:fe19:d2a/64 scope link valid_lft forever preferred_lft forever 6: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 3e:72:e3:48:25:69 brd ff:ff:ff:ff:ff:ff 7: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether ea:37:ea:92:a5:c5 brd ff:ff:ff:ff:ff:ff
root@ubuntu-xenial:~# ip route 10.0.0.0/24 dev xiemx-veth1 proto kernel scope link src 10.0.0.1 10.0.0.0/24 dev xiemx-veth2 proto kernel scope link src 10.0.0.2