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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite
1












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






share|improve this question




















  • 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














up vote
2
down vote

favorite
1












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






share|improve this question




















  • 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












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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






share|improve this question












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








share|improve this question











share|improve this question




share|improve this question










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
















  • 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










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.






share|improve this answer


















  • 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







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer


















  • 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















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.






share|improve this answer


















  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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













  • 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








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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos