2

In Linux, set up 2 network namespaces, ns1 and ns2. ip netns list can view the 2 network namespaces.

If I move one network device from Linux root namespace to ns1 then from ns1 to ns2, then delete ns2, I expect that the network device can move back to ns1.

The actual result is that eth1 is back to Linux root network namespace. I'm not sure whether it's as expected.

Here is the detail test scenario:

  1. ip netns add ns1
  2. ip netns add ns2
  3. ip link set eth1 netns ns1
  4. ip netns exec ns1 ip link set eth1 netns ns2
  5. ip netns del ns2

Expected result: eth1 will be in ns1

Actual result: eth1 is back in Linux root namespace 1

Question: is there any method to realize such scenario to make sure device can be back to ns1 not Linux root network namespace 1?

2
  • Make ns2 a child of ns1.
    – phemmer
    Jun 15, 2018 at 12:28
  • To overcome this limitation, you could have an event loop running (on the output of) ip monitor link to detect the reapparition of the device on the initial net namespace, and immediately cast it to ns1. Interface's name could be different from the former in some cases of renaming or naming conflict.
    – A.B
    Jan 7, 2019 at 18:45

1 Answer 1

3

As far as I know, there is no way to enforce "physical" network interfaces return to a different network namespace than the so-called "initial" network namespace. Contrary to Patrick's comment, the Linux kernel considers network namespace to be flat and thus without any hierarchy (see ioctl_ns - ioctl() operations for Linux namespaces, which explicitly mentions only PID and user namespaces to be hierarchical).

Because there is no hierarchy for network namespaces defined (which would be difficult to apply to IP stacks, addresses, and route tables), Linux has the return-to-home rule which moves network interfaces back into the initial network namespace, that is, the network namespace to which init(1)/PID1 is joined to.

All ip netns add ... does is create a new network namespace by calling unshare() and then bind-mounts it into /run/netns/ns1 to keep it floating alive. The next ip nets add ... starts anew, mount-binding another network namespace into /run/netns/ns2. Two completely unrelated network namespaces.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .