Linux Workstation as Router

Since I have some Rasperry PI’s which do not have a WAN connection because they are in an autonomous LAN or just do not have a WLAN module. From time to time I want to do some upgrades. So I need a connection between the LAN containing my PI’s and the WAN.

Same problem when playing around with a new PI without a WLAN configuration or dongle.

The Router

Easiest way to give them connection to WAN is to use my linux workstation/laptop as router. All to do is to run following script on my laptop

#!/bin/bash
sudo sysctl -w net.ipv4.ip_forward=1

sudo iptables -A FORWARD -o wlp2s0 -i enx847beb52b12b -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -o wlp2s0 -s 172.16.100.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o wlp2s0 -j MASQUERADE

In this case:

  • wlp2s0 is the target interface which has a connection to WAN
  • enx847xxx is the LAN which should be routed
  • 172.16.100.0/24 is the network on LAN which is allowed to be routed

To be routed

On the client just set default gatway to ip address of the linux station.

If you do something like this there is already a static IP set on the client.
In my case on raspberry I just set static ip in ‘/etc/network/interfaces’

iface eth0 inet static
address 172.16.100.21
netmask 255.255.255.0
network 172.16.100.0
broadcast 172.16.100.255
gateway 172.16.100.1

where 172.16.100.1 is the ip of my routing pc.

The original file normally has just one line for eth0 like

iface eth0 inet dhcp

After changing ‘/etc/network/interfaces’ I had to reboot my pi, however ‘sudo systemctl restart networking.service’ did not help.

About the author

Adrian Elsener

Add comment

Recent Posts