I want my Raspberry Pi to be used as access point or as client connected to my internet router but not both together. So I want to switch between these two modes with one simple command without rebooting.
How can I do this?
Access point and client mode are both running as services so we should use
systemd-networkd to switch off one service and switch on the other. With its options we are able to do it without rebooting. For reference I use Raspbian Stretch Lite 2019-04-08 full upgraded with
sudo apt update && sudo apt full-upgrade && sudo reboot done at 2019-04-14.
For detailed information look at (1). Here only in short. Execute these commands:
# disable debian networking and dhcpcd
rpi ~$ sudo -Es
rpi ~# systemctl mask networking.service dhcpcd.service
rpi ~# sudo mv /etc/network/interfaces /etc/network/interfaces~
rpi ~# sed -i '1i resolvconf=NO' /etc/resolvconf.conf
# enable systemd-networkd
rpi ~# systemctl enable systemd-networkd.service systemd-resolved.service
rpi ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
Setup wpa_supplicant as wifi client with wlan0
Setup wpa_supplicant with this file and your settings and enable it.
rpi ~# cat >/etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
rpi ~# systemctl disable wpa_supplicant.service
rpi ~# systemctl enable [email protected]
Setup wpa_supplicant as access point with ap0
Create this configuration file:
rpi ~# cat > /etc/wpa_supplicant/wpa_supplicant-ap0.conf <<EOF
rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-ap0.conf
Create these two configuration files:
rpi ~# cat > /etc/systemd/network/08-wlan0.network <<EOF
rpi ~# cat > /etc/systemd/network/12-ap0.network <<EOF
Modify service for access point to use ap0
ap0 is a virtual interface and it must be created and deleted with start/stop of the service. It is also required to modify dependencies. This cannot be done with a drop in file, so we have to modify the full service. In addition this service conflicts with the client connection service with wlan0. We set a dependency
Conflicts= so we do not have to care about stopping the other service. Do it with:
rpi ~# systemctl disable [email protected]
rpi ~# systemctl edit --full [email protected]
Modify/insert only these lines:
ExecStopPost= as shown. Leave all other untouched, save it and quit the editor:
Description=WPA supplicant daemon (interface-specific version)
# NetworkManager users will probably want the dbus version instead.
ExecStartPre=/sbin/iw dev wlan0 interface add ap0 type __ap
ExecStart=/sbin/wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant-%I.conf -Dnl80211,wext -i%I
ExecStopPost=/sbin/iw dev ap0 del
Now you can determine in which mode the RasPi should start after bootup. Just enable that service and disable the other one. If I want to start with client connection:
rpi ~$ sudo systemctl enable [email protected]
rpi ~$ sudo systemctl disable [email protected]
You should then be able to switch the service with:
rpi ~$ sudo systemctl start [email protected]
rpi ~$ sudo systemctl start [email protected]
No need to stop a service.
If you want to make the pi change between access point and client mode automatically, so that the pi provides an access point only if there is no known SSID nearby, then it isn't possible with this setup. The wpa_supplicant client service must always run so it can detect an available hotspot nearby. You can only switch on/off the access point in addition to the client service. So you have first to setup a Access point as WiFi repeater, optional with bridge or Access point as WiFi repeater with additional WiFi-dongle. Then you could use wpa_cli running as daemon with option
-a to detect a connection to a hotspot and switch on/off the access point on the RasPi. A generic example of doing this you can find at Run a script when wifi is connected to a particular wifi network.
Based on the answer by Ingo, I went and made a script to do this job.
The gist is that there's one script to run that'll setup the Pi for reboot-less AP/STA switching and then there are helper scripts that'll actually do the switching for you.
I would have commented but don't have enough reputation.
Ingo's answer almost worked for me. However there was a weird bug where the virtual interface was not removed sometimes. Apparently (source: https://github.com/seemoo-lab/nexmon/issues/221) this can happen because the broadcom driver crashes when the pi is started up without wifi.
The solution is to, after switching from ap to wlan, run the following command twice: sudo modprobe -r brcmfmac && sudo modprobe brcmfmac
That restarts the driver, and the virtual interface is/can be removed.