4

Is there a single (simple) command that will create a veth interface pair and assign each interface to a different network namespace?

For example, suppose that I have two namespaces: mynamespace-1 and mynamespace-2. Is there a single (simple) command that will connect these two namespaces via a veth pair where each endpoint of the interface is named eth0?

Currently what I would do is create the veth pair, move each interface to the corresponding namespace, and then rename the interface from within that namespace. I'd like to know if these three commands can be compressed into a single command.

For context, here is an example of how I am currently connecting a pair of namespaces and testing the connection:

# Create two network namespaces
sudo ip netns add 'mynamespace-1'
sudo ip netns add 'mynamespace-2'

# Create a veth virtual-interface pair
sudo ip link add 'myns-1-eth0' type veth peer name 'myns-2-eth0'

# Assign the interfaces to the namespaces
sudo ip link set 'myns-1-eth0' netns 'mynamespace-1'
sudo ip link set 'myns-2-eth0' netns 'mynamespace-2'

# Change the names of the interfaces (I prefer to use standard interface names)
sudo ip netns exec 'mynamespace-1' ip link set 'myns-1-eth0' name 'eth0'
sudo ip netns exec 'mynamespace-2' ip link set 'myns-2-eth0' name 'eth0'

# Assign an address to each interface
sudo ip netns exec 'mynamespace-1' ip addr add 192.168.1.1/24 dev eth0
sudo ip netns exec 'mynamespace-2' ip addr add 192.168.2.1/24 dev eth0

# Bring up the interfaces (the veth interfaces the loopback interfaces)
sudo ip netns exec 'mynamespace-1' ip link set 'lo' up
sudo ip netns exec 'mynamespace-1' ip link set 'eth0' up
sudo ip netns exec 'mynamespace-2' ip link set 'lo' up
sudo ip netns exec 'mynamespace-2' ip link set 'eth0' up

# Configure routes
sudo ip netns exec 'mynamespace-1' ip route add default via 192.168.1.1 dev eth0
sudo ip netns exec 'mynamespace-2' ip route add default via 192.168.2.1 dev eth0

# Test the connection (in both directions)
sudo ip netns exec 'mynamespace-1' ping -c 1 192.168.2.1
sudo ip netns exec 'mynamespace-2' ping -c 1 192.168.1.1
  • Traditionally, UNIX aspired to simple commands that did one thing and one thing well. You then tied those commands together in shell scripts or other constructs to achieve more complex stuff. So one approach, is to write a simple script that takes some parameters and executes the above commands to achieve your desired goal, resulting in a single command (the shell script - many apparently simple commands in UNIX are already shell scripts). I appreciate you're after an existing command, and hence this is a comment, and not an answer. – EightBitTony Nov 20 '17 at 15:48
  • @EightBitTony Yeah, you're right. But apparently there's just something about this particular process that violates some aesthetic sensibility of mine. – igal Nov 20 '17 at 15:52
6

The best that I can offer is to execute the command in one namespace (using the -n shortcut), create each endpoint with the same name, and move one of them into a different namespace in that command:

ip -n mynamespace-1 link add eth0 type veth peer name eth0 netns mynamespace-2

You'll still need to do the other stuff like address assignment (the -n abbreviation may also be helpful), so you'll have to write a script, anyway.

As man ip says, the -n option is a shortcut for ip netns exec ... ip ..., so you can use this form if your ip doesn't support the -n option.

  • 1
    This is exactly what I had in mind. I must have missed that you could specify a namespace for the ip link command. My version of ip doesn't have the -n option, but the same basic idea worked using netns exec instead, e.g. ip netns exec mynamespace-1 ip link add eth0 type veth peer name eth0 netns mynamespace-2. Can you add that to your solution? – igal Nov 21 '17 at 22:13

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.