-
11... and they lived happily ever after.
You now have Raspberry Pi networking set up the systemd way. Share and enjoy.
- THE END -
PS: If you also want your Pi to act as an access point (AP mode), continue with the next step.
-
12Configure virtual network interface.
a) Add udev rule to create virtual ap interface for each wlan interface, e.g.:
sudo nano /etc/udev/rules.d/70-ap-interface.rules
and enter the following in it:
SUBSYSTEM=="net", KERNEL=="wlan*", ACTION=="add", RUN+="/sbin/iw dev %k interface add ap%n type __ap"
b) Create config file for the ap0 interface:
sudo nano /etc/systemd/network/20-ap0.network
with the following contents:
[Match] Name=ap0 [Network] Address=192.168.11.254/28 DHCPServer=yes
c) Trigger manual ap0 creation and configuration:
sudo udevadm trigger --action=add /sys/class/net/wlan0 sudo systemctl restart systemd-networkd
d) Check the status:
networkctl status ap0
The ap0 interface should be in no-carrier state (for now) and have an IP address.
-
13Install hostapd package.
a) Install the package:
sudo apt install hostapd
b) Since hostapd is not "systemd-ready" yet (doesn't come with a .service file), we will create our own .service file for it:
sudo nano /etc/systemd/system/hostapd@.service
with the following contents:
[Unit] Description=Advanced IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP Authenticator Requires=sys-subsystem-net-devices-%i.device After=sys-subsystem-net-devices-%i.device Before=network.target Wants=network.target [Service] Type=simple ExecStart=/usr/sbin/hostapd /etc/hostapd/hostapd-%I.conf [Install] Alias=multi-user.target.wants/hostapd@%i.service
-
14Set up wireless network.
a) Create config file for the wireless network:
sudo nano /etc/hostapd/hostapd-ap0.conf
with the following contents:
interface=ap0 country_code=US hw_mode=g ssid=raspberrypi channel=6 ignore_broadcast_ssid=0 auth_algs=1 wpa=2 wpa_passphrase=password wpa_key_mgmt=WPA-PSK wmm_enabled=0
Replace country_code with the same one you've chosen in step 4.
Replace ssid and wpa_passphrase with the name and password of your wireless network.
b) Enable and start hostapd on ap0:
sudo systemctl enable hostapd@ap0 sudo systemctl start hostapd@ap0
c) Check the status:
networkctl status ap0
The ap0 interface should now be in configured state.
d) At this point you should be able to see your wireless network, be able to connect to it and log into your Raspberry Pi through it:
ssh pi@192.168.11.254
-
15Fix service start-up sequence.
As a final step, we need to make sure wpa_supplicant on wlan0 starts after hostapd on ap0.
To do this, we need to add an override file:
sudo mkdir /etc/systemd/system/wpa_supplicant@wlan0.service.d sudo nano /etc/systemd/system/wpa_supplicant@wlan0.service.d/override.conf
and enter the following in it:
[Unit] Wants=hostapd@ap0.service After=hostapd@ap0.service [Service] ExecStartPre=/bin/sleep 3
Now, reboot your Pi and cross your fingers... and legs... and ears.
If everything went right, you should see the wireless network you set up and be able to connect to it and log into your Raspberry Pi.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
why using hostapd when networkd can host an ap by itself ?
Are you sure? yes | no
In buster, hostapd does support systemctl natively... just name the config file `/etc/hostapd/ap0.conf`
Also make sure to set the locale in iw reg set and crda.
I got everything working... except the default route seems to be a dead end, maybe I need masquerade.
Got it fixed... ip forwarding was not enabled. Also used netfilter-persistent to make a masquerade rule `sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE`. For some reason the DNS server is not exposed or being given in DHCP. The routing is fine now... will bork this and start over.
Are you sure? yes | no
@simonlock sorry for the delay (I didn't get notified). Make sure you've created udev rule in step 12.a. I've just tried it on a fresh RPi4 and it works fine.
Are you sure? yes | no
A nice concise write up. Thanks. I've recently taken up systemd-networkd on my pi4 and your instructions nicely summarised how to set this up. However, regarding your setup of a virtual ap (step d) for hostapd failed for me.
networkctl status ap0
returns:
Failed to request link: No such device
Perhaps you possibly suggest what might have since changed or what I might have done wrong? Thanks in advance.
Are you sure? yes | no