Setting up a Raspberry Pi 3 as a access point and a router

1. Set Up WiFi Access Point

  • Install Packages
$ sudo apt-get install hostapd dnsmasq

hostapd turns wlan0 into an access point, and Dnsmasq plays an role of DHCP and DNS.

  • Set Static IP Address
    First, set dhcp invalid in wlan0
$ sudo vi /etc/dhcpcd.conf

edit as follows:

~~dhcpcd.conf~~


denyinterfaces wlan0

Next, we need to set a static IP address for the WiFi interface.

$ sudo cp /etc/network/interfaces /etc/network/interfaces_org
$ sudo vi /etc/network/interfaces

At the bottom, add the following:

~~interfaces~~


allow-hotplug wlan0
iface wlan0 inet static
    address 172.20.5.1
    netmask 255.255.255.0
    network 172.20.5.0
    broadcast 172.20.5.255
  • Configure Hostapd
$ sudo vi /etc/hostapd/hostapd.conf

enter the folloing into that file:

~~hostapd.conf~~

interface=wlan0
driver=nl80211
hw_mode=g
channel=5
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
ieee80211ac=0
wmm_enabled=1
ieee80211d=1
ieee80211h=1
country_code=JP
local_pwr_constraint=3
spectrum_mgmt_required=1
wpa=3
wpa_key_mgmt=WPA-PSK
ssid=raspberrypi
wpa_passphrase=**********

You can set ssid, passphrase, and it is better to select a channel which is not interfered by other interfaces.
Test your configuration file works good.

$ sudo hostapd /etc/hostapd/hostapd.conf

If it runs successfully, you can check ssid by other machine.

Then, we make this configuration enable by telling its place to hostapd.

$ sudo vi /etc/default/hostapd

Uncomment # and replace the line #DAEMON_CONF=""with:
DAEMON_CONF="/etc/hostapd/hostapd.conf"

Set hostapd runs automatically

$ sudo systemctl unmask hostapd.service
$ sudo systemctl enable hostapd.service
  • Configure Dnsmasq

Edit Dnsmasq configuration

$ sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf_org
$ sudo vi /etc/dnsmasq.conf

paste in the text below

~~dnsmasq.conf~~


interface=wlan0
listen-address=172.20.5.1
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=172.20.5.100,172.20.5.200,24h

Then restart the Raspberry Pi

$ sudo reboot

Now you can use Raspberry Pi as a Access Point.

2. Enable Packet Forwarding

-Configure NAT(Network Address Translation)

Edit /etc/sysctl.conf: uncomment net.ipv4.ip_forward=1 by deleting #.

$ sudo vi /etc/sysctl.conf
~~sysctl.conf~~

net.ipv4.ip_forward=1

Next, configure NAT(Network Address Translation).

$ 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

After reboot, Raspberry Pi will revert back to its previous state. So it is needed to save and restore these settings.

$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

$ sudo vi /etc/rc.local

In rc.local script, just above the exit 0 add the following

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

That's all for configuration.
Restart Raspberry Pi to test the setting works.

$ sudo reboot

Reference

Setting up a Raspberry Pi 3 as an Access Point - SparkFun Learn