Connecting two network namespaces via a veth interface pair where each endpoint has the same name

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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
networking iproute network-namespaces
add a comment |Â
up vote
2
down vote
favorite
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
networking iproute network-namespaces
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
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
networking iproute network-namespaces
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
networking iproute network-namespaces
asked Nov 20 '17 at 15:42
igal
4,830930
4,830930
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
add a comment |Â
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
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
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
@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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
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 theip linkcommand. My version ofipdoesn't have the-noption, but the same basic idea worked usingnetns execinstead, 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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
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 theip linkcommand. My version ofipdoesn't have the-noption, but the same basic idea worked usingnetns execinstead, 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
add a comment |Â
up vote
5
down vote
accepted
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 theip linkcommand. My version ofipdoesn't have the-noption, but the same basic idea worked usingnetns execinstead, 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
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
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.
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.
edited Nov 22 '17 at 7:29
answered Nov 21 '17 at 22:00
dirkt
14.2k2931
14.2k2931
1
This is exactly what I had in mind. I must have missed that you could specify a namespace for theip linkcommand. My version ofipdoesn't have the-noption, but the same basic idea worked usingnetns execinstead, 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
add a comment |Â
1
This is exactly what I had in mind. I must have missed that you could specify a namespace for theip linkcommand. My version ofipdoesn't have the-noption, but the same basic idea worked usingnetns execinstead, 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
1
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
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405805%2fconnecting-two-network-namespaces-via-a-veth-interface-pair-where-each-endpoint%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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