Posts: 2
Joined: Wed Sep 13, 2017 2:20 pm

Creating a bridge between two nets in the same LAN using raspberry pi

Wed Sep 13, 2017 2:28 pm


I needed to use raspberry as a bridge for two different nets in the same LAN. It is because it's a small biz LAN without VLAN (not supported by switch or routers). Also, the routers don't allow two different IP address.
I saw a lot of info "how to bridge Wi-Fi and eth0 using Raspi" but not how to bridge two nets in the same LAN. So, I hope it helps to anyone:

In my scenario, I have and nets.
- 0.0/24 does not use DHCP and has 0.1 as a GW (raspberry)
- 1.0/24 uses DHCP with (and secondary dns) as s GW and as a primary DNS.
- With that scenario, I don’t need extra DNS or DHCP (dnsmasq is not necesary)

So, I need raspberry to route 0.0/24 to the Internet and to 1.0/24.

0. Update and upgrade

Code: Select all

sudo apt-get update
sudo apt-get upgrade
1. Static IP for raspberry (PI v1 is enough) and create secondary eth0:0

Code: Select all

sudo /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto eth0
iface eth0 inet static
auto eth0:0
iface eth0:0 inet static

2. DCHP disabled

Code: Select all

pi@raspberrypi:~ $ sudo systemctl disable dhcpcd

3. At this time, there is connectity from the raspberry to 0.0/24 and 1.0/24 LAN. We want to bridge 0.0/24 to 1.0/24

Code: Select all

sudo nano /etc/sysctl.conf
#net.ipv4.ip_forward=1 replace with net.ipv4.ip_forward=1

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

4. Playing with iptables: traffic is forwarded from our eth0:0 interface over to our eth0 connection. This means that anyone connecting to the Ethernet will be able to utilize our eth0 internet connection.
Run the following commands to add our new rules to the iptable:

Code: Select all

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE  
sudo iptables -A FORWARD -i eth0 -o eth0:0 -m state --state RELATED,ESTABLISHED -j ACCEPT  
sudo iptables -A FORWARD -i eth0:0 -o eth0 -j ACCEPT  
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

sudo nano /etc/rc.local
add above exit 0:
iptables-restore < /etc/iptables.ipv4.nat

5. DNS:

Code: Select all

pi@raspberrypi:~ $ sudo nano /etc/resolv.conf
nameserver #local primary DNS server
nameserver #local secondary DNS server
nameserver #google DNS server

Now, you should bridge from 0.0/24 to 1.0/24:
- 0.0/24 can use the Internet
- You can connect from 0.0/24 to 1.0/24.

Reboot your pi and check again :)

*EXRA: To connect from 1.0/24 to 0.0/24, you have to create a route in the 1.1 router. All the traffic from 1.0/24 to 0.0/24 should go through (eth0 of raspberry pi)

W. H. Heydt
Posts: 8398
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Creating a bridge between two nets in the same LAN using raspberry pi

Wed Sep 13, 2017 6:18 pm

Do it just like a WiFi bridge, but use a USB to Ethernet adapter instead of the WiFi for the other connection.

What you are trying to do is, in fact, a bridge. A network bridge echoes all packets received on one connection to the other connection. The smarter version is a router. A router only forwards packets that are for the other connection, so that would work for you as well. (Routers are, I fear, rather misunderstood these days because most people think of then as a LAN-to-WAN connection device, especially since most SOHO routers have a built-in switch. But in actual networking, a router simply does a smart job of interconnecting two networks. The router/bridge relationship is very much like the switch/hub one.)

Posts: 2
Joined: Wed Sep 13, 2017 2:20 pm

Re: Creating a bridge between two nets in the same LAN using raspberry pi

Wed Sep 13, 2017 9:57 pm

Completly agree with you!

In this case, with the solution I posted it works fine! Agree is not the best solution but was the easy way to dont change anything in the LAN, and to not buy more stuff... so, finally the easy temp solution! :)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 44 guests