[LINUX] Turn Raspberry Pi into a router (Wake on LAN [4] over NAT)

Introduction

This is a continuation of Last time. This is what I want to do for the time being.

network_detail.png

This time, "Make Raspberry Pi a router".

-[1] Obtain a domain using GCP and MyDNS -[2] Set up Softether Server on GCP (Connect from iPhone / Raspberry Pi) -[3] Reverse proxy from Apache on GCP to local Raspberry Apache -[4] Make Raspberry Pi a router -[5] Create a Python script for Wake on LAN.

Buy a wired LAN adapter

Since there is only one LAN port on Raspberry Pi, I will write a wired LAN adapter. Like this guy.

Assign a static IP address to the new Ethernet

If you check with the ʻip a command (sudo ifconfig), a new Ethernet Ineterface should be added. This time it was named ʻeth1.

$ sudo ifconfig #ip a seems to be better
eth1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 04:ab:18:3b:af:e2  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Assign a fixed IP. This time, I want to create a new network (192.168.1.0/24) using Raspberry Pi as a router * as shown in the figure below.

network

sudo vi /etc/dhcpcd.conf

Add the following.

/etc/dhcpcd.conf


# External ethernet
interface eth1
static ip_address=192.168.1.1/24 # you can assign arbitrary ip address and subnet mask. Note that client must designate this address as gateway
static routers=**.**.**.** # you can assign arbitrary ip too. if this server's network is from another router, set routers address may be better.(you will not need to set dns masquerade)
static domain_name_servers=8.8.8.8 # see above line's comment

Verification

When you turn on the power of the desktop PC connected to Raspberry Pi and check the ip, IP is assigned to ʻeth1`.

sudo ifconfig # or ip a
# eth1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
#         inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255
#         ether **:**:**:**:**:**  txqueuelen 1000  (Ethernet)
#         RX packets 0  bytes 0 (0.0 B)
#         RX errors 0  dropped 0  overruns 0  frame 0
#         TX packets 0  bytes 0 (0.0 B)
#         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ping 192.168.1.**

IP masquerade settings

Set the IP masquerade. 1st line: Allow transfer from private IP address space to -o. 2nd line: SNAT. Line 3: DNAT.

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT # SNAT
sudo iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT # DNAT

If this is left as it is, it will be temporary and the information will be lost when you restart.

mkdir ~/iptable && cd ~/iptable
sudo iptables-save > iptables.dat
sudo vi /etc/rc.local 

/etc/rc.local


# iptables
iptables-restore < /home/{user name}/iptable/iptables.dat

Your desktop PC should now be connected to the Internet.

reference

Making a router on Ubuntu 16.04 Building a NAT router on Ubuntu 18.04 LTS How to turn a Linux machine into a router Add a USB-LAN adapter to your Ubuntu to make it a router Try to build a new LAN using Ubuntu PC as a router How to Setup a Raspberry Pi DNS Server Install Dnsmasq on Raspberry Pi

Recommended Posts

Turn Raspberry Pi into a router (Wake on LAN [4] over NAT)
Reverse proxy from Apache on GCP to local Raspberry Pi Apache (NAT traversal Wake on LAN [3])
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
Set up Softether Server on GCP (Connect from iPhone / Raspberry Pi) (NAT traversal Wake on LAN [2])
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
Get a domain using GCP and MyDNS (NAT traversal Wake on LAN [1])
Build a Django environment on Raspberry Pi (MySQL)
Try using a QR code on a Raspberry Pi
Build a Python development environment on Raspberry Pi
NAT router on Linux
pigpio on Raspberry pi
Wake on lan on Linux
Cython on Raspberry Pi
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
Testing uart communication on Raspberry Pi
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install ghoto2 on Raspberry Pi (memo)
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4