Skip to content

Linux Network Namespaces — Network Isolation for Containers and Modern Infrastructure

Network Namespaces are a Linux kernel feature that provides isolated network environments for processes. Each network namespace has its own network interfaces, IP addresses, routing tables, firewall rules, Address Resolution Protocol (ARP) tables, and network devices. Network namespaces are the foundation of Docker, Kubernetes, container networking, virtual networking, and cloud-native infrastructure. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, Site Reliability Engineer (SRE), and Network Engineer should understand network namespaces.


Learning Path

Networking Mastery → Module 9: Linux Networking → Lesson 10

Difficulty: Intermediate

Reading Time: 180 Minutes

Course Progress

Course: Networking Mastery

Module: Linux Networking

Lesson: 10 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand Linux Network Namespaces
  • Create isolated network environments
  • Configure virtual Ethernet (veth) pairs
  • Connect namespaces using Linux bridges
  • Understand container networking
  • Learn Kubernetes networking fundamentals
  • Troubleshoot namespace networking

Prerequisites

Complete:

Basic understanding of:

  • IP Addressing
  • Routing
  • Linux Networking

Why Learn Network Namespaces?

Imagine two applications:

Application A


Uses Port 80
Application B


Also Uses Port 80

Normally:

Port Conflict

With Network Namespaces:

Namespace A


Port 80
Namespace B


Port 80

Each namespace has its own isolated network stack.


What is a Network Namespace?

A Network Namespace is an isolated networking environment.

Each namespace has its own:

  • Network Interfaces
  • IP Addresses
  • Routing Table
  • ARP/Neighbor Table
  • Firewall Rules
  • Socket Table
  • /proc/net Information

Processes inside one namespace cannot directly see the networking resources of another namespace.


Network Isolation

Without namespaces:

Applications


Shared Network Stack

With namespaces:

Application A


Namespace A
Application B


Namespace B

Each application has an independent network stack.


List Network Namespaces

ip netns list

Example output:

dev

test

lab

Create a Namespace

sudo ip netns add lab1

Verify:

ip netns list

Delete a Namespace

sudo ip netns del lab1

Execute a Command Inside a Namespace

sudo ip netns exec lab1 ip addr

This runs the command inside the specified namespace.


Default Interface

A newly created namespace contains only:

lo

Loopback is initially:

DOWN

Bring it up.

sudo ip netns exec lab1 ip link set lo up

Virtual Ethernet (veth)

Namespaces communicate using:

veth Pair

A veth pair behaves like a virtual network cable.

veth0


veth1

Packets entering one interface exit through the other.


Create a veth Pair

sudo ip link add veth0 type veth peer name veth1

Move Interface to Namespace

sudo ip link set veth1 netns lab1

Now:

Host


veth0
Namespace


veth1

Assign IP Addresses

Host:

sudo ip addr add 192.168.100.1/24 dev veth0

Namespace:

sudo ip netns exec lab1 ip addr add 192.168.100.2/24 dev veth1

Bring Interfaces Up

Host:

sudo ip link set veth0 up

Namespace:

sudo ip netns exec lab1 ip link set veth1 up

Test Connectivity

From host:

ping 192.168.100.2

From namespace:

sudo ip netns exec lab1 ping 192.168.100.1

Linux Bridge

Multiple namespaces communicate using a Linux bridge.

Namespace 1


veth


Linux Bridge


veth


Namespace 2

The bridge behaves like a virtual Layer 2 switch.


Create a Bridge

sudo ip link add br0 type bridge

Bring it up.

sudo ip link set br0 up

Connect veth to Bridge

sudo ip link set veth0 master br0

Additional namespaces can connect their veth interfaces to the same bridge.


Routing Between Namespaces

Namespaces can communicate through:

  • Linux Bridge
  • Router
  • Network Address Translation (NAT)
  • Firewall

The host system often acts as the gateway.


Namespace Architecture

Namespace A


veth


Bridge


veth


Namespace B

Each namespace remains isolated while still being able to communicate through configured networking.


Container Networking

Docker creates:

  • Network Namespace
  • veth Pair
  • Linux Bridge
  • NAT Rules

Every container receives:

  • Private IP Address
  • Separate Routing Table
  • Independent Network Stack

Kubernetes Perspective

Every Pod receives:

  • Dedicated Network Namespace
  • Unique IP Address
  • veth Interface

The Container Network Interface (CNI) plugin connects Pods to the cluster network.

Common CNI plugins include:

  • Calico
  • Flannel
  • Cilium
  • Weave Net

Cloud Perspective

Cloud networking concepts resemble namespaces.

Examples include:

  • Virtual Private Clouds (VPCs)
  • Virtual Networks (VNets)
  • Virtual Interfaces
  • Overlay Networks

Although implemented differently, they also provide network isolation and segmentation.


Enterprise Example

Application Platform:

Container


Namespace


veth


Bridge


Host Network


Internet

Each application runs independently while sharing the host kernel.


Linux Perspective

List namespaces.

ip netns list

