SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-02-21 21:17:23

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Wireless connection using systemd

First create a wpa_supplicant configuration file for your wireless connection point(s):

sudo tee /etc/wpa_supplicant/wpa_supplicant.conf <<< "ctrl_interface=DIR=/run/wpa_supplicant"
sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf <<< $(wpa_passphrase SSID password)

Where "SSID" is the name of the connection point and "password" is the, er, password.

Repeat the second line for all the desired connection points (these can be added later if needed).

Then write the systemd unit file (as root):

[Unit]
Description=Wireless network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/sbin/ip l set %i up
ExecStart=/sbin/wpa_supplicant -B -i %i -c /etc/wpa_supplicant/wpa_supplicant.conf
ExecStart=/sbin/dhclient %i

ExecStop=/sbin/ip l set %i down

[Install]
WantedBy=multi-user.target

Save this file as /etc/systemd/system/network-wireless@.service

Now enable this .service to start at the next boot using:

sudo systemctl enable network-wireless@wlan0.service

If your wireless interface is not called "wlan0", change that to the actual interface name.

Now make sure all other wireless programs are disabled -- remove (or comment out) the wlan0 lines in /etc/network/interfaces if you're using ifupdown.

Reboot.

The .service can be stopped immediately using:

sudo systemctl stop network-wireless@wlan0.service

Or prevented from starting at the next (and subsequent) boot(s) using:

sudo systemctl disable network-wireless@wlan0.service

Quick comparison of `systemd-analyze critical-chain` output

Using /etc/network/interfaces (ifupdown):

graphical.target @23.545s
└─multi-user.target @23.544s
  └─exim4.service @21.133s +2.409s
    └─basic.target @19.268s
      └─paths.target @19.261s
        └─cups.path @19.261s
          └─sysinit.target @19.214s
            └─nfs-common.service @18.608s +605ms
              └─rpcbind.target @18.604s
                └─rpcbind.service @18.456s +146ms
                  └─network-online.target @18.455s
                    └─network.target @18.454s
                  *** └─networking.service @5.114s +13.338s ***
                        └─systemd-random-seed.service @4.850s +261ms
                          └─systemd-remount-fs.service @4.663s +181ms
                            └─keyboard-setup.service @3.113s +1.547s
                              └─systemd-udevd.service @2.966s +142ms
                                └─systemd-tmpfiles-setup-dev.service @2.335s +628ms
                                  └─kmod-static-nodes.service @2.231s +102ms
                                    └─system.slice @2.005s
                                      └─-.slice @1.999s

Using network-wireless".service:

graphical.target @14.822s
└─multi-user.target @14.821s
  └─exim4.service @13.807s +1.012s
    └─network-online.target @13.801s
      └─network.target @13.797s
    *** └─network-wireless@wlan0.service @9.199s +4.596s ***
          └─basic.target @9.066s
            └─paths.target @9.061s
              └─cups.path @9.061s
                └─sysinit.target @8.974s
                  └─rpcbind.service @13.805s +242ms
                    └─network-online.target @13.801s
                      └─...

As you can see, systemd > /etc/network/interfaces ]:D

[Crossposted from http://crunchbang.org/forums/viewtopic.php?id=39027]

Last edited by Head_on_a_Stick (2015-02-21 21:21:33)

Offline

Help fund CrunchBang, donate to the project!

#2 2015-02-21 22:02:18

Unia
#! Octo-portal-pussy
From: The Netherlands
Registered: 2010-07-17
Posts: 4,634
Website

Re: Wireless connection using systemd

Very nice, I like it smile Do you know how this works with roaming? E.g. will it automatically (re-)connect to a new network? You mentioned adding more lines for whatever network we need, but not if it (re)connects.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

I am a #! forum moderator. Feel free to send me a PM with any question you have!

Offline

#3 2015-02-21 22:14:54

Head_on_a_Stick
CatMod
From: A world of pure imagination
Registered: 2014-01-21
Posts: 4,797

Re: Wireless connection using systemd

Unia wrote:

Very nice, I like it smile Do you know how this works with roaming? E.g. will it automatically (re-)connect to a new network? You mentioned adding more lines for whatever network we need, but not if it (re)connects.

It will re-connect automatically as wpa_supplicant is run as a daemon.

This method is intended for use with access points at home, but it could be adapted for use with roaming networks by manually adding stanzas to the wpa_supplicant.conf file as needed using:

sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf <<< $(wpa_passphrase SSID password)

It would then connect to whichever networks were available (I think -- I don't roam with my laptop so I haven't tested this).

I think connections would be made in order of signal strength but I haven't tried it.

Offline

#4 2015-02-21 23:43:17

Unia
#! Octo-portal-pussy
From: The Netherlands
Registered: 2010-07-17
Posts: 4,634
Website

Re: Wireless connection using systemd

I usually only move between a fixed set of networks, so that would work for me. I might try it if the new NetworkManager doesn't impress me; I heard the 1.0 release brings a lot of goodies big_smile


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

I am a #! forum moderator. Feel free to send me a PM with any question you have!

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo