2

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...

Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185

2 Answers2

9

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"
Andrew McGuinness
  • 2,092
  • 13
  • 18
0

I am using following ways in my code.

1: Use batching all shell commands following way. Please take care of space/tab etc as batching is very sensitive to that. PS: I am using multiple namespace isolation using unshare, that is optional. Used that to give additional idea of various namespace isolation.

root@7fb1cd095b91:# ip netns exec $NSNAME unshare --fork --pid --mount-proc /bin/bash -e <<START readlink /proc/self /bin/bash readlink /proc/self ls -lrt START

2: Second using same commands in some script and call that script directly then batching bash commands inline.

root@7fb1cd095b91:#ip netns exec $NSNAME unshare -mpf --mount-proc --fork /bin/bash -c /tmp/start_ns.sh&