Network namespaces provide isolated network environments within a Linux system. Each namespace has its own network interfaces, routing tables, firewall rules, and network resources. This isolation enables containers, VMs, and network virtualization.
Key Concepts
- Network Namespace: Isolated network stack instance
- Virtual Ethernet (veth): Paired network interfaces
- Bridge: Virtual switch connecting namespaces
- Default Namespace: Initial namespace where system starts
- Loopback Interface: Separate lo interface per namespace
Command Syntax
ip netns [COMMAND] [OPTIONS]
- Manage network namespace operations
- Create, delete, execute commands in namespaces
- List and configure namespace networking
Common Options
add NAME - Create new network namespace
delete NAME - Remove network namespace
list - Show all network namespaces
exec NAME COMMAND - Run command in namespace
identify PID - Show namespace of process
Practical Examples
Example 1: Create and list namespaces
|
|
Creates two isolated network environments
Example 2: Execute commands in namespace
|
|
Shows interfaces and tests connectivity within namespace
Example 3: Create veth pair connection
|
|
Connects two namespaces with virtual cable
Example 4: Configure namespace networking
|
|
Assigns IPs and enables interfaces
Example 5: Test connectivity
|
|
Verifies communication between namespaces
Use Cases
- Container Networking: Docker/Podman isolation
- Network Testing: Simulate network topologies
- Security Isolation: Separate application networks
- VPN Solutions: Isolate VPN traffic
- Network Development: Test routing protocols
Related Commands
ip link - Manage network interfaces
ip addr - Configure IP addresses
ip route - Manage routing tables
bridge - Configure bridge devices
nsenter - Enter namespace of existing process
Tips & Troubleshooting
Common Issues
- No connectivity: Check if interfaces are UP
- Permission denied: Use sudo for namespace operations
- Namespace not found: Verify namespace exists with
ip netns list
Best Practices
- Always bring up loopback in new namespaces:
1sudo ip netns exec NAME ip link set lo up - Clean up unused namespaces to save resources
- Use descriptive namespace names for clarity
Debugging
|
|
Performance Notes
- Each namespace consumes kernel memory
- Limit namespaces based on system resources
- Use bridges for connecting multiple namespaces efficiently