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.


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


  • 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
           broadcast 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


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.

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 netmask {
            range dybamic-bootp;
            option routers;
            option broadcast-address;
            option domain-name-servers,;
            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 *