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

1. Connecting a network namespace to the host system - with a veth pair

category: howtoz · date: 2016-11-01 · updated: 2018-01-09

The Slovak original of this document: 1. Prepojenie sieťového menného priestoru a hostiteľského systému - pomocou páru veth adaptérov (slovensky).

2016 - Linux NET Namespace - Connecting a network namespace to the host system - with a veth pair

asciiart
======================================================================================================================
 [1] NET namespace - Connecting a network namespace to the host system - with a veth pair
======================================================================================================================

                        +--------------------+                       +--------------------+
                        | hostOS     veth1.1 |=========kabel=========| veth1.2        ns1 |     (PID = 2429)
                        +--------------------+                       +--------------------+
                          hostitelsky system                            namespace "ns1"

    [1.1] - Create one network (NET) namespace, "ns1".
    ----------------------------------------------------------------------------------------------------------------
    # ip netns add ns1
    ----------------------------------------------------------------------------------------------------------------

    [1.2]TERM2 - In network namespace "ns1", exec the command "bash". PID = 2429
    [1.3]TERM2 - Zistime PID BASH procesu.
    ----------------------------------------------------------------------------------------------------------------
    [1.2]TERM2# ip netns exec ns1 bash
    [1.3]TERM2# echo $$
    ----------------------------------------------------------------------------------------------------------------
    2429
    ----------------------------------------------------------------------------------------------------------------

    [1.4] - Create a pair of virtual Ethernet devices, which stand for a network cable with two RJ45
            plugs, putting one end (veth1.2) into namespace "ns1" (by identifier
            procesu [55718]) a druhu stranu (veth1.1) pripojime do hostitelskeho systemu.
    ----------------------------------------------------------------------------------------------------------------
    # ip link add veth1.1 type veth peer name veth1.2 netns 2429
    ----------------------------------------------------------------------------------------------------------------

    [1.5] - Check that the device pairs from step [1.4] were created.
    ----------------------------------------------------------------------------------------------------------------
    # ip link show
    ----------------------------------------------------------------------------------------------------------------
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:9b:30:f4 brd ff:ff:ff:ff:ff:ff
    3: veth1.1@if2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
        link/ether 22:74:36:08:a3:e0 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    ----------------------------------------------------------------------------------------------------------------

    [1.6] - Bring the network adapter "veth1.2" up in namespace "ns1" and give it the address "10.0.0.1".
    [1.7] - Bring the network adapter "veth1.1" up on the host system as the other end of the "cable" "veth1.2" and
            nastavime na nom IP adresu "10.0.0.2".
    [1.8] - Test that the host OS and network namespace "ns1" can reach each other.
    ----------------------------------------------------------------------------------------------------------------
    [1.6]# ip netns exec ns1 ifconfig veth1.2 10.0.0.1/24 up
    [1.7]# ifconfig veth1.1 10.0.0.2/24 up
    [1.8]# ping 10.0.0.1
    ----------------------------------------------------------------------------------------------------------------
    64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.075 ms
    64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.093 ms
    ...
    ----------------------------------------------------------------------------------------------------------------

    [1.9]  - Print the routing table of network namespace "ns1".
    [1.10] - List the network adapters/links of network namespace "ns1".
    ----------------------------------------------------------------------------------------------------------------
    [1.9] # ip netns exec ns1 route
    ----------------------------------------------------------------------------------------------------------------
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 veth1.2
    ----------------------------------------------------------------------------------------------------------------
    [1.10]# ip netns exec ns1 ip link
    ----------------------------------------------------------------------------------------------------------------
    1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: veth1.2@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT qlen 1000
        link/ether 7e:d3:d9:0a:56:b4 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    ----------------------------------------------------------------------------------------------------------------
← howtoz(EN | SK)