Connect a Raspberry to WWW, via Ethernet and a WiFied RasPI

ethernetThis is my first article in English so please be kind, it is not my language so forgive any mistake... 😳

I decided to write in English 'cause I've found many people with the same problem, and the different solutions found on the web were often useless. The problem was to connect a Raspberry on the Internet without adopting a WiFi dongle, letting the only 2 USB ports availables for mouse and keyboard (I didn't want to use a USB hub). So I thought to connect the Raspberrys via Ethernet and use one of them (already connected via WiFi) as gateway, like in this layout:

layout connessione

The steps are:

  • Configuring the LAN network between the 2 RasPI
  • Preventing WiFi connection drop when plug in Ethernet Cable
  • Configuring iptables to forward internet from WLAN to Cable LAN

Configuring Ethernet network

First, you have to set one RasPI as a client with a static IP and the other RasPI as gateway (with static IP); to do this, type in the shell:

sudo nano /etc/network/interfaces

and then, modify the opened file:

allow-hotplug eth0
iface eth0 inet static

save the new settings (Ctrl + X, Yes). On the gateway-RasPI change eth0 settings:configurazione ethernet

allow-hotplug eth0
iface eth0 inet static

in the wlan0 settings, add:

allow-hotplug wlan0

and save all. Done? Very well. You've just prepared your Ethernet link (with IP 192.168.0.x). If you want, you can also use a network manager with a graphical interface, such as Wicd. You can use direct or crossover cable with no preferences (modern ethernet controllers are "auto sensing": I tried both of them and the link worked good with either). First step completed!

Preventing WiFi connection drops

Probably you've already seen that when you plug RJ45 cable, WiFi connection drops immediately. The problem is caused by "ifplugd", a daemon installed in Raspbian (and many other Debian distros) that disables the slower network when it finds another faster one -in this case Ethernet- (ifplugd thinks that you want to work only with the faster connection). The final solution is to uninstall the daemon, and then type in the shell:

sudo apt-get remove ifplugd

Before adopting this solution, I also tried a script in bash (found on GitHub)  that controls WiFi link every 60 seconds, and re-initializes the interface if WiFi is down. This solution worked just few times, but I keep on using it also when I unistalled ifplugd.

Configuring IPtables

Last step! You have to configure IPtables if you want raspberry's client requests seems like coming from your WiFi connected Raspberry. First you have to create the rules, typing:

sudo iptables -A FORWARD -o wlan0 -i eth0 -s -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE 

To store the rules permanently, you have to install an application:iptables foe ethernet forwarding

sudo apt-get install iptables-persistent

and type the command to save the settings:

iptables-save > /etc/iptables/rules.v4

Now reboot Raspberry and enjoy Internet! 🙂

  • I really like your writing style, good information, regards for putting up abebaaecdeegeeba