Update While the below did work with the onboard WiFi interfaces it is a kludge, so I can not recommend it as a reliable solution.
This answer is based on the Foundation tutorial Setting up a Routed Wireless Access Point
If no networks can be found (by dhcpcd
) the "Fallback Profile" is used to set a Static IP Address (and disable WiFi gateway).
This enables the Pi to be accessed headlessly by WiFi - which is handy when travelling without keyboard, monitor etc, but allows full network functionality when a known network is available.
The steps below are a modified (and simplified) version of the tutorial. You are urged to read the full tutorial before proceeding.
Install Software 1
sudo apt update
sudo apt install dnsmasq hostapd
sudo systemctl stop dnsmasq
sudo systemctl stop hostapd
sudo reboot
1. The hostapd .service file is now automatically masked every time the package is upgraded with no valid configuration. After configuring you should unmask with sudo systemctl unmask hostapd.service
Configuring a static IP Fallback Profile
Add the following to the end of /etc/dhcpcd.conf
(This step differs from the Foundation tutorial)
# It is possible to fall back to a static IP if DHCP fails:
# define static profile
profile static_wlan0
static ip_address=10.1.4.1/24
nogateway
# fallback to static profile on wlan0
interface wlan0
fallback static_wlan0
You can test this by restarting dhcpcd
- to see the static fallback you need to rename the network in /etc/wpa_supplicant/wpa_supplicant.conf
so it will not be found by dhcpcd
sudo systemctl restart dhcpcd
Configuring the DHCP server (dnsmasq)
Type or copy the following information into the /etc/dnsmasq.conf
configuration file and save it:
NOTE the dhcp-range
should match the static IP set in /etc/dhcpcd.conf
interface=wlan0
dhcp-range=10.1.4.2,10.1.4.20,255.255.255.0,24h
Configuring the Access Point host software (hostapd)
Save the following in /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=NameOfNetwork
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=AardvarkBadgerHedgehog
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Edit /etc/default/hostapd
and replace the line with #DAEMON_CONF with this:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Start it up
sudo systemctl start hostapd
sudo systemctl start dnsmasq
The latest update to Raspbian (2019-03-09) has changed hostapd "wpa (2:2.6-10)".
You should run sudo systemctl unmask hostapd
and sudo systemctl enable hostapd
to ensure it runs on boot.
#Add routing and masquerade
The Foundation tutorial includes instructions to add routing and masquerade.
This is not necessary for my use case (and if I wanted to do this would probably write a systemd
service, rather than the clumsy rc.local approach)
Strictly, when running as a WiFi client hostapd and dnsmasq are not needed, but seem to do no harm.
NOTE there are other tutorials which do a similar job. http://www.raspberryconnect.com/network has a number of different options and can automatically switch between Access Point and WiFi client, but is more complex and uses wpa_cli
to switch between networks. This contains lots of helpful information.