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