gcc capability.c -lcap -o capability # 需要使用root执行,因为普通用户不能给进程设置能力 sudo ./capability # 输出如下内容 uid=0 euid=0 gid=0 The process 5044 was give capabilities = cap_setgid,cap_setuid,cap_setpcap,cap_net_bind_service,cap_net_raw+eip dropping caps The process 5044 was give capabilities =
Mount Namespace 是 Linux 下的一种挂载隔离机制,可以实现不同的进程拥有独立的挂载视图,各挂载点之间相互不受影响。
mount namespace 基本用法
Linux 启动时会创建一个默认的 mount namespace,在使用 clone() 或者 unshare() 系统调用通过 CLONE_NEWNS 会创建出新的 mount namespace。因为是第一个加入到 Linux 内核的中的 namespace,CLONE_NEWNS 的取名并不是太合理。
在宿主机上使用docker run -d -v /test ubuntu sleep 10000创建新的容器,并创建docker容器中的挂载点/test,该命令会自动在容器中创建目录,并将宿主机上指定目录下的随机目录挂载到容器中的/test目录下。
可在宿主机上通过如下命令查看到volume的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 列出当前docker在使用的所有volume [root@localhost vagrant]# docker volume ls DRIVER VOLUME NAME local 4ad97e6356707b66cd1cacc4a2e223d9c79d11eca26fe12b1becc9dd664fc5c6
[root@localhost software]# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:3e:95 brd ff:ff:ff:ff:ff:ff 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:a5:78:ca brd ff:ff:ff:ff:ff:ff 4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 02:42:a3:75:00:16 brd ff:ff:ff:ff:ff:ff 18: veth71f2650@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default link/ether ca:05:f7:db:6f:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0
# 增加network namespace ns1 [root@localhost software]# ip netns add ns1 [root@localhost software]# ip netns ns1
# 激活namespace ns1中的lo设备 [root@localhost software]# ip netns exec ns1 ip link set dev lo up
# 创建veth pair [root@localhost software]# ip link add veth-ns1 type veth peer name lxcbr0.1 # 多出了lxcbr0.1@veth-ns1和veth-ns1@lxcbr0.1两个设备 # 后面的操作步骤中将lxcbr0.1位于主网络命名空间中,veth-ns1位于ns1命名空间中 [root@localhost software]# ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:3e:95 brd ff:ff:ff:ff:ff:ff 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:a5:78:ca brd ff:ff:ff:ff:ff:ff 4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 02:42:a3:75:00:16 brd ff:ff:ff:ff:ff:ff 18: veth71f2650@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default link/ether ca:05:f7:db:6f:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0 19: lxcbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/ether c6:b7:4d:7f:f8:90 brd ff:ff:ff:ff:ff:ff 20: lxcbr0.1@veth-ns1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether c6:8a:26:3d:ba:de brd ff:ff:ff:ff:ff:ff 21: veth-ns1@lxcbr0.1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether f2:03:22:93:d6:f4 brd ff:ff:ff:ff:ff:ff
# 将设备veth-ns1放入到ns1命名空间中 [root@localhost software]# ip link set veth-ns1 netns ns1 # 可以看到veth-ns1设备在当前命名空间消失了 [root@localhost software]# ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:3e:95 brd ff:ff:ff:ff:ff:ff 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:a5:78:ca brd ff:ff:ff:ff:ff:ff 4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 02:42:a3:75:00:16 brd ff:ff:ff:ff:ff:ff 18: veth71f2650@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default link/ether ca:05:f7:db:6f:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0 19: lxcbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/ether c6:b7:4d:7f:f8:90 brd ff:ff:ff:ff:ff:ff 20: lxcbr0.1@if21: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether c6:8a:26:3d:ba:de brd ff:ff:ff:ff:ff:ff link-netnsid 1 # 同时在命名空间ns1中看到了设备veth-ns1,同时可以看到veth-ns1设备的状态为DOWN [root@localhost software]# ip netns exec ns1 ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 21: veth-ns1@if20: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether f2:03:22:93:d6:f4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
# 将ns1中的veth-ns1设备更名为eth0 [root@localhost software]# ip netns exec ns1 ip link set dev veth-ns1 name eth0 [root@localhost software]# ip netns exec ns1 ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 21: eth0@if20: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether f2:03:22:93:d6:f4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
# 添加一个网桥lxcbr0,类似于docker中的docker0 [root@localhost software]# brctl addbr lxcbr0 [root@localhost software]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.0242a3750016 no veth71f2650 lxcbr0 8000.000000000000 no
# 关闭生成树协议,默认该协议为关闭状态 [root@localhost software]# brctl stp lxcbr0 off [root@localhost software]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.0242a3750016 no veth71f2650 lxcbr0 8000.000000000000 no
# 将veth设备中的其中一个lxcbr0.1添加到网桥lxcbr0上 [root@localhost software]# brctl addif lxcbr0 lxcbr0.1 # 可以看到网桥lxcbr0中已经包含了设备lxcbr0.1 [root@localhost software]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.0242a3750016 no veth71f2650 lxcbr0 8000.c68a263dbade no lxcbr0.1
# 为网络空间ns1增加默认路由规则,出口为网桥ip地址 [root@localhost software]# ip netns exec ns1 ip route add default via 192.168.10.1 [root@localhost software]# ip netns exec ns1 ip route default via 192.168.10.1 dev eth0 192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.11