在 Arch Linux Arm 上使用 systemd-networkd 配置树莓派无线网路

近期有使用树莓派无线网路的需要,但是之前操作树莓派的无线网路基本上都是在 raspbian 上操作的,以前没有在 Arch 上操作过(或者说都是在台式上面操作的,因为**小螃蟹)

Arch linux arm 是默认安装的,我好像没有安装什么附加的套件

先在 Arch linux wiki 上找到一篇文章,但是它没有讲说怎么管理WPA的网络

首先在网路上找到一篇文章

大意是 首先将网络的名称和密码放到文件中

# wpa_passphrase MyNetwork SuperSecretPassphrase > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

这里 MyNetwork 是网络名称,而 SuperSecretPassphrase 是网络的密码

然后让这个profile启用

# systemctl enable [email protected]

接着把网络的配置塞到 /etc/systemd/network 下面

比方说就叫它 00-wireless-dhcp.network 好了 (实际上你想叫它什么都可以)

[Match]
Name=wlan0

[Network]
DHCP=yes

后面还有一些步骤是开 systemd-networkd 之类的,但是我本身已经用这玩意管理网络了,就没有继续参考了

侦错

重启之后当然是发现没有连上

ip a 输出如下

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether *MASKED* brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 *MASKED* scope global dynamic noprefixroute
       valid_lft 6990sec preferred_lft 6990sec
    inet6 *MASKED* scope global dynamic noprefixroute
       valid_lft 6990sec preferred_lft 6990sec
    inet6 *MASKED* scope global mngtmpaddr noprefixroute
       valid_lft forever preferred_lft forever
    inet6 *MASKED* scope global dynamic mngtmpaddr noprefixroute
       valid_lft 185858sec preferred_lft 99458sec
    inet6 fe80::dea6:32ff:fe0f:c62c/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether *MASKED* brd ff:ff:ff:ff:ff:ff

嗯,这当然是没有连上啦

错误讯息显示如下

# journalctl -u [email protected]
-- Journal begins at Thu 2021-02-04 06:22:10 CST, ends at Sat 2021-03-27 00:53:55 CST. --
Mar 27 00:51:46 raspi4b systemd[1]: Dependency failed for WPA supplicant daemon (interface-specific version).
Mar 27 00:51:46 raspi4b systemd[1]: [email protected]: Job [email protected]/start failed with result 'dependency'

Mar 27 00:51:46 raspi4b systemd[1]: sys-subsystem-net-devices-wlan0.conf.device: Job sys-subsystem-net-devices-wlan0.conf.device/start timed out.
Mar 27 00:51:46 raspi4b systemd[1]: Timed out waiting for device /sys/subsystem/net/devices/wlan0.conf.
Mar 27 00:51:46 raspi4b systemd[1]: Dependency failed for WPA supplicant daemon (interface-specific version).
Mar 27 00:51:46 raspi4b systemd[1]: [email protected]: Job [email protected]/start failed with result 'dependency'.
Mar 27 00:51:46 raspi4b systemd[1]: sys-subsystem-net-devices-wlan0.conf.device: Job sys-subsystem-net-devices-wlan0.conf.device/start failed with result 'timeout'.

我找了半天,都没有发现有什么相关的报错,但是在我的不懈努力下,找到了这篇文章

虽然这也不是什么解决的方法,但是我从中得到了启发,于是我

# rm /etc/systemd/system/multi-user.target.wants/[email protected]

接着重新建立启用就行

#systemctl enable [email protected]

重启系统后,就会发现已经连上无线网路了

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether *MASKED* brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 *MASKED* scope global dynamic noprefixroute
       valid_lft 6866sec preferred_lft 6866sec
    inet6 *MASKED* scope global dynamic noprefixroute
       valid_lft 6866sec preferred_lft 6866sec
    inet6 *MASKED* scope global mngtmpaddr noprefixroute
       valid_lft forever preferred_lft forever
    inet6 *MASKED* scope global dynamic mngtmpaddr noprefixroute
       valid_lft 184874sec preferred_lft 98474sec
    inet6 fe80::dea6:32ff:fe0f:c62c/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether *MASKED* brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.2/24 brd 192.168.0.255 scope global dynamic wlan0
       valid_lft 7200sec preferred_lft 7200sec
    inet6 *MASKED* scope global dynamic noprefixroute
       valid_lft 7197sec preferred_lft 7197sec
    inet6 *MASKED* scope global dynamic noprefixroute
       valid_lft 7197sec preferred_lft 7197sec
    inet6 *MASKED* scope global mngtmpaddr noprefixroute
       valid_lft forever preferred_lft forever
    inet6 *MASKED* scope global dynamic mngtmpaddr noprefixroute
       valid_lft 184874sec preferred_lft 98474sec
    inet6 fe80::dea6:32ff:fe0f:c62d/64 scope link
       valid_lft forever preferred_lft forever

离线模式配置网络

今天遇到一个情况,我本地没有有线网,但是无线网可用,那我肯定是需要配置这个无线网让它能连上

那照葫芦画瓢,我们把 wlan 的配置文件扔上去,配置一下服务就好了

首先 fdisk -l 确定一下树莓派是哪个分区

# fdisk -l
Disk /dev/sdb: 29.72 GiB, 31914983424 bytes, 62333952 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xfcb88066

Device         Boot   Start      End  Sectors  Size Id Type
/dev/sdb1         2048  1050623  1048576  512M  c W95 FAT32 (LBA)
/dev/sdb2      1050624 62333951 61283328 29.2G 83 Linux

然后把树莓派的根目录挂载上来,这里是 /dev/sdb2

# mount /dev/sdb2 /mnt

然后我们把 wlan 配置写到 /mnt/etc/wpa_supplicant/wpa_supplicant-wlan0.conf 里面去,这里还是用之前的那个例子

network={
        ssid="MyNetwork"
        #psk="SuperSecretPassphrase"
        psk=c370fef3b644bcf284350cdd6b74fae476847621731b64dd6829f4a54f33bd15
}

而后照葫芦画瓢,搞一个 service 出来

# /mnt/etc/systemd/system/[email protected]
[Unit]
Description=WPA supplicant daemon (interface-specific version)
Requires=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device
Before=network.target
Wants=network.target

# NetworkManager users will probably want the dbus version instead.

[Service]
Type=simple
ExecStart=/usr/bin/wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant-%I.conf -i%I

[Install]
Alias=multi-user.target.wants/[email protected]%i.service

然后把这个 service 扔到开机启动项里面

# ln -s /mnt/etc/systemd/system/[email protected] /mnt/etc/systemd/system/multi-user.target.wants/

记得把 wlan0 的配置文件放到 /mnt/etc/systemd/network 中,具体可以参见上面的那节

这样就基本完成了,插上卡通电开机默念 Arch linux 大法好,就会看到它连上 wlan 了。