Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Raspberry Pi 3 access-point-setup
# This version uses September 2016 rpi jessie image, please use this image
if [ "$EUID" -ne 0 ]
then echo "Must be root"
if [[ $# -lt 1 ]];
then echo "You need to pass a password!"
echo "Usage:"
echo "sudo $0 yourChosenPassword [apName]"
if [[ $# -eq 2 ]]; then
apt-get remove --purge hostapd -y
apt-get install hostapd dnsmasq -y
cat > /etc/systemd/system/hostapd.service <<EOF
Description=Hostapd IEEE 802.11 Access Point
ExecStart=/usr/sbin/hostapd -B /etc/hostapd/hostapd.conf -P /var/run/
cat > /etc/dnsmasq.conf <<EOF
cat > /etc/hostapd/hostapd.conf <<EOF
sed -i -- 's/allow-hotplug wlan0//g' /etc/network/interfaces
sed -i -- 's/iface wlan0 inet manual//g' /etc/network/interfaces
sed -i -- 's/ wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf//g' /etc/network/interfaces
cat >> /etc/network/interfaces <<EOF
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# Added by rPi Access Point Setup
allow-hotplug wlan0
iface wlan0 inet static
echo "denyinterfaces wlan0" >> /etc/dhcpcd.conf
systemctl enable hostapd
echo "All done! Please reboot"

Lewiscowles1986 commented Mar 6, 2016

This is specifically for the raspberry pi 3. The script should work with any wireless card setup as wlan0 that does not require a driver, please provide feedback, let me know any issues.

The use-case is setting up a private network, maybe for some rPi zero's to connect to, but the sky is the limit; you could bolt on a dns-server and provide a walled-garden internet experience like facebook (please don't), or host your personal project to showcase at interview.

Now allows you to specify the Access point (AP) name, so it does not have to be whatever it was before.

I suggest to share the eth0 connection that you add
in /etc/sysctl.conf
and execute the following iptables command:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

This will allow the traffic to pass through the wifi AP and out via the ethernet connection.


Lewiscowles1986 commented Mar 15, 2016 edited

Hi @vhgarandil,

Thanks for the suggestions, this is not a script to setup a wireless->ethernet bridge, however it is a good idea for an additional script to allow using a device (doesn't matter really if it's ethernet or another wifi dongle).

The main purpose of this script is to provide an access point with guest network (for now). But I may consider adding this, as many have asked for it

I am a very very new user having just received my rPi3 running Raspbian. In at the deep end I know but I thought this function would be useful. I have been able to compile the script and even run it, but am at a loss at getting past "you need a password!"
How do I respond to the usage advice line? I have tried to input a password but cannot get beyond causing the error 'command not found'. What am I missing?

Can you give me a prod in the right directions? Thanks if you can take the time to no doubt tell me the obvious!

I find this very useful. Could you add some options to help the user to share the eth0 connection through wifi.


Lewiscowles1986 commented Mar 25, 2016

Hi @ipplepen

So once you run the script, you can run
sudo password wifiapname
and it will setup with the password "password", and the SSID "wifiapname"


Lewiscowles1986 commented Mar 25, 2016


Thanks, I'm looking right now at releasing something separate for that, so that you can share eth0, or any other networking device; I believe @vhgarandil suggestion (see above) will work for bridging eth0 as the primary connection, but to make a script do this; I think they should be separate scripts for people like me that don't want a bridge or repeater station.

elck commented Mar 27, 2016

I have some problems with this script.
Mind you, I am a total beginner with linux
On line 19 there is a var $APSSID (should that be APSSID ?) whilst on line 40 I see ssid=$APNAME (shouldn't that be $APSSID ?)

The other thing is an error I get on line 50 (when I remove the redirection that is)
Failed to create interface mon.wlan0 :-95 (Operation not supported)
wlan0 could not connect to kernel driver

What am I doing wrong please?


Lewiscowles1986 commented Mar 28, 2016

@elck nice catch on APSSID vs APNAME, must have renamed a var and forgot to copy to / update all lines (managing this via git might have been a better idea)

can I ask if you are using this on a raspberry Pi 3 using raspbian?

Most of the steps are entirely applicable on any debian based Linux on the Pi3, but raspbian has the official rPi3 hardware libs; so I'd advise using that (they have a lite edition now)

Thanks for the feedback, keep me updated

crone commented Apr 3, 2016

I have tried this script. All seems to be ok on the RPI3 side. But non of my devices at home are able to connect to the AP.

Any help would be appreciated.


Lewiscowles1986 commented Apr 4, 2016

can I ask the exact commands you ran?

Most devices should be able to connect to the rPi3, although it's not an internet pass-through Access-Point; it's so you can access your Pi to help to configure it with fewer wires

Good code you have here, but it just does not work for me,even though i am using raspbian. I've been trying for a few days to get the Pi3 AP working but it's always the same thing, saying that it could not start mon.wlan0 but the AP is enabled(i've attached a screenshot). Even so i can not connect with any device to the ap. Perhaps is there something i haven't installed?


i copy the code. and type chmod-R 755
if i start it. i get the following error:

root@HaRuVerhuur-Matrix:/home# ./ testing aptest
You need to pass a password!
sudo ./ yourChosenPassword [apName]

Grees Ruben Dijk

marcelerkel commented Apr 21, 2016 edited by Lewiscowles1986

Try changing line 8 to:

if [[ $# -lt 1 ]];

The current code checks if the number of command line parameters is not equal to one (-ne 1). Since you specify the optional apName parameter, you have two parameters which causes the test to fail and hence the 'You need to pass a password!' error message.
Even better would be to test for less than one or greater than two, but I leave that as an exercise for @Lewiscowles1986 :)

_Author FYI_
I won't be taking part in "exercises" or assignments 😉

csatt commented Jun 4, 2016

This works for me, but only if I add the following line to rc.local:

service hostapd start

I put it right before the "service dnsmasq restart".

imac8t commented Jun 11, 2016

Syntax error: "fi" unexpected (expecting "then")

How can switch wifi from "access point" mode to "standard" mode ?
Is it enough if i just remove lines from rc.local file and reboot?
Is there any other service like "dnsmasq" that needs to be swicthed off in "standard" mode?

I have this problem.
pi@raspberrypi:~ $ sudo python
File "", line 3
if [ "$EUID" -ne 0 ]
SyntaxError: invalid syntax
pi@raspberrypi:~ $


ghost commented Jun 28, 2016

@lvhailong This is a shell script as denoted by the shebang at the top and .sh extension.


Lewiscowles1986 commented Jun 29, 2016

Thanks @usertaken

@navaneetharaopy you would have to edit the /etc/rc.local, but also disable hostapd and dnsmasq services

sed -i -- 's/wlan0/wlan999/g' /etc/rc.local should do for rc.local, then sed -i -- 's/wlan999/wlan0/g' /etc/rc.local if you want to restore on wlan0


Lewiscowles1986 commented Jun 29, 2016 edited


This works for me, but only if I add the following line to rc.local:

service hostapd start

I put it right before the "service dnsmasq restart".

that should not be required as dnsmasq is started on the next line. Hostapd is started within the script too!

csatt commented Jun 30, 2016

I see where dnsmasq is started. Where is hostapd started? The "hostapd - B ..." command on the last line fails (for me) unless the service is started first. Do you have to run "sudo update-rc.d hostapd enable" or something like that for it to work without explicitly starting the service in rc.local?


I'm having a really similar problem, getting the errors

Failed to create interface mon.wlan0: 95 (Operation not supported)
wlan0: could not connect to kernel driver

but not the other stuff.

Anyone have a solution?

I tried sudo hostapd /etc/hostapd/hostapd.conf
Then i got

ioctl[SIOCSIWMODE]: Invalid argument
Could not set interface to mode(3)!
Could not set interface to master mode!
rtl871xdrv driver initialization failed.

No one can help with @adiroman, @SammoMate AND my problem?


Lewiscowles1986 commented Aug 27, 2016

@marianomarini @adiroman @SammoMate I just used the script on a fresh jessie-lite image from the raspberry pi website. The only issue I encountered was one line for the access-point name which should now be fixed. This works with fresh jessie-lite image, not sure what could be interfering

Everything works perfectly, but I can not access the internet on my phone when connected to my pi. What should I change in order to be able to connect to the internet?

9H5G commented Sep 6, 2016

Many thanks @lewiscoles1986 - worked out of the box! Thank you :)

(If the bridge script were to come along that would be fantastic :))


Lewiscowles1986 commented Sep 8, 2016

I'll add the bridge script soon and link here

mehranexpert commented Sep 8, 2016 edited

Not work for me .

root@kali:~/git/ap# ./  testtest mehran
./ line 19: rPi3=mehran: command not found
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  freeglut3 libgnutls-deb0-28 libpng12-0 libpoppler57
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
0 upgraded, 0 newly installed, 1 to remove and 446 not upgraded.
After this operation, 981 kB disk space will be freed.
(Reading database ... 233701 files and directories currently installed.)
Removing hostapd (1:2.5-2+v2.4-2) ...
Purging configuration files for hostapd (1:2.5-2+v2.4-2) ...
dpkg: warning: while removing hostapd, directory '/etc/hostapd' not empty so not removed
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (230-7) ...
Reading package lists... Done
Building dependency tree       
Reading state information... Done
dnsmasq is already the newest version (2.76-4).
The following packages were automatically installed and are no longer required:
  freeglut3 libgnutls-deb0-28 libpng12-0 libpoppler57
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 446 not upgraded.
Need to get 0 B/491 kB of archives.
After this operation, 981 kB of additional disk space will be used.
Selecting previously unselected package hostapd.
(Reading database ... 233677 files and directories currently installed.)
Preparing to unpack .../hostapd_1%3a2.5-2+v2.4-2_armhf.deb ...
Unpacking hostapd (1:2.5-2+v2.4-2) ...
Processing triggers for systemd (230-7) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up hostapd (1:2.5-2+v2.4-2) ...
update-rc.d: We have no instructions for the hostapd init script.
update-rc.d: It looks like a network service, we disable it.
insserv: warning: current start runlevel(s) (empty) of script `hostapd' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `hostapd' overrides LSB defaults (0 1 6).
Processing triggers for systemd (230-7) ...
All done!


Lewiscowles1986 commented Sep 12, 2016 edited

root@kali yeah, looks like raspbian @mehranexpert... this is for specific hardware and software _RASPBERRY PI 3 & OFFICIAL RASPBIAN_

You might want to checkout

doktor500 commented Sep 26, 2016 edited

I have the same issue than @adiroman

I install the latest version of RASPBIAN JESSIE LITE on my Raspberry Pi Model 3
I ssh into it and run:

chmod +x
sudo apt-get update
sudo ./ password apname

All seems to be good, but when I run sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

This is the output:

Configuration file: /etc/hostapd/hostapd.conf
Failed to create interface mon.wlan0: -95 (Operation not supported)
wlan0: Could not connect to kernel driver
Using interface wlan0 with hwaddr b8:27:eb:fc:f9:b4 and ssid "dmcom-mad"
wlan0: interface state UNINITIALIZED->ENABLED

Lewiscowles1986 commented Oct 3, 2016

why are you running anything, just reboot the pi, it should automatically start... I'm going to make a slightly modified one using systemd service instead of hacky approach to /etc/rc.local, but I've just run this successfully on the pixel release and the latest jessie lite release.


Lewiscowles1986 commented Oct 3, 2016


cd /boot
sudo reboot
sudo cp /boot/wpa_supplicant.conf /etc/wpa_supplicant/
sudo reboot
chmod +x  2cOGZuL
sudo apt update
sudo 2cOGZuL password apname

I did see some error messages on boot, but I still have the apname show up in my list (which is new for me, as I generally pick one)



Lewiscowles1986 commented Oct 4, 2016 edited

Also there is now a newer version using systemd, and this companion script for anyone wanting to pass through an adapter.

simar88 commented Oct 6, 2016 edited

Hello everyone, i'll always receive "errors were encountered while processing:hoastapd" during the setup.
Do you have any idea?
Thanks in advance,

Dear All,

I want to use Rpi 3's wifi in both Access Point and Station mode simultaneously or at a time one but without reconfigure(reboot the pi).And now, i did to both mode individual configure and work its fine but problem is every time configure change for individual mode.
Please guide me.

nenoork commented Oct 10, 2016 edited

How to setting up a kind of bridge between eth0 and wlan0 like @vhgarandil told. I don't get it it's not working for me. Please you upload a kind of other script just for doing a bridge? (I'd like to access to internet by the AP) I'm still waiting for it. Keep updating i like what you do @Lewiscowles1986!


Lewiscowles1986 commented Oct 13, 2016

@nenoork, there is another script linked above... Please go to this url


Lewiscowles1986 commented Oct 13, 2016

@PratikGajera I'm really sorry I don't understand what you are asking

I am asking about that which way to configure wifi that run it both mode station and Access Point.

dreetie commented Oct 22, 2016

Script runs great, but how do you revert back to station mode?


Lewiscowles1986 commented Oct 28, 2016

systemctl disable hostapd
systemctl disable dnsmasq

and adding

wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf to /etc/interfaces should do it. Really at the point you are doing that this quick and dirty version won't work too well for you

Just ran this on my raspberry pi 3 and now I no longer have a wifi interface in raspbian. Does this require having another usb wifi adapter plugged in?

Also, How do I completely remove this?


Lewiscowles1986 commented Oct 31, 2016

@goodpunk6 this does not require a second wifi adapter unless using as a range extender...

@goodpunk6 to remove this try the following

sudo apt-get remove --purge hostapd dnsmasq

then add wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf to /etc/interfaces inside the /dev/wlan0 section

alternatively backup your home folder and re-flash the jessie-pixel or jessie-lite image

It would be really nice to have a way to automacally revert the chages made by this script, so that one could get back to normal use of your RPi as easily.

dreetie commented Nov 3, 2016

@goodpunk6 I've got the same problem as you: no wireless interfaces found

how i connect rspi3 to a network?


Lewiscowles1986 commented Nov 8, 2016

@heniotierra. More than happy to see a fork that does. I don't think shell is a good place to do this, it's more of a DSC deal where existing tools could take this and make for example an ansible playbook that stacks little scripts like this to specify a desired state of a raspberry pi

I'm having the same issue as @adiroman and @doktor500.
Apologies if I missed it but did either of you get a resolution to your problem?

Clint67 commented Jan 11, 2017 edited

GREAT JOB my firend!!
I was struggling among a lot of sligtly different directions to get my Rpi3 working as an AP.
Some of them didn't work at all, some just get the Rpi3 working as AP ignoring the other commands to route traffic to share the eth0 internet connection.
Your script worked at first shot and combined with your other script at
I got it working also as a AP with traffic forwarding,...

...but my question is:

Can the Rpi3 work like any other 'out of the box' AP on the market?

I mean, 'transparent' like any AP I can buy on store shelfs? So, the wifi devices connected to the AP 'share' the same IP class as the primary router. Or, even better, get the IP address from the DHCP working on the primary router?

I am quite out of the IP configurations world, and I am just wondering if I can set the static IP class of the wlan0 to join my home lan configuration and have wireless devices on the same subnet of the primary router.

This because I need to send/receive some UDP packets to devices 'wifi connected' to the lan through the Rpi (on other ports than the 80)
Any idea is welcome...

And, once again, thank you very very much for sharing your code!


Lewiscowles1986 commented Jan 14, 2017

@Clint67 thanks 👍

You can alter the dnsmasq config file to allocate to the same subnet if that is what you'd like. You could also adjust the mask or change the rules from the other script but I've never attempted nor have the inclination to make it compete with AP makers. Fork it and share your changes with the world!


Hi Lewis,

First off thank you for creating such an easy to use script and helping troubleshoot. I'm trying to set up a new Pi3 to create a local network for several other Pi3s and I stumbled upon your script (among several sketchy or out-of-date tutorials). I ran the script on a brand new installation of Rasbian using NOOBS but I'm having a couple issues. If I don't reboot after the script runs then I can see the newly created AP on my phone and laptop, but they are not being assigned an IP address. If I reboot the Pi3 then the AP doesn't even show up.

I'm a bit of a Linux noob so I'm unsure of how to go about troubleshooting; it appears as though hostapd and dnsmasq are both running after the reboot but they aren't functioning properly. Do these programs create a log file or anything I can look at? Is there any monitoring feature so I can see error/other messages in real-time?

Thanks again!

tverboon commented Jan 21, 2017 edited

Great script! Only thing I had to configure was the following:

sudo nano /etc/default/hostapd

Otherwise the systemd service would exit right away. Not sure why, because the config is passed as a parameter.


Lewiscowles1986 commented Jan 25, 2017 edited

@tverboon that is strange, as you say we specify hostapd inside the service we completely generate from scratch

@dfarley1 this wasn't written for NOOBS and hasn't been tested on NOOBS, it's written for jessie pixel and jessie lite

On logging I haven't set it up, but try sudo ls -lhaR /var/log it should recursively list all log files. If there are not specific logs it may end up in syslog (I Have to admit I pretty much record what I do to get working, I'm by no means an expert with either. Much like yourself I became fed-up of tutorials asking people to attend the installation and have more than minimal input)

SamGG commented Feb 22, 2017

Thanks for the script!
I ran it step by step on a minibian after having installed the wifi
Nevertheless, I didn't use the line 67, because it does not make sense to me.
@adiroman, @doktor500, @botzen67 I encountered the same error, but it disappeared after a couple of reboot
NB: the length of the password must be at least 8 characters
I added a command to prevent energy saver from stopping the wifi interface:

ikester commented Mar 5, 2017

Thank you for this script. I was having trouble getting this to work after boot using 'hypriot'. If I manually restarted the service I could see the SSID but after a fresh reboot it just wouldn't show up. There were no errors in the logs and the output look exactly the same as when it worked manually. I figured it was a timing issue with the startup sequence. I finally got it to work by changing line 3 of the Systemd unit file to this instead:
It now shows up just fine after a reboot.

How to disable the AP script after use?

This is great! I ran into a problem however because the password length must be at least 8 character. It'd be great to add a check like:

if [ $APPASS -lt 8 ]
  echo "Password must be greater than 8 characters"

Otherwise one must manually run /usr/sbin/hostapd -B /etc/hostapd/hostapd.conf -P /var/run/ to see the error (and the AP will silently fail to start).

reinhart1010, I copied the dhcpcd.conf and interfaces files to dhcpcd.conf.station and interfaces.station before I ran the script
and then copied them to dhcpcd.conf.access_point and interfaces.access_point after I ran the script. Then I have two little scripts in the same directory I can run to switch back and forth:


cp dhcpcd.conf.station /etc/dhcpcd.conf
cp interfaces.station /etc/network/interfaces
systemctl disable hostapd
shutdown -r now


cp dhcpcd.conf.access_point /etc/dhcpcd.conf
cp interfaces.access_point /etc/network/interfaces
systemctl enable hostapd
shutdown -r now

kaz325 commented Mar 30, 2017

Did you succeed with this setup? if so, could you share what you did?

xsteacy commented May 5, 2017

when running:
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
I got:

Configuration file: /etc/hostapd/hostapd.conf
Failed to create interface mon.wlan0: -95 (Operation not supported)
wlan0: Could not connect to kernel driver
Using interface wlan0 with hwaddr b8:27:eb:69:ae:44 and ssid "testing"
wlan0: interface state UNINITIALIZED->ENABLED

The AP is working, I can connect to it but there is no internet access.

I use an RPI3 with the build-in wireless and the latest Raspbian with Pixel

Help will be very much appreciated, Thnk you

Thank you! This worked for me. Also, thank you for not adding the wireless->ethernet bridge. A lot of people want this but in my case, I'm only providing an interface to my device.

Setting up captive portal would be helpful, so that a browser automatically opens and loads the main page from the server running on the RPi.

For captive portal, try adding in dnsmasq.conf

address=/#/<address of your ap>

MrWeax1 commented Jun 17, 2017

Hi there! I was wondering how I could make the SSID hidden, as in it isn't broadcast to all computers and you have to manually enter the SSID on the connecting devices.

w7efs commented Jun 22, 2017 edited

ignore_broadcast_ssid=0 in /etc/hostapd/hostapd.conf should hide the SSID.

thank you very much for sharing this! :)
but i've and error : 6: [: Illegal number: 11: [[: not found 21: [[: not found Reading package lists... Done
how can I resolve this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment