How to use your Raspberry Pi as a wireless access point

How to use your Raspberry Pi as a wireless access point

The Raspberry Pi can do a lot, especially now that the new Raspberry Pi comes with wireless capabilities already on board. It can take the place of a ton of different (and more expensive) devices – including a router! If you turn your Raspberry Pi into a wireless access point, you can make it act as a router. It’s not the most powerful thing in the world, but it does work, and the project is a lot of fun.

 

How to use your Raspberry Pi as a wireless access point

We’re going to get into the command line a bit here, but this project isn’t really all that difficult. All we’re really doing is using Raspbian and installing a couple packages that give the Pi the ability to do router-like things like assign IP addresses to devices that connect to it.

 

What you’ll need

All you need for this project is a Raspberry Pi 3 and power source, basic peripherals (a keyboard, mouse, and monitor), a micro SD card, and an Ethernet cable. You can also do this project with an older model of Pi if you have a Wi-Fi dongle, though you may end up having to do a bit more troubleshooting.

 

Step 1: Install and update Raspbian

Check out our complete guide to installing Raspbian for the details on this one. Then plug everything in and hop into the terminal and check for updates and ugrades:

sudo apt-get update
sudo apt-get upgrade

If you get an upgrade, It’s a good idea to reboot with sudo reboot.

 

Step 2: Install hostapd and dnsmasq

These are the two programs we’re going to use to make your Raspberry Pi into a wireless access point. To get them, just type these lines into the terminal:

sudo apt-get install hostapd
sudo apt-get install dnsmasq

Both times, you’ll have to hit y to continue. hostapd is the package that lets us create a wireless hotspot using a Raspberry Pi, and dnsmasq is an easy-to-use DHCP and DNS server.

 

Step 3: Free up your wlan0 connection

We’re going to be using the wlan0 connection for the access point, so we have to make sure it’s kept free from other connections. To do this, we’ll edit the dhcpd configuration file. First, access the file:

sudo nano /etc/dhcpcd.conf

Once the configuration file is open, type this line at the bottom:

denyinterfaces wlan0

Now get out of there (Ctrl+X), save by hitting Y, and then hit Enter. Done!

 

Step 4: Set up a static IP for the wlan0 connection

We’re editing another file here. Open the interfaces file with this command:

sudo nano /etc/network/interfaces

You’ll see a section that looks kind of like this:

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

What you want to do here is add a # before the last line of that section, change “manual” to “static,” and insert some text below the second line. When you’re done, it should look like this:

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.50.1
netmask 255.255.255.0
network 192.168.50.0
broadcast 192.168.50.255
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

You can use these exact IP addresses – they’re only used within your home network, so they can be just the same as what we used. Then get out and save the file just like before (Ctrl+X, Y, Enter).

 

Step 5: Reboot your dhcpcd service

Let’s get your Pi used to the changes here.

sudo service dhcpcd restart
sudo ifdown wlan0; sudo ifup wlan0

 

Step 6: Another config file

This time, we’re messing with the hostapd config file. Open ‘er up:

sudo nano /etc/hostapd/hostapd.conf

This should create a brand new file. Type in this:

interface=wlan0
driver=nl80211

hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
macaddr_acl=0
ignore_broadcast_ssid=0

auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP

ssid=NETWORK
wpa_passphrase=PASSWORD

Note that where I have “NETWORK” and “PASSWORD,” you should come up with your own names. This is how you’ll join the Pi’s network from other devices.

 

Step 7: Show hostapd where your new config file is

Just a couple more chores before we’re all set!

sudo nano /etc/default/hostapd

In this file, track down the line that says #DAEMON_CONF=”” – delete that # and put the path to our config file in the quotes, so that it looks like this:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Now get out of there the same way you always do (Ctrl+X, Y, Enter).

Then we’ll do the same thing in a different file:

sudo nano /etc/init.d/hostapd

No quotes this time – just find the line that says DAEMON_CONF= and add the path after the equals sign.

DAEMON_CONF=/etc/hostapd/hostapd.conf

You know the drill by now: Ctrl+X, Y, Enter.

 

Step 8: Replace the dnsmasq config file

We’re going to make a whole new config file for dnsmasq, so first let’s ship the old one out of the way. Just move it via the command line:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.old

It doesn’t matter how you move or rename it.

Then, make a new one:

sudo nano /etc/dnsmasq.conf

That’s enough work for this step, so we’ll edit it in the next step.

 

Step 9: Edit the new dnsmasq config file

Type these lines into your new file:

interface=wlan0
listen-address=192.168.50.1
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.50.50,192.168.50.150,12h

As always, remember to save when you exit.

 

Step 10: Set up traffic forwarding

The idea here is that when you connect to your Pi, it will forward the traffic over your Ethernet cable. So we’re going to have wlan0 forward via Ethernet cable to your modem. This involves editing yet another config file:

sudo nano /etc/sysctl.conf

Now find this line:

#net.ipv4.ip_forward=1

…and delete the “#” – leaving the rest, so it just reads:

net.ipv4.ip_forward=1

The # keeps the line from being read as code, so you’re basically just bringing this line to life here.

Exit, save, yadda, yadda, yadda: Ctrl+X, Y, Enter.

This will all work on your next boot-up, but you can get it working immediately with this command (remember, you’re out of the config file and back in the command line now):

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

 

Step 11: Add new rules to the iptable

Now we need to set things up so that your Pi can translate some IP addresses. Run these lines in the command line to make some key changes to your iptable:

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

That’s two dashes there before the second “state” in line two. Then, run this line to save you work:

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

 

Step 12: More iptable fun

Our changes are saved, but they won’t yet be loaded every time we boot. To fix that, it’s time to tinker in yet another config file.

sudo nano /etc/rc.local

Find the line that says “exit 0” and nothing else. Then, directly above it, add this line:

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

Get out of here in that old familiar way: Ctrl+X, Y, Enter.

 

Step 13: Restart everything

This is it! Everything should work once we reboot it all. Back to the command line:

sudo service hostapd start

If you get an error saying “Warning: Unit file of hostapd.service changed on disk, ‘systemctl daemon-reload’ recommended,” do as it suggests and run the command sudo systemctl daemon-reload.

sudo service dnsmasq start

Now your Pi should be working as a wireless access point. Try it out by hopping on another device and looking for the network name you used back in step 6. To make sure it works every time you fire up your Pi, you can reboot with sudo reboot.

2 Comments on "How to use your Raspberry Pi as a wireless access point"

  1. flasterofreality | July 9, 2017 at 3:51 pm | Reply

    This is one of the best tutorials I have seen for the Pi3 with using onboard Wi-Fi! Thanks for including the /etc/rc.local config for iptables that often gets overlooked.

  2. Thanks for this, it works better than several other tutorials I’ve tried.

    But, what I’m really looking for is not a hotspot with its own IP range, but an extension of my existing Ethernet network with DHCP done on the main router. So that all devices can see each other. Do you have any ideas about how to accomplish that? There is a guide on the Raspberry Pi website, which uses bridge, but it doesn’t work.

Leave a comment

Your email address will not be published.


*