Create namespace.

sudo ip netns add demo

Run command.

sudo ip netns exec demo ip addr

Delete namespace.

sudo ip netns del demo

Common Namespace Commands

Command Purpose
ip netns list List namespaces
ip netns add Create namespace
ip netns del Delete namespace
ip netns exec Run command in namespace
ip link add Create veth pair
ip link set Move interface
ip addr add Assign IP address
ip link set up Bring interface up

Hands-on Lab

Task 1

Create a namespace.

sudo ip netns add lab1

Task 2

Display namespaces.

ip netns list

Task 3

Create a veth pair.

sudo ip link add veth0 type veth peer name veth1

Task 4

Move one interface into the namespace.

sudo ip link set veth1 netns lab1

Task 5

Assign IP addresses to both interfaces.


Task 6

Bring interfaces online and verify connectivity using ping.


Task 7

Create a Linux bridge and connect two namespaces through it.


Task 8

Draw a networking diagram showing:

  • Host
  • Linux Bridge
  • Two Network Namespaces
  • veth Pairs

Explain how packets travel between the namespaces.


Production Troubleshooting

Problem:

Container

Cannot

Reach

Another Container

Check:

ip netns list

Verify:

ip link

Check:

bridge link

Verify:

ip route

Capture traffic:

tcpdump -i any

Inspect:

  • veth Interfaces
  • Bridge
  • Routes
  • Firewall Rules

Network Namespace vs Virtual Machine

Network Namespace Virtual Machine
Shares Host Kernel Separate Kernel
Lightweight Heavier
Fast Startup Slower Startup
Used by Containers Used for Full Virtualization
Network Isolation Only Complete System Isolation

Common Mistakes

❌ Forgetting to enable the loopback interface.

✅ Bring lo up inside each namespace.


❌ Moving the wrong interface.

✅ Verify interface names before assigning namespaces.


❌ Missing IP configuration.

✅ Assign IP addresses to veth interfaces.


❌ Forgetting bridge configuration.

✅ Attach interfaces to the correct bridge.


❌ Assuming namespaces communicate automatically.

✅ Configure routing or bridging explicitly.


Best Practices

  • Use descriptive namespace names.
  • Always enable the loopback interface.
  • Assign IP addresses systematically.
  • Use Linux bridges for Layer 2 connectivity.
  • Use namespaces for testing network configurations safely.
  • Document virtual network topology.
  • Clean up unused namespaces and interfaces.

Interview Questions

Beginner

  1. What is a Network Namespace?
  2. Why are Network Namespaces used?
  3. What is a veth pair?
  4. What is a Linux bridge?

Intermediate

  1. Explain communication between two network namespaces.
  2. How does Docker use Network Namespaces?
  3. How does Kubernetes networking rely on namespaces?
  4. Explain the relationship between veth pairs and Linux bridges.

Architect Level

  1. Design container networking using Network Namespaces.
  2. Explain how Kubernetes Pods communicate using CNI plugins.
  3. How would you troubleshoot communication failures between containers?

Summary

In this lesson, you learned:

  • Network Namespaces
  • Network Isolation
  • Virtual Ethernet (veth)
  • Linux Bridges
  • Container Networking
  • Kubernetes Networking
  • Cloud Networking Concepts

Network Namespaces are a fundamental Linux kernel feature that enables isolated networking environments. They form the basis of container networking in Docker and Kubernetes by providing independent network stacks for workloads. Combined with veth pairs and Linux bridges, namespaces allow secure, scalable, and efficient virtual networking in modern cloud-native infrastructure.


Key Takeaways

  • Network Namespaces provide isolated network stacks.
  • Every namespace has its own interfaces, IP addresses, routes, and firewall rules.
  • veth pairs connect namespaces to the host or other namespaces.
  • Linux bridges provide Layer 2 connectivity between namespaces.
  • Docker and Kubernetes rely heavily on Network Namespaces.
  • Understanding namespaces is essential for container networking and cloud-native platforms.

Module 9 Complete

Congratulations! You have successfully completed Module 9: Linux Networking.

You now understand:

  • ip
  • ss
  • netstat
  • tcpdump
  • traceroute
  • dig
  • nslookup
  • curl
  • wget
  • Network Namespaces

You now have hands-on knowledge of Linux networking tools used daily for troubleshooting, automation, cloud infrastructure, Kubernetes operations, and production system administration.


What's Next?

Module 9 Summary — Linux Networking

Review the Module 9 summary, then continue to Module 10: Cloud Networking, where you'll learn how networking works in modern cloud platforms.

You'll explore:

  • AWS VPC
  • Azure Virtual Network (VNet)
  • Google Cloud VPC
  • Subnets
  • Route Tables
  • NAT Gateway
  • Internet Gateway
  • Load Balancers
  • Private Connectivity
  • Hybrid Networking

By the end of Module 10, you'll understand how to design, secure, and troubleshoot cloud networks across AWS, Azure, and Google Cloud, preparing you for real-world cloud architecture and networking roles.