4

I'm quite new to Linux networking.

I have a Debian PC with two Ethernet interfaces, one embedded on the motherboard and another on a PCI card. The first one, let say eth0 is connected to my router (which is connected to Inet). I want to "link" eth1 to eth0, in order to access my router (and Inet) when I plug a cable in eth1. The same way the eth0 cable is connected to one of my router's Ethernet ports.

The Debian PC should also be able to access Inet and LAN, and thus not simply behave as a "virtual link" between the cable plugged in eth0 (coming from my router) and the cable plugged in eth1 (going to another PC).

Is this achievable? How?

7

You can use a bridge interface. You can use brctl from bridge-utils to create a bridge interface. For example,

$ brctl addbr br0
$ brctl addif br0 eth0 eth1
$ brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.00004c9f0bd2       no              eth0
                                                        eth1

So after adding interfaces eth0 & eth1 into the bridge device br0 you're left with the following setup. You can use ifconfig to see it:

$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr BC:AE:AA:34:22:11  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
...

$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr BC:AE:AA:34:11:22  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
...

And the bridge device with the IP address:

$ ifconfig br0
br0       Link encap:Ethernet  HWaddr BC:AE:C5:11:22:33  
          inet addr:192.168.1.20  Bcast:192.168.1.255  Mask:255.255.255.0
...
  • Actually, I'm using SSH, and when I added the if, my console has frozen, suggesting me that I lost the connection. I've noticed that eth1 wasn't in /etc/network/interfaces. Should I configured it manually before ? Thanks anyway for the help – Maxime Piraux Aug 2 '13 at 17:31
  • @MaximePiraux: The Debian PC has to connect to the network through the bridge interface. – user26112 Aug 2 '13 at 17:41
  • @MaximePiraux - when you bridge multiple network devices together they will no longer have their own IP addresses. The bridge device (br0) will. – slm Aug 2 '13 at 18:40
  • @slm: Thank you for the additions to my answer. – user26112 Aug 2 '13 at 18:46
  • 1
    I hope you don't mind, it was easier to add to yours vs. starting a competing answer which usually is more confusing. If I ever do it and you disagree always feel free to roll it back. – slm Aug 2 '13 at 18:48
1

A few years later.. Adding another answer.

First of all, I'm not sure if the ifconfig package is maintained this days. it is better to use the ip command from the iproute2 package.

A few guides on iproute2:

http://andys.org.uk/bits/2010/02/24/iproute2-life-after-ifconfig/

https://www.tecmint.com/ifconfig-vs-ip-command-comparing-network-configuration/

https://lartc.org/howto/lartc.iproute2.html#LARTC.IPROUTE2.WHY

Secondly, in your case I would probably use the simple Linux bridge also, But, it is also important to mention that since 2014 Open vSwitch (OVS) is a strong contender to the Linux Bridge.

Some references:

http://www.fiber-optic-transceiver-module.com/ovs-vs-linux-bridge-who-is-the-winner.html

https://devinpractice.com/2016/10/18/open-vswitch-introduction-part-1/

https://kumul.us/switches-ovs-vs-linux-bridge-simplicity-rules/

https://networkengineering.stackexchange.com/questions/28408/difference-between-linux-bridge-and-open-vswitch


Edit: I'll show how to connect two Linux namespaces using a bridge.

Solution #1 - Using Linux bridge (Notice - all ip commands have a comment with 3#s on top):

# Variables
BRIDGE=my-bridge

TAP1=Tap1
TAP1-BR=TAP1-bridge-side

TAP2=Tap2
TAP2-BR=TAP2-bridge-side

NAMESPACE1=Namespace1
NAMESPACE2=Namespace2

## Create bridge
brctl addbr $BRIDGE

### Bring it up
ip link set dev $BRIDGE up

### Create a Veth pair named Tap1 <--> TAP1-bridge-side
ip link add $TAP1 type veth peer name $TAP1-BR

## Attach one side of Tap1 to bridge
brctl addif $BRIDGE $TAP1-BR

### And the other side to namespace1
ip link set $TAP1 netns $NAMESPACE1

###  Set the interface on the bridge side up
ip link set dev $TAP1-BR up

### Set the interface inside the namespace up - notice that we execute  'ip netns exec' in order to run the inside the namespace scope
ip netns exec $NAMESPACE1 ip link set dev $TAP1 up

####
# Now create another Veth and connect it to the bridge - just change $TAP1 ->$TAP2, $TAP1-BR -> $TAP2-BR and repeat the same steps.. 

## Now you can reach namespace1 from namespace2 and vice versa.

Solution #2.A - Configuring a bridge using open VSwitch:

# Install the package
sudo apt-get install openvswitch-switch

# Now run the exact same commands like before just replace the CLI tool:
## brctl -> ovs-vsctl

# And replace commands:
## addbr -> add-br
## addif -> add-port

Solution #2.B - Configuring a bridge using open VSwitch - replace Veth pairs with internal ports:

# Similar to # 2.A
ovs-vsctl add-br $BRIDGE

# Similar to 2.A - Just with the addition of -- set Interface...
ovs-vsctl add-port $BRIDGE $TAP1 -- set Interface $TAP1 type=internal

### Similar #2.A (and #1)
ip link set $TAP1 netns $NAMESPACE1

### Similar #2.A (and #1)
ip netns exec $NAMESPACE1 ip link set dev $TAP1 up

# Now repeat for $TAP2...
  • This answer would be better if instead of referencing external links (which may become invalid), you'd give a quick rundown how do use ip to make a bridge, and add both interfaces to it. – dirkt Jul 14 at 17:32
  • Of course, thanks for pointing out. – Rotemya Jul 14 at 17:36

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.