Privacy By Design For Everyone

Bridging a mobile hotspot to a wired network using Raspberry Pi

Bridging a mobile hotspot to a wired network using Raspberry Pi

This tutorial shows how to use a Raspbery Pi to bridge a mobile hotspot device to a local wired network.

Bridging a mobile hotspot to a wired network using Raspberry Pi

This tutorial shows how to use a Raspbery Pi to bridge a mobile hotspot device to a local wired network.

You can configure a Raspberry Pi single board computer into a router. Raspberry Pi has a Wi-Fi network interface and a wired network interface.

You can connect your Raspberry Pi to a wireless Wi-Fi network such as a Calyx Institute MiFi which has internet connectivity and route the internet traffic to the wired network interface. This way, you can use your Raspberry Pi as a wired router.

In this article, I am going to show you how to configure a Raspberry Pi as a wired router.

Things You Need

In order to configure your Raspberry Pi as a wired router, you need the following things:

  1. A Raspberry Pi single board computer (I use a Raspberry Pi 4)
  2. A Raspberry Pi power adapter or a 2.1A USB power bank
  3. An OS image flasher such as Etcher for flashing Raspbian OS onto a microSD card.
  4. A microSD card
  5. A network switch
  6. Ethernet cables
  7. A Calyx Institute MiFi to connect the Raspberry Pi to
  8. A computer/laptop for configuring the Raspberry Pi

Create installation microSD card

First, visit the official download page of Raspberry Pi OS Lite and download 2021-05-07-raspios-buster-armhf-lite.zip.

Once the download is complete, you can use balena Etcher or other image writing programs for Raspberry Pi to write the 2021-05-07-raspios-buster-armhf-lite.zip image to the microSD card. I will use Etcher in this write up.

You can download Etcher, visit the official website of balena Etcher. Then, download and install Etcher.

I use Etcher on Linux, check the article Install Etcher on Linux x64.

Once Etcher is installed, run Etcher. Click on Select image.

Select your Raspberry Pi OS Lite image that you’ve just downloaded and click on Open.

Insert your microSD card into your microSD card reader and plug it in your computer. Then, click on Select target.

Select your SD card from the list and click on Continue.

Now, click on Flash.

Etcher should start flashing the SD card and once flashed, you should see a boot drive on your computer. Navigate into it.

Create a new file, ssh (without any file extension).

Create a new file wpa_supplicant.conf and type in the following lines to it.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
  ssid="YOUR_WIFI_SSID"
  psk="YOUR_WIFI_PASSWORD"
  scan_ssid=1
  priority=1
}

Make sure to replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD to your Wi-Fi SSID and password.

Now, open the cmdline.txt file and add ipv6.disable=1 at the end of the line to disable IPv6.

Install Raspberry Pi OS

Now, insert the microSD card to the Raspberry Pi, connect one end of the ethernet cable to Raspberry Pi and one end to your network switch. Then, power on the Raspberry Pi.

Once Raspberry Pi starts, it should get an IP address from the Wi-Fi network. You can use any network scanner or your Wi-Fi routers administration page to find out the IP address of your Raspberry Pi.

NOTE: I use a KVM switch so I just select the Pi button on the switch and I am in to login as pi with password raspberry. Then I setup the root user with sudo passwd root and add a password for root user. Then log in as root using su. Once this is done and you reboot, you will be prompted with login and I always login as root. I am not worried about security since once I am completely finished setting up the Pi as a router, I remove it from the KVM switch, reboot iand it runs completely headless.

Now Raspberry Pis come with London, England as the default location, so we might need to setup a few things:

Set your time zone with

dpkg-reconfigure tzdata

Set up your keyboard layout with

dpkg-reconfigure keyboard-configuration

Now configure your network:

NOTE: If you are not logged in as root, you need to use sudo with your commands.)

Create a network configuration file for wlan0 network interface as follows:

sudo nano /etc/network/interfaces.d/wlan0

Now, type in the following lines

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

and save the configuration file by pressing

<Ctrl>+X followed by Y and <Enter>.

Now, create a network configuration file for eth0 network interface as follows:

sudo nano /etc/network/interfaces.d/eth0

Edit the eth0 interface file:

sudo nano /etc/network/interfaces.d/eth0

Now type in the following lines:

auto eth0
iface eth0 inet static
address 192.168.100.1
netmask 255.255.255.0

and save the configuration file by pressing <Ctrl>+X followed by Y and <Enter>.

Now, disable dhcpcd service:

sudo systemctl disable dhcpcd

Now, restart your Raspberry Pi for the changes to take effect.

sudo reboot

Once your Raspberry Pi starts, check the network configuration of wlan0 network interface as follows:

ip addr show wlan0

wlan0 should get an IP address via DHCP.

Also, check the network configuration of eth0 network interface as follows:

ip addr show eth0

A static IP address should be assigned to the eth0 network interface.

Configure DHCP Server

Now, update the package repository cache with the following command:

sudo apt update

Install ISC DHCP server with the following command:

sudo apt install isc-dhcp-server

Press Y and then press <Enter> to confirm the installation.

NOTE: You will get errors when the service tries to start after the installation because the dhcpd.conf has bogus default information. So, just follow the instructions below to configure the dhcpd.conf file, the isc-dhcp-server configuration file and reboot when done.

Once the DHCP server is installed, open the dhcpd.conf file as follows:

sudo nano /etc/dhcp/dhcpd.conf

Set the domain-name and domain-name-servers as follows.

option domain-name "rpi.local";
option domain-name-servers 8.8.8.8, 8.8.4.4;

Scroll down a little bit and uncomment authoritative; line.

Also, add the following lines to the configuration file and save the file.

subnet 192.168.100.0 netmask 255.255.255.0 {
  range 192.168.100.50 192.168.100.240;
  option routers 192.168.100.1;
  option subnet-mask 255.255.255.0;
}

Now, open the /etc/default/isc-dhcp-server configuration file as follows:

sudo nano /etc/default/isc-dhcp-server

Add, INTERFACESv4=eth0 variable to bottom of file and save the file.

Now, reboot the Raspberry Pi.

sudo reboot

Once your Raspberry Pi starts, the isc-dhcp-server service should be active (running).

sudo systemctl status isc-dhcp-server

Configure the Firewall

Now, install firewalld as follows:

sudo apt install firewalld

Press Y then press <Enter> to confirm the installation.

The firewalld service should be active (running) by default.

sudo systemctl status firewalld

Now, allow DHCP traffic through the firewall with the following command:

sudo firewall-cmd --add-service=dhcp --permanent

Allow IP packet forwarding with the following command:

sudo firewall-cmd --add-masquerade --permanent

Allow NTP traffic through with:

sudo firewall-cmd --add-service=ntp --permanent

Finally, reboot your Raspberry Pi.

sudo reboot

Once your Raspberry Pi starts, connect one end of an ethernet cable into the switch and the other end to your Laptop/Desktop or other devices.

Your device should be assigned an IP address via the DHCP server running on your Raspberry Pi and be able to connect to the internet.