Mastodon

Tuesday 1 August 2023

Raspberry Pi Access Point

 So you go on holiday and you take your tablets, laptop, phones and other stuff that connects to wifi. You then have to go and enter the new network details into each device to the new network and you put the details away and then find there's something else you haven't connected.

Wouldn't it be nice to take your own wifi connection with you.

So, I had a spare Raspberry Pi zero W version 2, broken camera port, a nice little case, a micro USB to large USB adaptor and some hot glue. All I needed was a second wifi adaptor.

I got this one from our friends at Amazon.

You don't have to get this one, I bought it and it works but feel free to try out any other ones.

First thing of course is to install Raspbian onto the Pi. I've gone for the lite 32 bit version. No desktop but it really isn't a problem. I'm not going through the setup process as if you can't do that you shouldn't do this. And all the config is done through ssh to the onboard wifi. Anything below that's in bold and italics is ok to copy and paste. 

Once you have finished setting it up, plug the adaptor in and you'll find like I did that although it's detected it doesn't work, yet.

The documentation with the adaptor gives you a link to the manufacturers website.

On the webpage it tells you to run the following.

 sh -c 'wget linux.brostrend.com/install -O /tmp/install && sh /tmp/install'

It takes a while and then it just works, you'll have the onboard adaptor of wlan0 and this one wlan2. 

Now we start doing other things and getting some software installed.

sudo apt install hostapd dnsmasq mc

I like mc, it's like Xtree Gold from the old dos days and is a great way to navigate the file system.

Now we run this.

sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent

After that reboot with sudo reboot.

Once it's restarted more configuring files to get it all working.

sudo nano /etc/dhcpcd.conf

Paste all this in at the end.

interface wlan1

    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

Save the files each time you finish, then onto.

sudo nano /etc/sysctl.d/routed-ap.conf

Paste this in and then save.

# Enable IPv4 routing
net.ipv4.ip_forward=1


Add a firewall rule with 
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

Save the firewall rules with:

sudo netfilter-persistent save

And now setup DHCP and DNS stuff

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf

Add the following to the file and save it.

interface=wlan1
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
domain=wlan
address=/gw.wlan/192.168.4.1


Now we setup the access point software.

sudo nano /etc/hostapd/hostapd.conf

Paste this in, changing the country code to where you live, the ssid to the same as your home one if you want and the passphrase needs to be changed too.

country_code=UK

interface=wlan1

ssid=SSID

hw_mode=g

channel=7

macaddr_acl=0

auth_algs=1

ignore_broadcast_ssid=0

wpa=2

wpa_passphrase=PWD

wpa_key_mgmt=WPA-PSK

wpa_pairwise=TKIP

rsn_pairwise=CCMP

Now we run this to get it starting up when the pi boots.


sudo systemctl unmask hostapd

sudo systemctl enable hostapd

sudo systemctl start hostapd


Then give it one final reboot and you should see a new wifi connection pop up.

That's about it really.

Thanks to hackster.io for the initial guidance and Github for a little help in making it work.

You can use just about any Pi for this, seems to work quite well and I may even add  a connection to my vpn from it.

If you want to connect the Pi to a different wifi connection when you're away and travelling, boot it up, ssh into it and run 

sudo raspi-config



Select option 1

Then select S1 and enter the wifi connection details there. There may be a more elegant way like setting up a graphical desktop but this is nice and minimal and it works.

Here's some pictures of the complete thing with a few magnets attached to the back to make it easy to stick to things. And yes, I went overboard with the hot glue gun as I was going to make something that was waterproof first.

Hope this all makes sense to you and have fun.