Namespace名称 |
隔离内容 |
内核版本 |
mount(mnt) | 隔离挂载点(文件系统) | 2.4.19 |
Process ID (PID) | 隔离进程 ID | 2.6.24 |
Network (net) | 隔离网络设备、网络栈、端口号等 | 2.6.29 |
Interprocess Communication (IPC) | 隔离信号量、消息队列和共享内存 | 2.6.19 |
UTS Namespace(UTS) | 隔离主机名和域名 | 2.6.19 |
User Namespace (user) | 隔离用户和用户组 | 3.8 |
Control groupNamespace (Cgroup) | 隔离 Cgroups 根目录 | 4.6 |
Time Namespace | 隔离系统时间 | 5.6 |
(1)Mount Namespace
使用以下命令创建一个 bash 进程并且新建一个 mount Namespace:
[root@weijing ~]# unshare --mount --fork /bin/bash
[root@weijing ~]# mkdir /tmp/tmpfs
[root@weijing ~]# mount -t tmpfs -o size=20m tmpfs /tmp/tmpfs
[root@weijing ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vda1 500G 1.4G 499G 1% / devtmpfs 16G 0 16G 0% /dev tmpfs 16G 0 16G 0% /dev/shm tmpfs 16G 0 16G 0% /sys/fs/cgroup tmpfs 16G 57M 16G 1% /run tmpfs 3.2G 0 3.2G 0% /run/user/1000 tmpfs 20M 0 20M 0% /tmp/tmpfs
[root@weijing ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 16G 0 16G 0% /dev tmpfs 16G 0 16G 0% /dev/shm tmpfs 16G 57M 16G 1% /run tmpfs 16G 0 16G 0% /sys/fs/cgroup /dev/vda1 500G 1.4G 499G 1% / tmpfs 3.2G 0 3.2G 0% /run/user/1000
[root@weijing ~]# ls -l /proc/self/ns/ total 0 lrwxrwxrwx. 1 root root 0 Sep 4 08:20 ipc -> ipc:[4026531839] lrwxrwxrwx. 1 root root 0 Sep 4 08:20 mnt -> mnt:[4026532239] lrwxrwxrwx. 1 root root 0 Sep 4 08:20 net -> net:[4026531956] lrwxrwxrwx. 1 root root 0 Sep 4 08:20 pid -> pid:[4026531836] lrwxrwxrwx. 1 root root 0 Sep 4 08:20 user -> user:[4026531837] lrwxrwxrwx. 1 root root 0 Sep 4 08:20 uts -> uts:[4026531838]
[root@weijing ~]# ls -l /proc/self/ns/
total 0
lrwxrwxrwx. 1 root root 0 Sep 4 08:20 ipc -> ipc:[4026531839]
lrwxrwxrwx. 1 root root 0 Sep 4 08:20 mnt -> mnt:[4026531840]
lrwxrwxrwx. 1 root root 0 Sep 4 08:20 net -> net:[4026531956]
lrwxrwxrwx. 1 root root 0 Sep 4 08:20 pid -> pid:[4026531836]
lrwxrwxrwx. 1 root root 0 Sep 4 08:20 user -> user:[4026531837]
lrwxrwxrwx. 1 root root 0 Sep 4 08:20 uts -> uts:[4026531838]]
(2)PID Namespace
[root@weijing ~]# unshare --pid --fork --mount-proc /bin/bash
[root@weijing ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 115544 2004 pts/0 S 10:57 0:00 bash root 10 0.0 0.0 155444 1764 pts/0 R+ 10:59 0:00 ps aux
(3)UTS Namespace
[root@weijing ~]# unshare --uts --fork /bin/bash
[root@weijing ~]# hostname -b weijingdocker
[root@weijing ~]# hostname
weijingdocker
[root@weijing ~]# hostname
weijing
(4)IPC Namespace
[root@weijing ~]# unshare --ipc --fork /bin/bash
- ipcs -q 命令:用来查看系统间通信队列列表。
- ipcmk -Q 命令:用来创建系统间通信队列。
[root@weijing ~]# ipcs -q
------ Message Queues --------
key msqid owner perms used-bytes messages
[root@weijing ~]# ipcmk -Q
Message queue id: 0
[root@weijing ~]# ipcs -q
------ Message Queues --------
key msqid owner perms used-bytes messages
0x73682a32 0 root 644 0 0
[root@weijing ~]# ipcs -q
------ Message Queues --------
key msqid owner perms used-bytes messages
(5)User Namespace
[root@weijing ~]# unshare --user -r /bin/bash
[root@weijing ~]# id
uid=0(root) gid=0(root) groups=0(root),65534(nfsnobody) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@weijing ~]# reboot
Failed to open /dev/initctl: Permission denied
Failed to talk to init daemon.
(6)Net Namespace
# ip add 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 02:11:b0:14:01:0c brd ff:ff:ff:ff:ff:ff inet 172.20.1.11/24 brd 172.20.1.255 scope global dynamic eth0 valid_lft 86063337sec preferred_lft 86063337sec inet6 fe80::11:b0ff:fe14:10c/64 scope link valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:82:8d:a0:df brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:82ff:fe8d:a0df/64 scope link valid_lft forever preferred_lft forever
[root@weijing ~]# unshare --net --fork /bin/bash
[root@weijing ~]# ip add 1: 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