Skip to content

Instantly share code, notes, and snippets.

Last active July 16, 2023 15:33
Show Gist options
  • Save Lewiscowles1986/fecd4de0b45b2029c390 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/fecd4de0b45b2029c390 to your computer and use it in GitHub Desktop.
Raspberry Pi 3 access-point-setup
# This version uses September 2017 august stretch 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 -yqq
apt-get update -yqq
apt-get upgrade -yqq
apt-get install hostapd dnsmasq -yqq
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
sed -i -- 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd
cat >> /etc/network/interfaces <<EOF
# Added by rPi Access Point Setup
allow-hotplug wlan0
iface wlan0 inet static
echo "denyinterfaces wlan0" >> /etc/dhcpcd.conf
systemctl enable hostapd
systemctl enable dnsmasq
sudo service hostapd start
sudo service dnsmasq start
echo "All done! Please reboot"
Copy link

Aimma commented Feb 23, 2018

This file has resulted in no internet connectivity to my raspberry pi. It says Connection to dhcpcd lost. I'm totally clueless what to do now. Pleaseeeee helllpppp!!!! Plus the raspberry pi didn't become the access point

Copy link

lucascrandle commented Feb 25, 2018

Awww. spam :removed: 😉

Copy link

Lewiscowles1986 commented Mar 3, 2018

@Aimma did you follow the guide like 2 comments above your question?

@lucascrandle removed for spam, please don't do that again. I've just re-tested (again), the script works on raspberry pi 3's (it's designed platform) using raspbian (as designed). Start a blog elsewhere if you want to post links. I don't want people to be confused further.

@dereksnyder42v2 cheers 👍

Copy link

Thanks Lewis!
13-Mar-2018 -- Pi-day-eve.
I thought I'd contribute a tiny bit back by sharing what I had to do to employ this on the 2017-11-09 version of Raspian Lite (Stretch) on my first generation Model B Raspberry Pi.
I added an empty file named "ssh" to the root folder of my SD card in order to enable SSH to my headless Pi.
I prepended "net.ifnames=0 " to the contents of /boot/cmdline.txt in order to get old-school interface names back.
I installed the policykit-1 package after running your script (above) and having it break my eth0 interface, thus preventing me from SSH-ing back into the Pi after reboot. So I suggest doing a "sudo apt-get install policykit-1" on this version of Raspian BEFORE running Policykit-1 allowed eth0 to come back online after the next reboot.
I ran, of course.
I received the cryptic error about dhcpcd failing due to /etc/network/interfaces defining some interfaces that will use a DHCP client or static address. I spent hours trying to understand all the parts of that, and stumbled across another of your scripts (thank you again) --, here.
I copied to its intended home "sudo cp /usr/lib/dhcpcd5/dhcpcd" and rebooted. All now works as intended. Yeah!

Copy link

Question: After employing this solution on the 2017-11-09 version of Raspian Lite (Stretch), it seems like I may have two competing DHCP clients running.
"pi@raspberrypi:~ $ dpkg -l | grep dhcp"

  • ii dhcpcd5 1:6.11.5-1+rpt4 armhf DHCPv4, IPv6RA and DHCPv6 client with IPv4LL support
  • ii isc-dhcp-client 4.3.5-3 armhf DHCP client for automatically obtaining an IP address
  • ii isc-dhcp-common 4.3.5-3 armhf common manpages relevant to all of the isc-dhcp packages

Am I interpreting that correctly? And if so, is the answer to simply uninstall ics-dhcp-client? Aside from this question, I'm convinced it is all working as intended on this platform.

Copy link

GigiBoot commented Mar 22, 2018

I get this error:

pi@raspberrypi:~/Desktop $ sudo bash ./ password apname
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
./ line 35: /etc/hostapd/hostapd.conf: No such file or directory
sed: can't read /etc/default/hostapd: No such file or directory
Failed to enable unit: File hostapd.service: No such file or directory
Failed to enable unit: File dnsmasq.service: No such file or directory
Failed to start hostapd.service: Unit hostapd.service not found.
Failed to start dnsmasq.service: Unit dnsmasq.service not found.
All done! Please reboot

