Search code examples
linuxbashnamespacesiplinux-namespaces

Run multiple commands in network namespace


I'd like to run a series of concatenated bash commands in a network namespace in one single step, but it seems that ip netns exec mynetns only takes one bash command as argument. For instance:

ip netns exec mynetns ip a

...works as expected.

ip netns exec mynetns "ip a ; ip a"

...returns exec of "ip a" failed: No such file or directory.

Clearly,

ip netns exec mynetns ip a ; ip a

... will return the interfaces in the network namespace and on the host machine.

Is there a way to make it work?

The manual only talks about one command, which is not a very good sign...


Solution

  • netns exec is running a command, not a bash command: it is executing a process with arguments itself.

    That's OK, it just means you need to explicitly make bash the command. This should work:

    ip netns exec mynetns bash -c "ip a ; ip a"