Networking Raspberry Pi

Using Raspberry Pi as a Wired Router

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

You can connect your Raspberry Pi to a wireless Wi-Fi network 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.

Or, you can connect to a network which has internet connectivity via the wired network interface, create a Wi-Fi hotspot using the Wi-Fi network interface of the Raspberry Pi and router the internet traffic to the Wi-Fi network interface. This way, you can use your Raspberry Pi as a wireless router.

In this article, I am going to show you how to configure Raspberry Pi as a wired router. So, let’s get started.

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
2) A Raspberry Pi power adapter or a 2.1A USB power bank
3) A SD card reader for flashing Raspbian OS onto the microSD card.
4) A microSD card
5) A network switch
6) Ethernet cables
7) A Wi-Fi network to connect the Raspberry Pi to
8) A computer/laptop for configuring the Raspberry Pi

Flashing Raspbian OS on microSD Card:

First, visit the official download page of Raspbian and click on the Download ZIP button of Raspbian Buster Lite image.

Your browser should start downloading the Raspbian Buster Lite image.

Once the download is complete, you can use balena Etcher or other image writing programs for Raspberry pi to write the Raspbian Buster Lite image to the microSD card. I will use Etcher in this article.

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

NOTE: Etcher also works on Linux. To install Etcher on Linux, check the article Install Etcher on Linux.

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

Select your Raspbian Buster 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.

At this point, the SD card should be flashed.

Now, 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.

Powering on Raspberry Pi:

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.

Connecting to Raspberry Pi via SSH:

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.

Once you find the IP address of your Raspberry Pi, connect to it via SSH as follows:

$ ssh pi@192.168.0.105

Type in yes and press <Enter>.

Type in the default password raspberry and press <Enter>.

You should be logged in to your Raspberry Pi.

Configuring the Network:

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

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

Now, type in the following lines and save the configuration file by pressing <Ctrl> + X followed by Y and <Enter>.

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

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

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

Now, type in the following lines and save the configuration file by pressing <Ctrl> + X followed by Y and <Enter>.

auto eth0
iface eth0 inet static
address 192.168.100.1
netmask 255.255.255.0

Now, disable dhcpcd service with the following command:

$ 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.

wlan0 and eth0, both should be configured correctly.

Configuring DHCP Server:

Now, update the APT 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.

ISC DHCP server should be installed.

Now, open the dhcpd.conf file as follows:

$ sudo nano /etc/dhcp/dhcpd.conf

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

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, eth0 to the INTERFACESv4 variable 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

Configuring the Firewall and Enable Packet Forwarding:

Now, install firewalld as follows:

$ sudo apt install firewalld

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

firewalld should be installed.

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

Finally, reboot your Raspberry Pi.

$ sudo reboot

Connecting Clients to the Switch:

Once your Raspberry Pi starts, connect one end of another 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.

So, that’s how you use your Raspberry Pi as a wired router. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.