Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
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
#!/bin/bash
#
# This version uses September 2017 august stretch image, please use this image
#
if [ "$EUID" -ne 0 ]
then echo "Must be root"
exit
fi
if [[ $# -lt 1 ]];
then echo "You need to pass a password!"
echo "Usage:"
echo "sudo $0 yourChosenPassword [apName]"
exit
fi
APPASS="$1"
APSSID="rPi3"
if [[ $# -eq 2 ]]; then
APSSID=$2
fi
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
interface=wlan0
dhcp-range=10.0.0.2,10.0.0.5,255.255.255.0,12h
EOF
cat > /etc/hostapd/hostapd.conf <<EOF
interface=wlan0
hw_mode=g
channel=10
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=$APPASS
ssid=$APSSID
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
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
address 10.0.0.1
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
EOF
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"
@jimbo1969
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 rPi3-ap-setup.sh 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 rPi3-ap-setup.sh. Policykit-1 allowed eth0 to come back online after the next reboot.
I ran rPi3-ap-setup.sh, 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) -- dhcpcd.sh, here.
I copied dhcpcd.sh to its intended home "sudo cp dhcpcd.sh /usr/lib/dhcpcd5/dhcpcd" and rebooted. All now works as intended. Yeah!

@jimbo1969
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"
returns

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

@GigiBoot
Copy link

GigiBoot commented Mar 22, 2018

I get this error:

pi@raspberrypi:~/Desktop $ sudo bash ./rPi3-ap-setup.sh 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?
./rPi3-ap-setup.sh: 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/rPi3-ap-setup.sh 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.

@Lewiscowles1986
Copy link
Author

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 https://gist.github.com/Lewiscowles1986/390d4d423a08c4663c0ada0adfe04cdb 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

@heartlandcoop-prichards
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/dhcpcd.sh into the /usr/lib/dhcpcd5 directory:

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

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

I then ran your script:
sudo ./rPi3-ap-setup.sh 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/dhcpcd.sh /usr/lib/dhcpcd5/dhcpcd

This will remove one more manual step.

Thanks

@swapab
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 ./rPi3-ap-setup.sh <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

@youwalther65
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.

@Lewiscowles1986
Copy link
Author

Thank you @youwalther65

@Maxl94
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

@RichardBronosky
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!

@moppymopperson
Copy link

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

@Lewiscowles1986
Copy link
Author

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

@ChrisRichards2
Copy link

Lewis
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?
Thanks
Chris

@mishra-ankit-dev
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

@Lewiscowles1986
Copy link
Author

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.

@emanresupdjs
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.

@Ridley-nelson17
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?
EDIT
Also, how. Would i implament this in something like pi-distro’s pi-gen?
https://github.com/RPi-Distro/pi-gen

@Lewiscowles1986
Copy link
Author

@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

@uttaravadina
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.

@Lewiscowles1986
Copy link
Author

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

@benjefferies
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 https://gist.github.com/Lewiscowles1986/fecd4de0b45b2029c390/raw/0c8b3af3530a35db9ab958defe9629cb5ea99972/rPi3-ap-setup.sh | sudo bash $0 password rPi3AP

sudo wget -q https://gist.githubusercontent.com/Lewiscowles1986/390d4d423a08c4663c0ada0adfe04cdb/raw/5b41bc95d1d483b48e119db64e0603eefaec57ff/dhcpcd.sh -O /usr/lib/dhcpcd5/dhcpcd
sudo chmod +x /usr/lib/dhcpcd5/dhcpcd

@Lewiscowles1986
Copy link
Author

@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

@RCCRAFT1
Copy link

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

@dixon1e
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.

@dixon1e
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.

@Lewiscowles1986
Copy link
Author

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

@Lewiscowles1986
Copy link
Author

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