How to configure Raspberry Pi As a Router

The Raspberry Pi 3B+ or 4B has a wireless network adapter and a ceramic antenna on board. It can be turned into a wireless router without additional wireless network card. There are two ways to make our Raspberry Pi as a wireless router. One way is to use OpenWRT firmware on Raspberry Pi to become a powerful and authentic router. Another method is to enable the AP function of the Raspberry Pi wireless network adapter and share its wired network. In this way, we can simply use the starndard Raspbian system, and the advantages of the Raspiberry Pi as a micro server can be used.

The following tutorial guide you how to enable the AP function of the Raspberry Pi 3B+ wireless network adapter, and share its wired network to implement the wireless routing function.

Preparation

Before start the configuration, our Raspberry Pi has connected to a network and can download software from the Internet. We need to download the relevant software first.

sudo apt-get install hostapd dnsmasq

hostapd will enable the AP function of the wireless adapter, while dnsmasq is the DHCP and DNS server.

sudo apt-get install isc-dhcp-server

isc-dhcp-server is for the DHCP service

Note.

  • In this work, it’s better to connect the your Raspberry Pi with a monitor, and directly configure it from keyboard and mouse in the Raspberry Pi CLI.
  • The following configuration don’t request connecting a network. After finished the configuration, you then plugging in a network cable to make your RPI as a router.

Set static IP

  1. Disable the dhcpcd to deny the wlan0 management to avoid setting conflicts.

    sudo nano /etc/dhcpcd.conf

    Add a line at the beginning of the file:

    denyinterfaces wlan0

2. Set the wlan0 static IP

modify the file:
sudo nano /etc/network/interfaces
Add the following settings at the bottom:

allow-hotplug wlan0
iface wlan0 inet static
           address 192.168.0.1
           netmask 255.255.255.0
           network 192.168.0.0
           broadcast 192.168.0.255

192.168.0.1 is the gateway IP address assigned to the Raspberry Pi as a router. This can not be the same as the other gateways address in the LAN. Modify the IP address accordingly if that conflicted with other gateway.

Configure the hostapd

create or edit the hostapd config file, add the following settings.

sudo nano /etc/hostapd/hostapd.conf

interface=wlan0
driver=nl80211
ssid=PiAssistant_WIFI
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GT-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=12345678
rsn_pairwise=CCMP

Above we set the WiFi SSID as PiAssistant_WiFI, and the password as 12345678, just example.

Temporary test if the configuration correct.
sudo /usr/sbin/hostapd  /etc/hostapd/hostapd.conf

Use your mobile phone or laptop to search the available WiFi list, if the SSID we set (PiAssistant_WIFI) was in the list, then it works.

Ctrl+C quit the test, then
sudo nano /etc/default/hostapd

uncomment the #DAEMON_CONF=”” and input the hostapd.conf path.
DAEMON_CONF="/etc/hostapd/hostapd.conf"。

configure the DHCP Service

Backup the orignial dhcpd.conf,
sudo cp /etc/dhcp/dhcpd.conf  /etc/dhcp/dhcpd.conf.bak

configure the dhcpd.conf,
sudo nano /etc/dhcp/dhcpd.conf

subnet 192.168.0.0 netmask 255.255.255.0 {
            range dybamic-bootp 192.168.0.10 192.168.0.100;
            option routers 192.168.0.1;
            option broadcast-address 192.168.0.255;
            option domain-name-servers 8.8.8.8,8.8.4.4;
            default-lease-time 600;
            max-lease-time 7200;
}

Enable the IPv4 Forwarding

Edig the sysctl.conf file,
sudo nano /etc/sysctl.conf

uncomment the #net.ipv4.ip_forward=1,

Set the NAT forwarding via iptables,

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT

sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

Save the NAT settings,
sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”

Enable the NAT service auto start after bootup,
sudo nano /etc/rc.local

add iptables-restore < /etc/iptables.ipv4.nat above exit 0,

Start the service

sudo service hostapd start
Reboot it. Now you have finished the configuration. Your Raspberry Pi become a wireless router for your mobile phones and other devices.

once connecting to the SSID (PiAssistant_WIFI), your RPI will automatically assign an IP address to the devices we connect to. If our Raspberry Pi is connected to the Internet through a wired network, we can also connect to the Internet though Raspberry Pi router.

About the author

Leave a Reply

Your email address will not be published. Required fields are marked *