13

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?

deitch
  • 545
  • 1
  • 4
  • 15
  • See also: https://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). – goldilocks Feb 13 '21 at 16:03

3 Answers3

17

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!

deitch
  • 545
  • 1
  • 4
  • 15
  • Let's hope that "1" is actually referencing the "root" network namespace... – TheDiveO Jun 25 '18 at 15:57
  • move eth10 to back to default network namespace: ip netns exec myns ip link set eth10 netns default – Ted Feng Apr 28 '19 at 23:38
  • 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 Feb 21 '20 at 19:10
  • Further messing about shows that these are ids. If you have created one additional namespace that will have id 2. – Paul Feb 21 '20 at 19:21
7

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.

Paul
  • 103
  • 4
Dave
  • 171
  • 1
  • 4
  • 3
    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 Jul 30 '20 at 07:18
  • The above is confirmed in [network_namespaces(7)](https://man7.org/linux/man-pages/man7/network_namespaces.7.html): "When a namespace is freed, the veth(4) devices that it contains are destroyed." – sjy Jul 30 '20 at 08:28
  • 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 https://askubuntu.com/questions/826542/interface-missed-after-namespace-removal 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` – Dave Jun 29 '22 at 17:26
5

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

Shedee
  • 51
  • 1
  • 4