LINUXOR.SK ... open source notes ...

2. Connecting two network namespaces (ns1, ns2) - with a veth pair

category: howtoz · date: 2016-11-01 · updated: 2017-01-16

The Slovak original of this document: 2. Prepojenie dvoch sieťových menných priestorov (ns1, ns2) - pomocou páru veth adaptérov (slovensky).

2016 - Linux NET Namespace - Connecting two network namespaces (ns1, ns2) - with a veth pair

1 Schéma

asciiart
                        +--------------------+                       +--------------------+
    (PID = 12112)       | ns1          veth1 |=========kabel=========| veth2          ns2 |     (PID = 12130)
                        +--------------------+                       +--------------------+
                           namespace "ns1"                               namespace "ns2"

2 Creating network namespaces

Create two network (NET) namespaces, "ns1" and "ns2".

asciiart
# ip netns add ns1
# ip netns add ns2

3 Running processes inside namespaces

asciiart
[1]TERM1# ip netns exec ns1 bash
[2]TERM1# echo $$
----------------------------------------------------------------------------------------------------------------
12112
----------------------------------------------------------------------------------------------------------------
[3]TERM2# ip netns exec ns2 bash
[4]TERM2# echo $$
----------------------------------------------------------------------------------------------------------------
12130

4 Creating a pair of virtual Ethernet devices

koncovkami, pricom nasledne jednu stranu (veth1) umiestnime do menneho priestoru "ns1" a druhu stranu (veth2) umiestnime do menneho priestoru "ns2".

asciiart
[1]# ip link add veth1 type veth peer name veth2
[2]# ip link set veth1 netns ns1
[3]# ip link set veth2 netns ns2

5 Bringing the virtual Ethernet devices up in the namespaces and testing that they talk

asciiart
[1] # ip netns exec ns1 ifconfig veth1 10.0.0.1/24 up
[2]# ip netns exec ns2 ifconfig veth2 10.0.0.2/24 up
[3]# ip netns exec ns1 ping 10.0.0.2
----------------------------------------------------------------------------------------------------------------
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.036 ms
...
----------------------------------------------------------------------------------------------------------------
[4]# ip netns exec ns2 ping 10.0.0.1
----------------------------------------------------------------------------------------------------------------
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.069 ms
...
← howtoz(EN | SK)