Skip to content

Instantly share code, notes, and snippets.

@shawnzhu
Last active June 9, 2023 13:56
Show Gist options
  • Save shawnzhu/715a6b57da9a2d1deedb349fe90b66e3 to your computer and use it in GitHub Desktop.
Save shawnzhu/715a6b57da9a2d1deedb349fe90b66e3 to your computer and use it in GitHub Desktop.
Setup Pi as a router (share Wifi via Ethernet)

Use case

It turns a Raspberry Pi into a router:

  1. Connect to internet via Wifi - this is a one time manual step.
  2. Route traffic from devices connected via ethernet to internet - automatic step as long as devices are connected via ethernet port

Notice it's more like a wireless bridge router but do NAT for all connected devices.

How to use it

  1. setup the wifi network on Pi - via either UI or editing wpa_supplicant.conf
  2. sudo apt update & sudo apt install dnsmasq
  3. save start-wifi-eth-route-2023-06-08.sh and stop-wifi-eth-route-2023-06-08.sh under /usr/local/bin and change ownership to root:
    sudo chown root:root /usr/local/bin/*-wifi-eth-route-2023-06-08.sh
  4. save wifi-to-eth-route.service under /etc/systemd/system/ and enable it:
    sudo systemctl daemon-reload
    sudo systemctl enable wifi-to-eth-route

You can manually start it by running sudo systemctl start wifi-to-eth-route or stop it via sudo systemctl stop wifi-to-eth-route

Or simply reboot your Pi.

Reference

https://github.com/arpitjindal97/raspbian-recipes/blob/master/wifi-to-eth-route.sh

#!/bin/bash
ip_address_and_network_mask_in_CDIR_notation="192.168.2.1/24"
dhcp_range_start="192.168.2.2"
dhcp_range_end="192.168.2.100"
dhcp_time="12h"
dns_server="1.1.1.1"
eth="eth0"
wlan="wlan0"
iptables -t nat -A POSTROUTING -o $wlan -j MASQUERADE
iptables -A FORWARD -i $wlan -o $eth -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $eth -o $wlan -j ACCEPT
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
ip link set $eth up
ip addr add $ip_address_and_network_mask_in_CDIR_notation dev $eth
# Remove default route created by dhcpcd
ip route del 0/0 dev $eth &> /dev/null
rm -rf /etc/dnsmasq.d/* &> /dev/null
tmpfile=$(mktemp)
echo -e "interface=$eth
bind-interfaces
server=$dns_server
domain-needed
bogus-priv
dhcp-range=$dhcp_range_start,$dhcp_range_end,$dhcp_time" > "$tmpfile"
cp "$tmpfile" /etc/dnsmasq.d/custom-dnsmasq.conf
rm "$tmpfile"
#!/bin/bash
ip_address_and_network_mask_in_CDIR_notation="192.168.2.1/24"
eth="eth0"
wlan="wlan0"
iptables -t nat -D POSTROUTING -o $wlan -j MASQUERADE
iptables -D FORWARD -i $wlan -o $eth -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i $eth -o $wlan -j ACCEPT
ip addr del $ip_address_and_network_mask_in_CDIR_notation dev $eth
rm -f /etc/dnsmasq.d/custom-dnsmasq.conf
[Unit]
Description=WiFi to eth route
After=network-online.target
[Service]
Type=oneshot
User=root
ExecStart=/usr/bin/sudo /bin/bash /usr/local/bin/start-wifi-eth-route-2023-06-08.sh
ExecStartPost=/bin/systemctl try-restart dnsmasq.service
ExecStop=/usr/bin/sudo /bin/bash /usr/local/bin/stop-wifi-eth-route-2023-06-08.sh
ExecStopPost=/bin/systemctl try-restart dnsmasq.service
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment