GameReady Router: How I Used A Raspberry Pi for Console & Laptop LAN Connectivity
Tags: raspberrypi, diy, playstation, linux
Published: 2023-10-01 23:13:08


![DIY RPi LAN Router Title Card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xct8pn4xn29lfm2dzxmk.jpg)



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:
>
> 1. **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.
> 2. **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.
> 3. **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:
>
> ```shell
> 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.

```shell
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
```

In the file, set your country= code. Then add your WiFi credentials:

```bash
network={
	ssid="Your WiFi SSID"
	psk="Your WiFi Password"
}
```

Update your package list and upgrade installed packages:

```shell
sudo apt-get update
sudo apt-get upgrade
```

Reboot your Raspberry Pi:

```shell
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.

```shell
# 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:

```bash
# 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:

```shell
sudo reboot
```



### Step 3: Update Network Interfaces

Edit the network interfaces file:

```shell
sudo nano /etc/network/interfaces
```

Add the following:

```bash
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 of `192.168.5.1`, a subnet mask of `255.255.255.0`, a network address of `192.168.5.0`, and a broadcast address of `192.168.5.255`. We have also allowed `wlan0` 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 of `192.168.101.99`, a subnet mask of `255.255.255.0`, a default gateway of `192.168.101.1`, and a DNS server at `192.168.101.1`. It also uses WPA/WPA2 security settings from `/etc/wpa_supplicant/wpa_supplicant.conf`.
>
> 

Now disable DHCP client services:

```shell
sudo systemctl enable networking
sudo systemctl disable dhcpcd

# Edit dhcpcd config to ignore eth0 and wlan0
sudo nano /etc/dhcpcd.conf
```

Add:

```bash
denyinterfaces eth0
denyinterfaces wlan0
```

> ℹ _Technical Information_
>
> 1. `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.
> 2. `denyinterfaces wlan0`: Similarly, this line prevents the wlan0 interface from being managed automatically.
>
> 

Reboot again:

```shell
sudo reboot
```

Verify your settings:

```shell
ifconfig
```

> ℹ _Technical Information_
>
> You should see something similar to this after running the command
>
> ```bash
> 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:

```bash
# 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:

```bash
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:

```shell
sudo systemctl start dnsmasq
```



### Step 5: Configure Port Forwarding and Internet Sharing

Edit the `sysctl.conf` file to enable IP forwarding:

```shell
sudo nano /etc/sysctl.conf
```

Uncomment or add:

```bash
net.ipv4.ip_forward=1
```

Run:

```shell
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
```

Configure NAT and port forwarding:

```shell
# 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:

```shell
sudo nano /etc/rc.local
```

Add above "exit 0" in the `rc.local` file:

```bash
iptables-restore < /etc/iptables.ipv4.nat
```

Finally, reboot:

```shell
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!

![LAN Cable Connected to Raspberry. Pi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh3kwjpd38jsirrii779.JPG)


Here's the result of all that work! My Playstation 5 console is connected to the internet via the LAN we setup.

![Playstation 5 Network Screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bw78v2deiohocemwzsl8.JPG)



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!