DOCKER: LINUX NAMESPACE

Posted on Mon, Jul 3, 2023 Linux

What’s the Linux Namespace?

Linux Namespace是 Linux 提供的一种内核级别环境隔离的方法。Namespace 为不同的资源提供了隔离和限制的能力,如 UTSIPCmountPIDnetworkUser 等隔离机制,使得多个进程可以在同一系统上并行运行,彼此隔离互不干扰。

Linux Namespace Types

Linux Namespace supports many types of namespaces, official documentation is here《Namespace in Operation》:

  1. PID Namespace: Provides process ID isolation, allowing each namespace to have its own set of process IDs. Processes in different PID namespaces can have the same PID, but they refer to different processes in the global namespace.
  2. Network Namespace: Provides network isolation, enabling each namespace to have its own network interfaces, IP addresses, routing tables, and network-related resources. Processes in different network namespaces can have separate network stacks.
  3. Mount Namespace: Provides file system isolation, allowing each namespace to have its own set of mount points and file system views. Mount namespaces enable processes to have a private file system hierarchy.
  4. UTS Namespace: Provides isolation for the hostname and domain name. Each UTS namespace can have its own unique hostname and domain name.
  5. IPC Namespace: Provides isolation for interprocess communication (IPC) mechanisms such as System V IPC and POSIX message queues. Processes in different IPC namespaces can have separate IPC resources.
  6. User Namespace: Provides isolation for user and group identifiers (UID and GID). User namespaces allow mapping of different user and group IDs inside the namespace to different IDs in the global namespace.

Linux 提供了一些系统调用函数来操作命名空间,用于创建、管理和操作命名空间。以下是一些常用的操作命名空间的系统调用函数:

clone()系统调用

首先,我们来看一下一个最简单的clone()系统调用的示例: