Skip to content

Instantly share code, notes, and snippets.

@zanppa
Forked from tcg/README.md
Last active March 11, 2020 21:28
Show Gist options
  • Select an option

  • Save zanppa/19ed0d5a39bb3e4625ca54afa8a5b1b7 to your computer and use it in GitHub Desktop.

Select an option

Save zanppa/19ed0d5a39bb3e4625ca54afa8a5b1b7 to your computer and use it in GitHub Desktop.
Raspberry Pi Zero W as Internet connected Access Point *and* Wireless Client

I wanted this capability for various reasons. It's not necessarily stable, because uap0 has to use the same channel as wlan0, and if you're traveling/mobile, wifi channels aren't necessarily predictable.

This is not a guide. This is just so I remember how to get back where I started, in case I wipe this thing. =)

Extremely helpful guides that got me here:

In my particular configuration, the Pi was set up with a hostname of altoid. With the avahi tweak, I am now consistently able to access the Pi via altoid.local from machines on the same network (or connected directly, via the AP).

Out of the box, I used the "NOOBS" that comes preloaded, got internet via wifi, and selected to install Raspbian Lite. After that, I installed dnsmasq, and hostapd. Not sure if anything else was required.

/etc/network/interfaces

source-directory /etc/network/interfaces.d

auto lo
auto eth0
auto wlan0
auto uap0

iface lo inet loopback

iface eth0 inet dhcp

allow-hotplug wlan0

iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

iface uap0 inet static
  address 192.168.50.1
  netmask 255.255.255.0
  network 192.168.50.0
  broadcast 192.168.50.255
  gateway 192.168.50.1

/etc/dnsmasq.conf

# Lots of default stuff commented out. 
# Then this... 
interface=lo,uap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.50.50,192.168.50.150,12h

/etc/rc.local

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# I don't think this part even works...?
printf "Sleeping for 5 seconds befor hostapdstart"
sleep 5
hostapdstart >1&

exit 0

/usr/local/bin/hostapdstart

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
iw dev wlan0 interface add uap0 type __ap
service dnsmasq restart
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 ! -d 192.168.50.0/24 -j MASQUERADE
ifup uap0
hostapd /etc/hostapd/hostapd.conf

/etc/hostapd/hostapd.conf

interface=uap0
#driver=
# The name for the AP. Can include spaces, like "Inconspicuous Rock".
ssid=your_ap_wifi_network_name
#country_code=US
hw_mode=g
channel=11
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
# Comment out the wpa* group below if you 
# want to run an open network. 
wpa=2
wpa_passphrase=the_password_for_this_network
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_group_rekey=86400
ieee80211n=1
wme_enabled=1

/etc/default/avahi-daemon

# I had to change this from 1 to 0, because avahi 
# wasn't running on start (probably failing early). 
# This was key to be able to access the Pi via 
# its "{hostname}.local" address on the network.
AVAHI_DAEMON_DETECT_LOCAL=0

/etc/default/hostapd

 Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
#DAEMON_CONF=""

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""
DAEMON_CONF="/etc/hostapd/hostapd.conf"

sudo chmod 775 /usr/local/bin/hostapdstart

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