Preparing a Raspberry Pi to be used as a Gateway
Share Raspberry Pi Wi-Fi Internet to Ethernet
You will now configure the Raspberry Pi to share the Wi-Fi Internet connection to Ethernet. Run the following commands on the Raspberry Pi.
1. (Optional) Update the Raspberry Pi:
sudo apt-get update
sudo apt-get upgrade2. To configure the DHCP client, open the configuration file
sudo vi /etc/dhcpcd.confand paste denyinterfaces eth0 at the end of the file. Close the configuration file (Esc, :w (Enter), :q (Enter)).
3. Next, open the network interfaces
sudo vi /etc/network/interfacesand paste the following lines at the end of the file:
auto eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.66.1
netmask 255.255.255.0
network 192.168.66.0
broadcast 192.168.66.255Close the network interface file (Esc, :w (Enter), :q (Enter)).
4. Install and configure the DHCP service to automatically assign IP addresses for every device in the Ethernet Port. Install dnsmasq and create a new configuration:
sudo apt-get install dnsmasq
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo vi /etc/dnsmasq.confAdd the following lines to the file
interface=eth0
listen-address=192.168.66.1
dhcp-range=192.168.66.50,192.168.66.100,12h
server=8.8.8.8
bind-interfaces
domain-needed
bogus-privand close the configuration (Esc, :w (Enter), :q (Enter)).
5. Reboot the Raspberry Pi.
sudo rebootAfterwards, we need to reopen the SSH connection.
6. Now, we forward the Internet connection from Wifi to the Ethernet port where the PLC is connected by setting IPv4 forwarding. Open the /etc/systcl.conf file:
sudo vi /etc/sysctl.confUncomment the following line by removing the # (line 28):
net.ipv4.ip_forward=1Save and close the file (Esc, :w (Enter), :q (Enter)).
7. Activate IP forwarding by running the following command:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"8. Now, we install iptables and add firewall rules. Run the following commands one at a time:
sudo apt install iptablessudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADEsudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPTsudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT 9. Check that the changes were made and save the rules:
sudo iptables -L -n -vsudo sh -c "iptables-save > /etc/iptables.ipv4.nat"To automatically reload these rules after every reboot, edit the /etc/rc.local file:
sudo vi /etc/rc.localPaste the following line before exit 0:
iptables-restore < /etc/iptables.ipv4.natSave and close the file (Esc, :w (Enter), :q (Enter)).
10. To check the firewall settings, run
routeThe routing table should look like this:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.2.1 0.0.0.0 UG 304 0 0 wlan1
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
192.168.2.0 * 255.255.255.0 U 304 Now, you can check the SDA console to see if the connection to the Gateway was established and if the PLC is connected.
Last updated
