How can I move an interface out of a network namespace?

22,175

Solution 1

I found an answer. Sure, you cannot do it from within the netns. But, if you execute ip netns exec .... from within the root network namespace, it all works.

ip netns exec <PID> ip link set eth10 netns 1

Then it works! It takes the PID (1 in this case) to which we are assigning it to be in the context of the executing command (wrapper) before we enter the netns. Done!

Solution 2

You can use the ip netns delete function. From the man page:

ip [-all] netns delete [ NAME ] - delete the name of a network namespace(s)

If NAME is present in /var/run/netns it is umounted and the mount point is removed. If this is the last user of the network namespace the network namespace will be freed and all physical devices will be moved to the default one, otherwise the network namespace persists until it has no more users. ip netns delete may fail if the mount point is in use in another mount namespace.

It can not be used for all purposes, but it's definitely the easiest route.

Solution 3

What worked for me based on deitch's answer is to execute the command from the custom netns:

ip netns exec myns ip link set eth10 netns 1

Share:
22,175

Related videos on Youtube

deitch
Author by

deitch

Updated on September 18, 2022

Comments

  • deitch
    deitch almost 2 years

    If I move an interface temporarily into a netns with

    ip link set eth10 netns myns
    

    then it no longer is visible in the root, only within the namespace myns.

    How do I move it back, something like (these obviously don't exist):

    ip link unset eth10
    

    or perhaps

    ip link set eth10 netns root
    

    or similar?

    • goldilocks
      goldilocks over 3 years
      See also: askubuntu.com/a/854855/239729 Although I could delete the namespace (/var/run/netns/foo is gone, referencing netns foo results in error, etc.), none of the normal methods mentioned here worked to get the physical (wifi) interface back until I located processes (in this case, forgotten dhcpcd,wpa_supplicant) accessing it and killed them (at which point it re-appeared without doing anything further).
  • TheDiveO
    TheDiveO about 6 years
    Let's hope that "1" is actually referencing the "root" network namespace...
  • Ted Feng
    Ted Feng about 5 years
    move eth10 to back to default network namespace: ip netns exec myns ip link set eth10 netns default
  • Paul
    Paul over 4 years
    On my Debian 10 system "default" seems to indeed be 1. ip netns exec testns ip link set tun0 netns 1 worked, whereas 'default' resulted in Error: argument "default" is wrong: Invalid "netns" value.
  • Paul
    Paul over 4 years
    Further messing about shows that these are ids. If you have created one additional namespace that will have id 2.
  • sjy
    sjy almost 4 years
    Just an additional warning, since this didn't jump out at me the first time I read this answer: this only applies to physical devices. ip netns delete will delete virtual devices in the deleted namespace.
  • sjy
    sjy almost 4 years
    The above is confirmed in network_namespaces(7): "When a namespace is freed, the veth(4) devices that it contains are destroyed."
  • Admin
    Admin almost 2 years
    This will only work if there are no processes actively running in the name space. If you run this command and there are processes running with the network interface in the name space it will not be returned unless you reboot, or kill the processes. This post here details how you can look up the processes using the interface askubuntu.com/questions/826542/… Here is the oneliner you can use to find them and kill them for i in $(sudo find /proc/ -name "enp1s0.10"| grep task | awk -F'/' '{print$3}'); do sudo kill -9 $i; done