When working with Linux network namespaces, you might need to temporarily move an interface into a namespace for testing or isolation purposes. The standard way to do this is:
ip link set eth10 netns myns
But what's less documented is how to properly move it back to the root namespace when you're done.
To move an interface back to the root namespace, you actually need to specify the root namespace's PID (which is 1):
ip link set eth10 netns 1
Or alternatively, you can use the symbolic name:
ip link set eth10 netns root
1. Permission Issues: You need CAP_NET_ADMIN capabilities to perform this operation. Either run as root or use sudo.
2. Interface Not Found: Remember you need to execute this command from within the namespace where the interface currently resides.
3. Namespace Cleanup: If you're deleting the namespace, first move all interfaces back:
ip netns del myns # Will fail if interfaces remain
Here's a complete example of moving an interface to a namespace and back:
# Create namespace
ip netns add testns
# Move interface to namespace
ip link set eth10 netns testns
# Work in the namespace (example configuration)
ip netns exec testns ip link set eth10 up
ip netns exec testns ip addr add 192.168.1.10/24 dev eth10
# When done, move back to root
ip netns exec testns ip link set eth10 netns 1
# Verify in root namespace
ip link show eth10
You can also work with namespace file descriptors for more complex scenarios:
# Get namespace FD
mkfifo /tmp/netns
ip netns exec testns sh -c 'echo $$ > /tmp/netns'
# Move interface using FD
ip link set eth10 netns $(cat /tmp/netns)
Frequent movement of interfaces between namespaces can cause:
- Brief packet loss during transition
- ARP cache invalidation
- Potential routing table updates
When working with Linux network namespaces, a common task is moving interfaces between namespaces. While moving an interface into a namespace is straightforward, beginners often struggle with moving it back to the root namespace.
The proper command to move an interface back to the root namespace is:
ip link set eth10 netns 1
Or alternatively:
ip link set eth10 netns PID
Where PID is the process ID of any process in the root namespace (usually 1 for init/systemd).
Let's see a complete workflow:
# Create a test namespace
ip netns add testns
# Move interface to namespace
ip link set eth10 netns testns
# Verify it's gone from root
ip link show eth10 # Should show "Cannot find device"
# Move it back to root
ip link set eth10 netns 1
# Verify it's back
ip link show eth10 # Should show the interface
If you don't know the interface name in the target namespace:
# Execute commands in the namespace
ip netns exec testns ip link
# Then move a specific interface back
ip netns exec testns ip link set eth10 netns 1
Common issues and solutions:
- Interface disappears: Check with
ip netns exec testns ip link
- Permission denied: Run as root or with sudo
- Device busy: First bring the interface down with
ip link set eth10 down
Remember that:
- The root namespace is always represented by PID 1
- Physical interfaces can be moved, but virtual interfaces (veth pairs) are often better for namespaces
- Some interface types (like bridges) can't be moved between namespaces