With ip netns exec you can execute a command in a custom network namespace – but is there also a way to execute a command in the default namespace?
For example, after executing these two commands:
sudo ip netns add test_ns
sudo ip netns exec test_ns bash
How can the newly created bash execute programs in the default network namespace? There is no ip netns exec default or anything similar as far as I've found.
My scenario is:
I want to run a SSH server in a separate network namespace (to keep the rest of the system unaware of the network connection, as the system is used for network testing), but want to be able to execute programs in the default network namespace via the SSH connection.
What I've found out so far:
-
Created network namespaces are listed as files under /var/run/netns (but there is no file for the default namespace)
-
The ip netns exec code can be found here: http://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git/tree/ip/ipnetns.c#n132 – I haven't grasped everything that it is doing yet, but it doesn't look very promising.
-
ip netns identify $$ as suggested by Howto query and change network namespace on linux? returns nothing when in the default network namespace
Best Answer
Newer distros/kernels support the
nsenter
command which, should do what you want, providing you are root when you do it.Here is an example (Fedora 20).
This relies on the
setns
system call. You need at least a 3.0 kernel and glibc-2.14 for this to work.RHEL 6.5 provides support for persistent namespaces but not support for moving existing processes into new namespaces.