Docs / Networking / Understanding and Configuring Linux Network Namespaces

Understanding and Configuring Linux Network Namespaces

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 122 views · 2 min read

What Are Network Namespaces?

Network namespaces provide isolation of network resources — each namespace has its own interfaces, routing tables, and firewall rules. Docker and other container technologies use them internally.

Create a Namespace

# Create
sudo ip netns add test-ns

# List
ip netns list

# Execute command inside namespace
sudo ip netns exec test-ns ip addr show
# You'll see only the loopback interface

Connect Namespaces with veth Pairs

# Create a virtual ethernet pair
sudo ip link add veth0 type veth peer name veth1

# Move one end to the namespace
sudo ip link set veth1 netns test-ns

# Configure the host end
sudo ip addr add 10.0.0.1/24 dev veth0
sudo ip link set veth0 up

# Configure the namespace end
sudo ip netns exec test-ns ip addr add 10.0.0.2/24 dev veth1
sudo ip netns exec test-ns ip link set veth1 up
sudo ip netns exec test-ns ip link set lo up

# Test connectivity
sudo ip netns exec test-ns ping 10.0.0.1

Internet Access from Namespace

# Enable IP forwarding
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

# Add NAT
sudo iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE

# Add default route in namespace
sudo ip netns exec test-ns ip route add default via 10.0.0.1

Use Cases

  • Testing network configurations safely
  • Running services on isolated networks
  • Simulating multi-host environments on a single server
  • Network security testing

Clean Up

sudo ip netns delete test-ns

Was this article helpful?