Sharing internet connection to Raspberry PIs, wired to Local Network
Sometime before I write this article, I bought 5 units of Raspberry Pi 3 Model B, along with : 6 pieces of UTP Cat 6 RJ-45 cables, 5 Sandisk 16GB Micro SD cards, an 8 ports TP-Link TL-SF1008D LAN Switch, and an Anker 6 ports USB Power Adapter with 6 USB OTG cables as well. I ordered those parts, to be used for building an ARM based Computers Cluster, wired altogether forming a Local Area Network.
Once I have setup Raspbian Jessie OS on each of these RPis, I then booted them, (by using my Desktop PC’s Keyboard , Mouse & HDMI monitor) and began on assigning host name & static IP on each of the RPis (through editing their /etc/hostname
& /etc/hosts
files). I did this because I want to remote the RPis from my own desktop PC using SSH or Remote Desktop, by specifying their static IP Address.
I wired the Rpis using the UTP Cat6 cables to the LAN Switch, and also connected my Ubuntu Desktop PC into the Switch as well. I planned the topology of the wired Network as shown in this following picture:
I did ping test from an RPi to other connected RPis, from my Desktop PC to RPis, run ssh command from my desktop PC to the RPis, and the outcome were all went well.
A problem arise when I connected my Desktop PC to internet. Either through tethering my iPhone to my PC or connecting the PC to my Wireless MiFi modem, I could get access to internet from my Desktop PC, BUT not from the RPis in the wired LAN. It seemed that my Desktop PC did not share the Internet connection it receives to my RPis.
It took me few hours to understand, how sharing internet connection from my Ubuntu PC to my RPis can be done. Through google-ing efforts and trial-error attempts, I bumped into this article & followed partial steps in the article to get this done.
In this article, I am going to share my experience about how to share the internet connection received by my Ubuntu Desktop PC to the wired Raspberry Pis via Local Area Network.
Configure DHCP Client service on each of RPis
Defining a static IP address on an RPi through writing it on /etc/hosts
file , helped me remoting the RPi from my Desktop PC. However, it’s not enough to help the RPis for recognising my Desktop PC as the Gateway to Internet. Also, we need to define the address of DNS servers that the RPi should lookup to, so that when they browse to a specific URL Site, they could find it by the site’s Domain name (e.g. www.intel.com), instead of by its IP Address (e.g. 103.56.234.57).
Through experimentation, I also found that each of RPis runs DHCPCD server. It is a daemon software which helps the RPi receiving dynamic IP assigned by DHCP server. On one of my RPi, it gave me trouble: it overridden the defined Static IP, replaced it with Dynamic IP (e.g. 169.71.55.12).
In order to prevent this and also `telling` RPis that they should lookup for valid DNS server and recognise my PC as Gateway to the internet, I modified the /etc/dhcpd.conf
file in the RPi as in the following screenshot:
Additional changes in the red-squared lines are interpreted as follow:
interface eth0
— Next configuration lines are applied to the LAN interface of the RPi, theeth0
.static ip_address
— This line defines the static IP of current RPi and the subnet mask (/24)
static routers
— Defines the Gateway’s IP. In this case, it points to the IP Address of my Ubuntu PC.static domain_name_servers
— Define IP addresses of valid DNS servers. In this case, I defined IP Addresses of Google’s DNS servers.
I saved the changes and then reboot the RPi. By the time my RPi was rebooted successfully, I ran ifconfig
to confirm the IP address of eth0
interface has been assigned with the Static IP defined in /etc/dhcpd.conf
earlier.
Then, I ping my ubuntu Desktop and I can confirm that the RPi still can reach out my PC.
I repeated these modification steps against the rest of RPis, except I gave different IP Address but still under same subnet address (e.g. RPi #2 = 192.168.8.202, RPi #3 = 192.168.8.203, etc).
Configure IP Tables on the Ubuntu Desktop PC
At this point, It was still half way to share the internet connection to the RPis. Then, I added Network Address Translation ( NAT
) configuration on my PC, through running thisiptables
command in terminal window:
sudo iptables -t nat -A POSTROUTING -o wlx6466b309ab63 -j MASQUERADE
Then followed by running this command, to ensure that the IP packets routed from any wired RPis, will be forwarded to wlx6466b309ab63
wireless interface:
sudo iptables -A FORWARD -i enp6s0 -o wlx6466b309ab63 -j ACCEPT
Lastly, we run this iptables
command to forward incoming packets from wlx6466b309ab63
interface to corresponding RPis, when the packets have an established initial request.
sudo iptables -A FORWARD -i wlx6466b309ab63 -o enp6s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
To confirm that the changes were applied, I browsed a web page from any of the connected RPi and I confirmed that the web page is loaded on my RPi’s Chromium.
Persist the Changes on IP Tables, permanently
In this moment, I have configured NAT & packets forwarding rules in my Ubuntu Desktop PC for sharing the internet to my RPis. To ensure that these configuration won’t be gone at next time I restart my PC, I installed iptables-persistent
tool (run sudo apt-get install iptables-persistent
for installing it) and then I ran sudo netfilter-persistent save
command in terminal window. This will ensure that the IP Tables Configuration that I has made will be persisted and reloaded at next time I restarted the PC.
Where to go from here
I felt excited, seeing my RPis are fully connected in Local Area Network and also able to access Internet. Next, I would like to setup DNS server(s) in the LAN, so that each of nodes in the LAN would have their own internal domain name, such as mydesktop.nextresearch.local
Once I could setup this, I would like to setup DHCP server in the LAN, so that each of the connected parties will have Dynamic IPs.
Reference(s)