wpa_supplicant doesn't work from systemd

6,018

Solution 1

Changing the Type to forking instead of dbus worked for me:

[Service]
Type=forking

Solution 2

The question is "What might prevent the correct behavior when launching the same command from systemd?

wpa_supplicant runs as a daemon and in order to make a connection without the use of a configuration file containing the hard coded connection details, a client must be used to supply the connections details to wpa_supplicant.

There are two methods by which a client can connect to a running wpa_supplicant: the control file interface and a DBUS method.

The systemd unit file supplied by Debian (and Ubuntu) for wpa_supplicant starts the daemon with the "-u" parameter which tells it to use the DBUS interface. I do not understand why the -O /run/wpa_supplicant parameter is provided since this interface does not get created and so clients cannot connect via this non-existent control file socket. The clients supplied as part of the wpa_supplicant source, namely wpa_cli and wpa_gui (Qt4) do not have DBUS capabilities and so can only use the socket file interface. The systemd unit file is intended for use on systems with network manager (which communicated via DBUS) to manage the network connections or possibly the alternative "wicd".

Your handcrafted command

  wpa_supplicant -u -Dwext,nl80211 -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B

tells wpa_supplicant to use the configuration file containing the SSID and authorization details, so when it starts, it tries to connect to the SSID and establish the connection.

By comparison, the systemd unit file just starts up the wpa_supplicant daemon forever waiting for some message via the DBUS interface to tell it what and how to try to establish a connection.

You could hardwire the systemd unit file to use your fixed configuration but you would also need to remove the references to DBUS and not use the -u flag. When using the DBUS interface you also need to ensure that the dbus-1 configuation files are present for wpa_supplicant and also that the policy allows non-root users to interact if requried (usually allowed by membership of the netdev group in /etc/dbus-1/system.d/wpa_supplicant.conf.

Manu explained above to change the "type" from "dbus" to forking, but I suggest it would be better to use "simple" and NOT to use the "-B" (fork the daemon into the background option) because systemd units are more easily managed when they do not fork (and no need to worry about creating and watching PID files of the forked process).

Share:
6,018

Related videos on Youtube

Mark
Author by

Mark

Updated on September 18, 2022

Comments

  • Mark
    Mark almost 2 years

    I have a strange behavior I cannot fix by myself. I'm running a Raspbian Stretch Lite on an RPi2. As WiFi dongle I use a Realtek RTL8188EU (drivers are from staging directory).

    By default it creates the wlan0 interface but it doesn't try to connect to the access point at all. The configuration is correct because a different WiFi dongle works fine.

    Here my /etc/wpa_supplicant/wpa_supplicant.conf file:

    country=IT
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    ap_scan=1
    update_config=1
    network={
        ssid="ssid"
        scan_ssid=1
        psk="password"
        key_mgmt=WPA-PSK
    }
    

    After a lot of trials I found a working command:

    wpa_supplicant -u -Dwext,nl80211 -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
    

    issuing this command from the console leads to the expected behavior (i.e. it connects to the AP). But if I place this line into the systemd service:

    $ cat /etc/systemd/system/multi-user.target.wants/wpa_supplicant.service 
    [Unit]
    Description=WPA supplicant
    Before=network.target
    After=dbus.service
    Wants=network.target
    
    [Service]
    Type=dbus
    BusName=fi.epitest.hostap.WPASupplicant
    #ExecStart=/sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
    ExecStart=/sbin/wpa_supplicant -u -Dwext,nl80211 -i wlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf -B
    
    [Install]
    WantedBy=multi-user.target
    Alias=dbus-fi.epitest.hostap.WPASupplicant.service
    

    it doesn't work, I mean it doesn't connect. Even if I manually (re)start the systemd service.

    What might prevent the correct behavior when launching the same command from systemd?

    By the way, if it could help, even when it works (after manually issuing the command from the console) the command iw list outputs nothing.

    • Ulrich Schwarz
      Ulrich Schwarz over 6 years
      Maybe try without the -B? systemd prefers things not to fork into the background, and the line you had before didn't, AFAICT. Maybe systemd thinks it immediately fails. (What does systemctl status wpa_supplicant say?)
    • Mark
      Mark over 6 years
      Well, when I tried before it hanged the whole system. Instead, now it seems to work. Please, let me try it for some time. Then you might convert this comment as an answer.
    • GAD3R
      GAD3R over 6 years
      What is the output of ps aux | grep wpa?
  • Nickolai
    Nickolai over 6 years
    Not sure why this got downvoted, it worked for me