通常來說,Namespace是個通用概念,我初次認識這個概念是在學習C++的時候,面嚮對象語言用它來命名衝突問題。這裏, Namespace是指Linux用來控制一個進程對系統的可見性。最先的應用是chroot, 這裏有介紹淺析chroot。chroot就是經過改變進程的fs_struct
,來限制進程可見的文件系統視圖,默認狀況下每一個進程能夠看到整個文件系統,即根系統/
。最先Namespace應用於文件系統,以致於CLONE_NEWNS指的就是文件系統隔離,參見man namespaces
:linux
Mount namespaces (CLONE_NEWNS) Mount namespaces isolate the set of filesystem mount points, meaning that processes in different mount namespaces can have different views of the filesystem hierarchy.
後來,隨着操做系統級別的虛擬化, 即容器技術,愈來愈多的子系統都支持了Namspace特性,參考man namespaces
:安全
Linux provides the following namespaces: Namespace Constant Isolates IPC CLONE_NEWIPC System V IPC, POSIX message queues Network CLONE_NEWNET Network devices, stacks, ports, etc. Mount CLONE_NEWNS Mount points PID CLONE_NEWPID Process IDs User CLONE_NEWUSER User and group IDs UTS CLONE_NEWUTS Hostname and NIS domain name
在沒有了解Namespace之前,我有這些錯誤的見解:1. Namespace應該就像C++語言裏的命名空間同樣簡單吧; 2. 聽人說,容器/Namespace的隔離效果或安全性並很差,好比top命令仍能看到系統中的其它進程...dom
我真的錯了,在玩了一下Namespace以後,我爲它的強大感到震驚! 這裏強烈推薦一個Namespace系列博客,很容易上手寫點demo代碼,快速體驗一下, Introduce to Linux Namespaceside
我也不知道,可是分析這塊代碼應該分兩步走:1. 分析clone()系統調用代碼; 2. 分析某個子系統的Namespace, 如CLONE_NEWPID
進程pid的隔離。第一步,能夠參考這系列博客fork,clone系統調用分析。 第二步,留待之後瞭解吧。學習