Bài gốc: Network Namespace and Virtual Ethernet (VETH) — tác giả BaconYao, lấy qua Notion public API.
Key Takeaway
Introduction of Linux Namespace
Linux namespaces are a core component of containerization technology. They provide resource isolation between processes and are widely used in containerization, virtualization, and security scenarios. By dividing system resources into separate namespaces, processes are restricted to accessing only the resources within their own namespace, preventing direct interference with resources in other namespaces. The following are the current types of namespaces:-
Network Namespace
- Isolates network resources, including network interfaces, routing tables, firewall rules, etc.
- Use case: Each container can have its own networking environment, such as a unique IP address and ports.
- Example: Containers can run independent web servers bound to the same port (e.g., 80) without conflict.
-
UTS Namespace (UNIX Timesharing System)
- Isolates hostname and NIS domain name.
- Use case: Containers can have their own hostnames, separate from the host or other containers.
-
Example: The
hostnamecommand inside a container returns a unique hostname.
-
User Namespace
- Isolates user and group ID mappings. It allows processes to run as root within a namespace while being mapped to an unprivileged user on the host.
- Use case: Enhances container security by preventing root privileges inside the container from affecting the host.
- Example: UID 0 (root) inside the container may be mapped to a non-root UID on the host.
-
IPC Namespace (Inter-Process Communication)
- Isolates IPC resources such as message queues, shared memory, and semaphores.
- Use case: Ensures that IPC resources used within a container do not interfere with the host or other containers.
-
Cgroup Namespace
- Isolates the view of control groups (cgroups), so that processes only see the cgroups within their namespace.
- Use case: Restricts access to cgroup structures inside containers, improving resource management and isolation.
-
Time Namespace
- Isolates system time and monotonic time.
- Use case: Allows processes in containers to perceive different time settings (a newer feature introduced in Linux 5.6).
Network Namespace
A Network Namespace isolates the following network-related resources:-
Network Interfaces: Such as
eth0,lo(loopback interface). Each namespace can have its own set of network interfaces. - IP Address: Each namespace can be assigned a unique IP address, supporting both IPv4 and IPv6.
- Routing Table: Each namespace has its own routing rules that determine how packets are forwarded.
- Firewall Rules (iptables/netfilter): Each namespace can define its own firewall policies independently.
- Protocol Stack: Separate instances of network protocols like TCP, UDP, etc.
- Port Space: Each namespace has an independent port range, allowing the reuse of the same port numbers (e.g., multiple containers binding to port 80 without conflict).
Prepare the Experiment Environment
Here’s the vagrant file, you can simply spin up the virtual machine via$ vagrant up and ssh into it via $ vagrant ssh
Vagrantfile
Vagrantfile
Basic Commands and Demonstration
ip netns command
-
ip netns ls: list all namespaces -
ip netns add <name>: Add a namespace -
ip netns delete <name>: Delete a namespace -
ip netns exec <name>: Run arbitrary commands inside a given network namespace
Create namespace
We can create a network namespace named
ns1 like left hand side.However, we may encounter a mkdir … error like the one shown on the left.Why does this happen?/var/run/netns directory. If this directory does not exist, the ip netns add command will try to create the /run/netns directory when you attempt to create a namespace. However, due to insufficient permissions, it fails to create this directory.
ns1 and ns2, after elevating privileges with sudo.
Operate and Inspect the Namespace
The left side of the image below shows the network configuration as seen on my virtual machine, while the right side shows the network configuration inside thens1 namespace accessed via ip netns exec ns1.
We can observe that in the ns1 namespace, there is only one interface — the lo (loopback) interface — and it is in the DOWN state.
Ping External Networks
We didping 8.8.8.8 both from the host and from within the ns1 network namespace. You can observe that since the ns1 namespace has no active network interfaces enabled, it cannot communicate with external networks.
Docker and Network Namespace
However, do you know why can’t we see the container’s network namespace from the host using theip netns command?
The image below shows two containers, each with its own network namespace present in the /var/run/docker/netns directory.
We can create symbolic links for the containers’ network namespaces in /var/run/netns. By doing so, we can also view and exec the Docker containers’ namespaces using the ip netns ... commands
Introduction to Virtual Ethernet (veth)
Core Concepts of VETH
-
Always Created in Pairs: VETH interfaces always come in pairs (e.g.,
veth0andveth1), like a virtual network cable connecting two endpoints that can reside in different network namespaces or devices. -
Data Transmission: When data is sent from one end (e.g.,
veth0), it is immediately received at the other end (veth1), simulating a real network connection. -
Use Cases:
-
Container Networking: In container technologies, VETH pairs connect a container’s network namespace to the host’s namespace or a bridge (like Docker’s
docker0bridge), enabling communication between containers and external networks. - Network Isolation: By placing one end of the VETH pair in a separate network namespace, network isolation is achieved while allowing controlled data flow between namespaces.
- Virtual Machines or Network Simulation: Used to simulate complex network topologies.
-
Container Networking: In container technologies, VETH pairs connect a container’s network namespace to the host’s namespace or a bridge (like Docker’s
How It Works
-
Create a VETH Pair: Use a command like
$ ip link add veth0 type veth peer name veth1to create a pair of linked VETH interfaces. -
Assign to Network Namespaces: Move one end (e.g.,
veth1) into a network namespace (such as a container), while keeping the other end (veth0) in the host namespace or connecting it to a bridge. - Configure IP and Routing: Assign IP addresses and routing rules to ensure proper data flow.
- Data Forwarding: Packets sent to one VETH interface appear directly on the peer interface, enabling efficient virtual network communication.
Experiment
The following three labs are connected in sequence. Ultimately, they will build the setup shown below.Experiment 1 – Creating a veth Pair Using the ip Command

Using the
ip link command, we let the Linux kernel create a pair of virtual Ethernet interfaces, named veth1 and veth2.You can observe that two interfaces are created: veth1@veth2 and veth2@veth1. The name after the @ symbol indicates their peer interface, showing that they form a connected veth pair, simulating a virtual network cable.
Try to bring up the
veth1 interface. You will observe that the veth1@veth2 interface changes from DOWN to UP.Experiment 2 – Communication Between Host and a Single Network Namespace

-
Step 1: Create a network namespace named
ns1. -
Step 2: Attach one of the virtual interfaces,
veth2, to thens1network namespace and rename it toeth1000. -
Step 3: Enter the
ns1namespace and check if the interface has been successfully attached.

After successfully attaching the virtual interface
veth2 to ns1, we perform the following steps:-
Step 1: Bring up the
eth1000interface. -
Step 2: Assign an IP address. Here, we assign the IP address
10.66.77.24/24to the interface.

After attaching
veth2 to the ns1 network namespace (shown on the left side of the left image), if we check the network interfaces on the host again, we find that veth2 is no longer present—only veth1 remains (shown on the right side of the left image).This is because the ip command only displays interfaces within the current network namespace. Since veth2 has been moved out of the host’s default namespace and into the ns1 network namespace, it is expected that it won’t be visible from the host.
Next, we assign the IP address
10.66.77.25/24 to the host’s veth1 interface (shown on the right side of the left image).On the host side, we use the tcpdump command to monitor ICMP packets on this interface. Meanwhile, inside the ns1 network namespace (left side of the left image), we send packets to the veth1 interface by using the ping command.The result shows that the host and ns1 can communicate with each other through the VETH pair: veth1 on the host and veth2 (renamed to eth1000) in ns1.Experiment 3 – Point-to-Point Communication Between Network Namespaces

Previously, we established communication between
ns1 and the host.Next, we create another network namespace called ns2 and attach veth1 to ns2, renaming it to eth99. This setup enables successful communication between ns1 and ns2 via the virtual Ethernet pair.Conclusion
Network namespaces provide isolated network environments within a single Linux host, allowing processes to have separate network stacks including interfaces, IP addresses, and routing tables. This isolation is fundamental for containerization and advanced network segmentation.
Virtual Ethernet (veth) pairs act as virtual cables connecting two network namespaces. By linking namespaces through veth pairs, isolated network environments can communicate securely and efficiently. Veth pairs are essential for enabling container-to-container or container-to-host networking, making them a key building block in modern Linux networking and container technologies.