If I type : sudo bash Desktop/ YourWifiApName YourWifiApPassword the terminal gets frozen.
I have Pi zero W.How can I make this work?
I am a novice in this Pi programming.

Copy link

Lewiscowles1986 commented Apr 10, 2018

@jimbo1969 sorry, as it states all over the place. This is intended to work with a Raspberry Pi 3. I Cannot comment on your needed changes for an earlier raspberry Pi or packages as I don't make Raspberry Pi distro. There is a networking fix for dhcpcd5 here same deal. It should be installed before this script.

@GigiBoot, Looks like some of the stages fail, likely due to you not having networking setup. I've not run this on a PiZero-W. This script is only intended for a Raspberry Pi 3 and it's onboard network wlan

Copy link

heartlandcoop-prichards commented Apr 12, 2018

This worked with Raspian Stretch March 2018

Unsolicited advice

First I needed to mv your .sh:
Lewiscowles1986/ into the /usr/lib/dhcpcd5 directory:

sudo mv /usr/lib/dhcpcd5/dhcpcd /usr/lib/dhcpcd5/dhcpcd.orig

sudo mv /boot/ /usr/lib/dhcpcd5/dhcpcd

I then ran your script:
sudo ./ hello123 ApYo

I did need to make one modification, I added:
apt-get update
after apt-get upgrade.
I'm not sure why the upgrade was messing with the ability to install hostapd.

I also recommend placing both .sh files in the /boot directory and adding the command:
cp /boot/ /usr/lib/dhcpcd5/dhcpcd

This will remove one more manual step.


Copy link

swapab commented Apr 14, 2018

***EDIT: *** @Lewiscowles1986 the following scripts might have saved you time Adapter passthrough & Stretch dhcpcd fix. the below linked a blog post I've removed.

The script executed without any issues, but I was not able to get internet for my mobile devies(Connect, no internet)

Unsolicited Advice

I followed a combination of this gist and this blog: removed

I believe SET UP IPV4 FORWARDING section from removed did the trick for me.

  1. Execute ./ <password> <ssid>

  2. Explicitly set ipv4 forwarding

sudo vim /etc/sysctl.conf

remove the # from the beginning of the line containing net.ipv4.ip_forward=1
  1. share our Pi's internet connection to our devices connected over WiFi by the configuring a NAT between our wlan0 interface and our eth0 interface. We can do this using the following commands:
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  
  1. However, we need these rules to be applied every time we reboot the Pi, so run sudo sh -c "iptables-save > /etc/iptables.ipv4.nat to save the rules to the file /etc/iptables.ipv4.nat. Now we need to run this after each reboot, so open the rc.local file with sudo nano /etc/rc.local and just above the line exit 0, add the following line
iptables-restore < /etc/iptables.ipv4.nat  
  1. It's working!!!

Thanks! @Lewiscowles1986 @frillip

Copy link

After struggling with several instructions on the Internet this script works perfect for my Raspberry Pi Zero W with latest Raspian Stretch Lite and dslrdashboard server to Wifi control my Nikon DSLR over a standalone SSID. Thank you very much for this.

Copy link

Thank you @youwalther65

Copy link

Maxl94 commented May 14, 2018

Thank's 👍 I was struggling the complete day setting up the AP didn't get it working. Your script does it :-)

Also had the problem that eth0 disappear: but @JddAndrewLauren commet fixed that

Copy link

This is so awesome! I've made an RPi Zero W based gadget that I want to control via mobile phone. This was a VERY easy way to set up hostapd to allow me to connect device to device and not need a local network. I'll credit you when I publish my build notes. Thank you!

Copy link

Worked on first try for me! Thanks to @JddAndrewLauren and @maxi94 for comments about getting eth0 back!

Copy link

Just a note, if you search the comments there are other ways to get eth0 back without adding an /etc/networking/interfaces entry for it 😉

Thanks @RichardBronosky & @moppymopperson

Copy link

Thanks for your excellent script.
I have connected my rPi3 running Stretch Lite with your AP script to a USB tethered Samsung phone & forwarded AP traffic to a USB port.

Everything works fine & is stable. However if the throughput to the Internet is say 30Mbps when I connect the phone to a windows PC via a USB port, I am getting about 8Mbps when connecting the PC via the rPi3 AP. The cpu load on th rPi3 is minimal.

