If you’re like me, you’ve probably found yourself in a situation where you need a LAN connection—yes, even in 2023. Maybe you’re somewhere in a building with strong WiFi but no LAN outlets to connect your PlayStation 5 or your laptop. Doesn't that just suck?
You see, when it comes to gaming or just browsing the internet in general, LAN doesn’t just beat WiFi, it annihilates it. It’s the silent guardian, the watchful protector of seamless gaming (and surfing the internet, duh!). Lower latency, fewer packet drops, and that steady stream of data, it’s like having a VIP pass in the chaotic festival of online gaming (and ... you get the point).
Anyway, here’s how to turn your Raspberry Pi into a router that can act as a makeshift LAN outlet.
Prerequisites:
- Raspberry Pi connected to the internet via WiFi (I used a Raspberry Pi 3)
- A LAN cable
- An internet-capable device with a LAN connector (e.g., Laptop or Gaming Console; in my case, a PlayStation 5)
This guide assumes you’ve already installed an operating system (preferably a Linux distribution) on your Raspberry Pi and have internet access.
ℹ Technical Information
Introduction to Core Concepts:
Before diving into the steps to transform your Raspberry Pi into a router, it's essential to grasp some networking fundamentals:
- DHCP (Dynamic Host Configuration Protocol): DHCP is a network protocol used to assign IP addresses dynamically to devices on a network. When a device connects to a network, DHCP allocates an available IP address, enabling the device to communicate with others on the same network.
- Static IP: Unlike DHCP, which assigns IP addresses dynamically, a static IP is a fixed address assigned to a device. This consistent address ensures that other devices on the network can reliably communicate with it, which is crucial for servers or, in this case, your Raspberry Pi acting as a router.
- NAT (Network Address Translation): NAT is a method used to modify network address information in packet headers while in transit across a traffic routing device. It allows a single device, such as a router, to act as an agent between the public network and a local network, which enables a single unique IP address to represent an entire group of computers to anything outside their network.
Step 1: Connect Raspberry Pi to WiFi
After booting your Raspberry Pi and accessing the Command Line Interface (CLI), run the following command to edit the wpa_supplicant.conf
file:
ℹ Technical Information
It would be a good idea to back up your wpa config file before carrying out the next few commands:
sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.bak
This would help if you ever need to revert the changes made to what you had previously.
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
In the file, set your country= code. Then add your WiFi credentials:
network={
ssid="Your WiFi SSID"
psk="Your WiFi Password"
}
Update your package list and upgrade installed packages:
sudo apt-get update
sudo apt-get upgrade
Reboot your Raspberry Pi:
sudo reboot
Step 2: Set a Static IP Address for Your Ethernet Adapter
To setup a gateway for the device that you want to connect to the Ethernet port, you will need to set a static IP address. This fixed address ensures that the devices connected to your makeshift router can consistently find it on the network, aiding in reliable communication. Without a static IP, the Raspberry Pi's address could change over time, disrupting the network setup and requiring reconfiguration.
# Install dnsmasq for DHCP and DNS services
sudo apt-get install dnsmasq
# Edit dhcpcd configuration
sudo nano /etc/dhcpcd.conf
At the bottom of the dhcpcd configuration file, paste the following:
# set eth0 (i.e the LAN port) to a static IP of 192.168.101.98, a gateway of 192.168.101.1 and uses a DNS server at 192.168.101.1
interface eth0
static ip_address=192.168.101.98/24
static routers=192.168.101.1
static domain_name_servers=192.168.101.1
# set wlan0 (wireless) to a static IP of 192.168.101.99, a gateway of 192.168.101.1 and uses a DNS server at 192.168.101.1
interface wlan0
static ip_address=192.168.101.99/24
static routers=192.168.101.1
static domain_name_servers=192.168.101.1
Once this is done and saved, reboot the device again by running:
sudo reboot
Step 3: Update Network Interfaces
Edit the network interfaces file:
sudo nano /etc/network/interfaces
Add the following:
auto eth0
iface eth0 inet static
address 192.168.5.1
netmask 255.255.255.0
network 192.168.5.0
broadcast 192.168.5.255
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.101.99
netmask 255.255.255.0
gateway 192.168.101.1
dns-nameservers 192.168.101.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
ℹ Technical Information
What we have done is make sure that eth0 starts automatically on boot (
auto eth0
) and uses a static IP address of192.168.5.1
, a subnet mask of255.255.255.0
, a network address of192.168.5.0
, and a broadcast address of192.168.5.255
. We have also allowedwlan0
to be "hotplug-able" meaning that a device such as a WiFi dongle can be plugged into the raspberry without needing a reboot. It also assigns the Raspberry Pi a static IP address of192.168.101.99
, a subnet mask of255.255.255.0
, a default gateway of192.168.101.1
, and a DNS server at192.168.101.1
. It also uses WPA/WPA2 security settings from/etc/wpa_supplicant/wpa_supplicant.conf
.
Now disable DHCP client services:
sudo systemctl enable networking
sudo systemctl disable dhcpcd
# Edit dhcpcd config to ignore eth0 and wlan0
sudo nano /etc/dhcpcd.conf
Add:
denyinterfaces eth0
denyinterfaces wlan0
ℹ Technical Information
denyinterfaces eth0
: This line tells the system not to manage the eth0 interface using DHCP or other automatic configuration methods. Essentially, eth0 will be ignored by the dhcpcd service or similar networking services.denyinterfaces wlan0
: Similarly, this line prevents the wlan0 interface from being managed automatically.
Reboot again:
sudo reboot
Verify your settings:
ifconfig
ℹ Technical Information
You should see something similar to this after running the command
eth0: flags=XXXX… inet 192.168.5.1 netmask 255.255.255.0 broadcast 192.168.5.255 …… Other confuration stuff wlan0: flags=XXXX… inet 192.168.101.99 netmask 255.255.255.0 broadcast 192.168.101.255
Step 4: Set Up DHCP Server
We installed dnsmasq earlier. Now stop the service, back up the existing configuration, and add our settings:
# Stop the service
sudo systemctl stop dnsmasq
# Backup existing configuration
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
# Edit configuration
sudo nano /etc/dnsmasq.conf
Add:
interface=eth0
listen-address=192.168.5.1
dhcp-range=192.168.5.50,192.168.5.100,12h
server=8.8.8.8
bind-interfaces
domain-needed
bogus-priv
ℹ Technical Information
In summary, this is what those lines of code do:
- Operates on eth0 interface.
- Listens on IP 192.168.5.1.
- Allocates IPs between 192.168.5.50 and 192.168.5.100 with a 12-hour lease.
- Uses Google’s DNS (8.8.8.8) for upstream queries.
- Binds only to eth0 and includes some security and efficiency settings (domain-needed, bogus-priv).
Restart the service:
sudo systemctl start dnsmasq
Step 5: Configure Port Forwarding and Internet Sharing
Edit the sysctl.conf
file to enable IP forwarding:
sudo nano /etc/sysctl.conf
Uncomment or add:
net.ipv4.ip_forward=1
Run:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Configure NAT and port forwarding:
# Add IP tables rules
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
# Save changes
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Persist these settings by running:
sudo nano /etc/rc.local
Add above "exit 0" in the rc.local
file:
iptables-restore < /etc/iptables.ipv4.nat
Finally, reboot:
sudo reboot
After rebooting, connect a LAN cable to the Raspberry Pi and the other end to the device needing internet access. You’ve successfully turned your Raspberry Pi into a router!
Here's the result of all that work! My Playstation 5 console is connected to the internet via the LAN we setup.
I hope this article has been helpful as I had a use case that made me scour the internet for details on how to achieve this. So this is written not just for anyone else that might have this challenge but also as a documentation for myself in the event that I might need to do this again!
Top comments (1)
Thinking about compressing all of this into a script. Would that be a good idea? Considering this guide requires a couple of reboots of RPi 🤔