15

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?

1
  • 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).
    – goldilocks
    Feb 13, 2021 at 16:03

3 Answers 3

19

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!

5
  • Let's hope that "1" is actually referencing the "root" network namespace...
    – TheDiveO
    Jun 25, 2018 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, 2019 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, 2020 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, 2020 at 19:21
  • 2
    ip -n NS link set netns 1 dev DEV can be used instead of exec
    – Zouppen
    Apr 11, 2023 at 10:11
8

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.

3
  • 4
    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, 2020 at 7:18
  • The above is confirmed in network_namespaces(7): "When a namespace is freed, the veth(4) devices that it contains are destroyed."
    – sjy
    Jul 30, 2020 at 8:28
  • 1
    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
    – Dave
    Jun 29, 2022 at 17:26
6

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

You must log in to answer this question.

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