This answer is based on the Foundation tutorial Setting up a Raspberry Pi as an access point in a standalone network (NAT)
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
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
# fallback to static profile on 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
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:
dhcp-range should match the static IP set in
Configuring the Access Point host software (hostapd)
Save the following in
/etc/default/hostapd and replace the line with #DAEMON_CONF with this:
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.