Howto query and change network namespace on linux?

5,023

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 $$
Share:
5,023

Related videos on Youtube

Isaac
Author by

Isaac

Updated on September 18, 2022

Comments

  • Isaac
    Isaac almost 2 years

    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?

  • Isaac
    Isaac almost 11 years
    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.
  • Michael Hampton
    Michael Hampton almost 11 years
    It's on my Fedora 19 system.
  • Isaac
    Isaac almost 11 years
    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.