Have you any idea why this is so? Suggestions to improve?

Copy link

mishra-ankit-dev commented Aug 12, 2018

I have everything working i can connect to the access point and all the traffic is also routed from ethernet to access point but my ethernet icon says "connection to dhcpcd lost" though when i ifconfig it shows eth0 and also ip address. Plzz help me out about ethernet icon plzz.. Thnx in advance

Copy link

Hi @ChrisRichards2 I have no idea matey.

Perhaps the wifi on the Pi3 is just not up to it. I've never really tried to measure WiFi speed.

@amishm766 I don't know anything about that problem, it sounds like the GUI is misreporting or maybe something is messed up.

Copy link

@adiroman, @doktor500, @botzen67 ,I have face the same error, However the AP works fine for me even with this error. For wpa authentication, just try kill wpa_supplicant process before running hostapd.

Copy link

Ridley-nelson17 commented Mar 9, 2019

Is this possible on an rpi 3b? If so, how? Im new to all this, i dont know shell, so is there like an image i can just flash onto my raspi?
Also, how. Would i implament this in something like pi-distro’s pi-gen?

Copy link

@Ridley-nelson this works on a 3b & 3b+

How? Follow the guides, learn shell first. I've tried to document, but I'm afraid it might be too much of a stretch for me to advise the whole internet on how to use. If you scroll up you'll see some notes to others.

  • use raspbian
  • follow those instructions above
  • have fun

pi-gen wouldn't run these exact commands as they are on a Pi. In order to run these in an environment like Pi-gen, you'd need to be in a chroot so that the paths worked and the commands act in the expected way for the built image. This is not something I can help with, but hopefully that helps to signpost

Copy link

uttaravadina commented Jul 4, 2019

Hey @Lewiscowles1986, awesome job on the script. I am just stuck at one part and would like your quick scrutiny on that. I am using raspberry pi 3 b+ as the device and ran the script on it without any errors. Rebooted it. Access point shows up. But, it is unable to provide an IP Address to whichever end device I try to connect it with. What could be the issue here?

EDIT: I am not using any bridging, just the Access point which your script generates.

Copy link

It could be that the DHCP server has not started so cannot assign address, or that you're not entering the correct password

Copy link

This works for me on my raspberry pi zero w. hostapd failed to start up initially but a sudo service hostapd stop && sudo service hostapd start fixed it.

curl -sSL | sudo bash $0 password rPi3AP

sudo wget -q -O /usr/lib/dhcpcd5/dhcpcd
sudo chmod +x /usr/lib/dhcpcd5/dhcpcd

Copy link

@bengefferies, the dhcpcd5 should come first as you may lose internet. Same for the adapter pass-through script. They should at least be downloaded before running any script

Copy link

Thank you for this! I couldn't find anything else on what to do.

Copy link

dixon1e commented May 28, 2020

@bkis your script calls out correctly "systemctl unmask hostapd" and for dnsmasq also are now required to start the services.

Running this script as is fails because services are now masked by default.

Thank you and thank you to the author @Lewiscowles1986.

Copy link

dixon1e commented May 28, 2020

For those interested, my configuration uses a USB Wireless Adapter (Edimax which is Realtek RTL8188CUS) in addition to the built in WiFi for Raspberry Pi 3.

When executing this script, the internal WiFi is defeated and does not start. I will attempt debug and report here for those searching for such a configuration and why it stops the internal WiFi. All help is welcome if someone knows why this is happening.

Copy link

Lewiscowles1986 commented May 30, 2020

@bkis please link to your own blog in future.

I've edited the comment to be in a <details><summary> block. Please don't respond with long-form content, this really isn't the place for it.

It's quite enough to maintain something, without many people offering additional changes, or polluting scope with things that are separate.

There is other advice in this thread to backup if you wish to switch between AP and Original interfaces from 2016, as well as a script to keep newer raspbians connecting and a separate bridge script. They are all separate parts and not meant to be combined within the context of this Gist.

I've deliberately stayed away from dual purpose AP/NON-AP mode. It's a complication

Copy link

This script is actually a little old now. I will try to find time to work on it, but the debian network stack has changed.

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