Raspi as a router

Assuming Ubuntu LTS, Here are the main steps.

Setup

  • eth0 connected to laptop

  • wlan0 connected to internet (via home network) either static IP or dynamic

Steps

  • configure a static IP in /etc/netplan for the local raspi network. say 192.168.10.1/24

  • use dnsmasq as a DHCP Server, to provide a dynamic IP address, and a default gateway

  • enable IPv4 forwarding

  • MASQUERADE

Tools to install

sudo apt install dnsmasq # for DHCP server
sudo apt install iptables-persistent # for IPv4 and IPv6 Persistent config

Code

First place a config file in /etc/netplan

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      optional: false
      addresses:
        - 192.168.10.1/24
  wifis:
    wlan0:
      access-points:
        starport:
          password: 1uEu@xjajo@AynX
      addresses:
        - 2601:0181:c400:121b::bebe/64
        - fde8:b060:bd2c:0001::bebe/64
        - 2601:0181:c400:121b:b060:bd2c:0001:bebe/64
        - 192.168.0.21/24
      dhcp4: false
      dhcp6: false
      gateway4: 192.168.0.1
      gateway6: "2601:181:C400:121B:1E3B:F3FF:FED2:6309"
      nameservers:
        addresses: [192.168.0.1]    # DNS Server IP addresses.

Caution

Since the wlan0 wifi is using a static IP, a default gateway must be provided

Second dnsmasq should bind to a specific interface, otherwise it conflicts with systemd-resolv

bind-interfaces # to bind to specific interfaces rather than all available interfaces
interface=eth0 # specify the interface to bind to
dhcp-range=192.168.10.100,192.168.10.120,255.255.255.0,24h # DHCP server config

Third, enable IPv4 forwarding.

  • uncomment net.ipv4.ip_forward=1 for the next reboot

  • run sudo sysctl -w net.ipv4.ip_forward=1 to enable in this session

Forth, enable NAT MASQUERADE

  • sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE to enable right away

  • sudo /sbin/iptables-save && sudo /sbin/ip6tables-save to persist iptables settings