Skip to main content

Linux Network Namespace Configuration

A Linux network namespace provides an isolated network environment, allowing multiple instances of network stacks to coexist on a single host. each namespace has its own interfaces, IP addresses, FDB(L2 forwarding DB). and routing tables, ensuring separation of network configurations. This isolation is crucial for containerization and virtualization, ensuring separate network configurations within shared hardware.

Network namespace is equivalent to VPNs or VRFs in traditional network devices.

VETH links are used to interconnect different NETNS(network namespace) and achieve connectivity between them.

info

Unlike Linux VRF links which provide routing table isolation for it's slave devices, the network namespace has a complete isolation of the linux network stack.

Basic Network Namespace configuration

In this document, we will create two netns(network namespace), VPN10 and VPN20, then we will create two VETH links to interconnect the two netns to achive connectivity between them.

Topology

topology1

Configuration

  • Create two netns (vpn10 and vpn20), then we
  • Add enp0s4 link to vpn10 netns and enp0s5 link to vpn20 netns.
dent-1(config)# netnses-iproute2
dent-1(config-netnses-iproute2)# netns vpn10
dent-1(config-netnses-iproute2)# netns vpn20
dent-1(config-netnses-iproute2)# exit
dent-1(config)# links-iproute2
dent-1(config-links-iproute2)# link enp0s4
dent-1(config-[name='enp0s4'])# netns vpn10
dent-1(config-[name='enp0s4'])# ip 192.168.10.10/24
dent-1(config-[name='enp0s4'])# admin-status up
dent-1(config-[name='enp0s4'])# exit
dent-1(config-links-iproute2)# link enp0s5
dent-1(config-[name='enp0s5'])# netns vpn20
dent-1(config-[name='enp0s5'])# ip 192.168.20.20/24
dent-1(config-[name='enp0s5'])# admin-status up
dent-1(config-[name='enp0s5'])# commit
note

Now we have two PCs in different netns, if we check the fdb/arp table we can see that each link PC ip belong to different netns

dent-1# show config-running neighbors-iproute2 
neighbor 192.168.20.1 netns vpn20
dev enp0s5
router false
use false
managed false
extern_learn false
lladdr e6:70:03:8d:7e:09
nud reachable
neighbor 192.168.10.1 netns vpn10
dev enp0s4
router false
use false
managed false
extern_learn false
lladdr b6:1d:6e:03:0e:e6
nud stale

dent-1#

Veth Configuration

Now if we want to have connectivity between the two different netns, we will need to create a veth link for each netns, and interconnect them.

To achieve this we do the following:

  • Create veth10 and set veth20 as its peer.
  • add each veth to it's netns.
  • add route for each vpn client subnet (192.168.10.0/24 for vpn10) and (192.168.20.0/24 for vpn20). and the nexthop for those route are the veth interfaces.
dent-1(config-links-iproute2)# link veth10
dent-1(config-[name='veth10'])# type iproute2-ip-link:veth
dent-1(config-[name='veth10'])# virtual_peer_name veth20
dent-1(config-[name='veth10'])# ip 1.1.1.9/30
dent-1(config-[name='veth10'])# admin-status up
dent-1(config-[name='veth10'])# netns vpn10
dent-1(config-[name='veth10'])# exit
dent-1(config-links-iproute2)# link veth20
dent-1(config-[name='veth20'])# type iproute2-ip-link:veth
dent-1(config-[name='veth20'])# netns vpn20
dent-1(config-[name='veth20'])# ip 1.1.1.10/30
dent-1(config-[name='veth20'])# admin-status up
dent-1(config-[name='veth20'])# virtual_peer_name veth10
dent-1(config-[name='veth20'])# exit
dent-1(config)# routes-iproute2
dent-1(config-routes-iproute2)# route 192.168.20.0/24 netns vpn10
dent-1(config-[prefix='192.168.20.0/24'][netns='vpn10']# nexthop veth10
dent-1(config-[prefix='192.168.20.0/24'][netns='vpn10']# exit
dent-1(config-routes-iproute2)# route 192.168.10.0/24 netns vpn20
dent-1(config-[prefix='192.168.10.0/24'][netns='vpn20']# nexthop veth20
dent-1(config-[prefix='192.168.10.0/24'][netns='vpn20']# commit

Now we should have connectivity between PC1 and PC2.

PC1> ping 192.168.20.1
PING 192.168.20.1 (192.168.10.1) 56(84) bytes of data.
64 bytes from 192.168.20.1: icmp_seq=1 ttl=64 time=0.878 ms
64 bytes from 192.168.20.1: icmp_seq=2 ttl=64 time=1.00 ms
64 bytes from 192.168.20.1: icmp_seq=3 ttl=64 time=1.16 ms
64 bytes from 192.168.20.1: icmp_seq=4 ttl=64 time=1.07 ms

tip

To allow the communication between PC1 and PC2 from different netns, we need to enable net.ipv4.ip_forward on each netns:

  • ip netns exec vpn10 sysctl -w net.ipv4.ip_forward=1
  • ip netns exec vpn20 sysctl -w net.ipv4.ip_forward=1