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

2016 - Linux Namespaces - NET namespace

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

The Slovak original of this document: 2016 - Linux Namespaces - NET namespace (slovensky).

NET (network stack) namespaces in Linux

1 Úvod

The NET namespace (network stack) isolates the network resources. Each network namespace has its own devices, addresses, routing tables, port numbers and its own "/proc/net" directory.

mnt_namespace
mnt_namespace

2 Working with NET namespaces

2.1 Creating new NET namespaces

Create two network (NET) namespaces, the first named "ns1" and the second "ns2".

asciiart
# ip netns add ns1
# ip netns add ns2

2.2 Checking that the new network (NET) namespaces exist

asciiart
# ls -l /var/run/netns
----------------------------------------------------------------------------------------------------------------
-r--r--r--. 1 root root 0 Nov 10 12:28 ns1
-r--r--r--. 1 root root 0 Nov 10 12:28 ns2

2.3 Listing every network (NET) namespace

List every network (NET) namespace with "ip".

asciiart
# ip netns list
----------------------------------------------------------------------------------------------------------------
ns2
ns1
----------------------------------------------------------------------------------------------------------------
# ip netns list-id
----------------------------------------------------------------------------------------------------------------
nsid 0 (iproute2 netns name: ns1)
nsid 1 (iproute2 netns name: ns2)

2.4 Watching network (NET) namespaces come and go

"ip" with "netns monitor" watches network namespaces being created and removed.

asciiart
# ip netns monitor
----------------------------------------------------------------------------------------------------------------
delete ns2
add ns2

2.5 Running processes inside network (NET) namespaces

The example below runs a BASH process in the namespace named "ns1".

asciiart
[1]TERM1# ip netns exec ns1 bash
[2]TERM1# echo $$
----------------------------------------------------------------------------------------------------------------
27768
----------------------------------------------------------------------------------------------------------------
[3]TERM1# ifconfig -a
----------------------------------------------------------------------------------------------------------------
lo: flags=8<LOOPBACK>  mtu 65536
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

In effect we have "moved" into namespace "ns1", and every command that follows runs in it.

In the second terminal (TERM2), check that the new network namespace exists with "namespaces-info.sh", printing only the non-default namespaces (the -n switch). Two namespaces (MNT and NET) have been created for our BASH process (27768).

asciiart
TERM2# ./namespaces-info.sh -n
----------------------------------------------------------------------------------------------------------------
---------- + ---------- + -------------------- + ----------------------------------------
PID        | PPID       | NAMESPACE            | COMMAND
---------- + ---------- + -------------------- + ----------------------------------------
18         | 2          | mnt:[4026531856]     | [kdevtmpfs]
---------- + ---------- + -------------------- + ----------------------------------------
585        | 1          | mnt:[4026532423]     | /usr/lib/systemd/systemd-udevd
---------- + ---------- + -------------------- + ----------------------------------------
720        | 1          | mnt:[4026532450]     | /usr/bin/vmtoolsd
---------- + ---------- + -------------------- + ----------------------------------------
755        | 1          | mnt:[4026532451]     | /usr/sbin/NetworkManager
---------- + ---------- + -------------------- + ----------------------------------------
854        | 755        | mnt:[4026532451]     | /sbin/dhclient
---------- + ---------- + -------------------- + ----------------------------------------
27768      | 21524      | mnt:[4026532584]     | bash
27768      | 21524      | net:[4026532455]     | bash
---------- + ---------- + -------------------- + ----------------------------------------

The previous example listed every non-default namespace, each with its i-node number. The network namespace "net:[4026532455]" has i-node number "4026532455", which is the i-node number of the file "/var/run/netns/ns1". To check, print the i-node number of the file "/var/run/netns/ns1".

asciiart
# ls -lhi /var/run/netns/
----------------------------------------------------------------------------------------------------------------
4026532455 -r--r--r--. 1 root root 0 Nov 15 13:59 ns1

2.6 Removing network (NET) namespaces

Remove the network (NET) namespace named "ns1" with "ip".

asciiart
# ip netns del ns1

When a namespace is removed, every migratable network adapter in it is moved back to the default namespace.

2.7 Moving network devices between network (NET) namespaces

Network adapters that can be moved between network namespaces are those whose "netns-local" feature is "off" — in other words, those that are not local: \-- netns-local: off -> a migratable adapter \-- netns-local: on -> a non-migratable adapter (a local one)

"ethtool" will say which adapters are migratable. The example below checks "ens33". The output shows it is migratable — not local — because "netns-local" is "off".

asciiart
# ethtool -k ens33 | grep netns-local
----------------------------------------------------------------------------------------------------------------
netns-local: off [fixed]
← howtoz(EN | SK)