3

I was wondering if there is a way to

  1. Query the network namespace the current shell is in
  2. Change the current network namespace of the current shell

I know I can do things like

sudo ip netns exec <namespace_name> <some command>

to execute a certain command in a certain network namespace, but I was thinking of something like

:~$ sudo query_current_namespace
some_namespace
:~$ sudo change_current_namespace_to other_namespace
:~$ sudo query_current_namespace
other_namespac
:~$ dothings in newnamespace

Just like changerooting into into some subdir, so I won't have to prefix every command with the sudo ip netns exec <namespace> thingy.

Searching the net it looks like I would have to write my own littel C Program, like suggested in this blog post. Is there allready a "standard" tool for this?

Edit: I just found that I could do

sudo ip netns exec <namespace> bash

which will give me a root shell in a new namespace. So almost there, but how do I query the current namespace?

4

See the man page:

   ip netns identify PID - Report network namespaces names for process
       This command walks through /var/run/netns and finds all the network
       namespace names for network namespace of the specified process.

So you would do something like:

ip netns identify $$
  • On what kind of system have you found this man page? At least on ubuntu 12.04 and fedora 18, the option identify is not present. The man page does not list it and the command does not recocnise it. – Isaac Jul 19 '13 at 8:13
  • It's on my Fedora 19 system. – Michael Hampton Jul 19 '13 at 8:51
  • I see, so its brand spankin new ;) Still, I am amazed that it takes that long (since linux 2.6.29) for such a cool feature to make it into admin tools. – Isaac Jul 19 '13 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.