如何使用“netplan”为 Ubuntu 服务器设置 Raspberry Pi 3 板载 WiFi?

l -*_*c l 15 server wireless raspberrypi netplan 18.04

如何为 Ubuntu Server 18.04 设置 Raspberry Pi 3 B+ 板载 WiFi?特别是,与netplan?

现有答案,例如“如何在带有 Ubuntu Server 16.04 的 Raspberry Pi 3 上使用板载 wifi?” ,似乎不再因为申请/etc/network/interfaces,国家netplan已经取代ifupdown

# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
Run Code Online (Sandbox Code Playgroud)

这是Raspberry Pi 3Ubuntu Server 映像的全新安装。

##### release ###########################
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:    18.04
Codename:   bionic

##### kernel ############################
Linux 4.15.0-1034-raspi2 #36-Ubuntu SMP PREEMPT Fri Apr 5 06:21:41 UTC 2019 aarch64 aarch64 aarch64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

根据Ubuntu Wiki RaspberryPI,所需的包应该已经到位。

自 18.04.2 起,linux-firmware 和 linux-firmware-raspi2 包现在包含 Pi 3B 和 3B+ 上的内置 WiFi 所需的文件。

sudo lshw -C network

*-network:0 DISABLED      
   description: Wireless interface
   physical id: 2
   logical name: wlan0
   serial: b8:27:eb:69:f2:3b
   capabilities: ethernet physical wireless
   configuration: broadcast=yes driver=brcmfmac driverversion=7.45.18 firmware=01-6a2c8ad4 multicast=yes wireless=IEEE 802.11
*-network:1
   description: Ethernet interface
   physical id: 3
   logical name: eth0
   serial: b8:27:eb:3c:a7:6e
   size: 1Gbit/s
   capacity: 1Gbit/s
   capabilities: ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
   configuration: autonegotiation=on broadcast=yes driver=lan78xx driverversion=1.0.6 duplex=full ip=172.16.76.7 link=yes multicast=yes port=MII speed=1Gbit/s
Run Code Online (Sandbox Code Playgroud)

Netplan.io 提供了一些通用的Netplan 配置示例

要配置 netplan,请/etc/netplan/使用.yaml扩展名(例如/etc/netplan/config.yaml)保存配置文件,然后运行sudo netplan apply.

...然而,没有特定于 RaspberryPi 的指南。...特别是关于RaspberryPi Ubuntu Server 安装上现有的 /etc/netplan/50-cloud-init.yaml文件。

##### Netplan config ####################

[/etc/netplan/50-cloud-init.yaml]

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            match:
                macaddress: <MAC 'eth0' [IF1]>
            set-name: eth0

Run Code Online (Sandbox Code Playgroud)

因此,鉴于使用netplan和默认生成的.yaml文件。应该如何添加WiFi网络SSID和密码?并保留现有的有线以太网?

l -*_*c l 25

发现以下步骤可在Raspberry Pi 3 B+ 上使用netplanUbuntu Server 18.04ubuntu-18.04.2-preinstalled-server-arm64+raspi3.img.xz映像提供持久的 WiFi 设置。

更新系统:

sudo apt update
sudo apt full-upgrade
sudo reboot
Run Code Online (Sandbox Code Playgroud)

确定接口名称:

ip link show

# 1: lo: <LOOPBACK,UP,LOWER_UP> …
# 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> … state UP …
# 3: wlan0: <BROADCAST,MULTICAST> … state DOWN 
Run Code Online (Sandbox Code Playgroud)

确定 your-cloud-init.yaml 并打开进行编辑。

cd /etc/netplan/
ls -l
# -rw-r--r-- 1 root root 666 May 15 22:00 50-cloud-init.yaml
### note your *cloud-init.yaml file name

### backup *cloud-init.yaml file
cp 50-cloud-init.yaml 50-cloud-init.yaml.bak
### restrict read access
sudo chmod 640 /etc/netplan/50-cloud-init.yaml
### edit *cloud-init.yaml
sudo nano 50-cloud-init.yaml
Run Code Online (Sandbox Code Playgroud)

将 WiFi 访问信息添加到 your-cloud-init.yaml 文件。

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    version: 2
    ethernets:
        eth0:
            optional: true
            dhcp4: true
    # add wifi setup information here ...
    wifis:
        wlan0:
            optional: true
            access-points:
                "YOUR-SSID-NAME":
                    password: "YOUR-NETWORK-PASSWORD"
            dhcp4: true
Run Code Online (Sandbox Code Playgroud)

测试、生成并应用更改后的 your-cloud-init.yaml 配置:

  • 测试:(sudo netplan --debug try即使成功也继续)
  • 生成:(sudo netplan --debug generate在上一个命令出现问题的情况下提供更多详细信息)
  • 应用:(sudo netplan --debug apply如果在之前的命令期间没有问题)

确认测试:

sudo reboot

### wait, then without the wired ethernet connected ... 
ssh ubuntu@wifi-ip-address
Run Code Online (Sandbox Code Playgroud)

上述序列是从Larnu提到的“使用 Ubuntu Server 18.04 ARM Image 和 Netplan 的 Raspberry Pi 3B/B+ 无线桥接器”要点链接中提炼出来的。要点不仅仅是启用 WiFi,因为它将 Pi 变成了桥。


一些额外的有用 WiFi 设置步骤。

设置主机名。

sudo hostnamectl set-hostname my-server-name
Run Code Online (Sandbox Code Playgroud)

sudo nano /etc/hosts

sudo hostnamectl set-hostname my-server-name
Run Code Online (Sandbox Code Playgroud)

sudo nano /etc/cloud/cloud.cfg

127.0.0.1 localhost
# add host name
127.0.0.1 my-server-name
Run Code Online (Sandbox Code Playgroud)

从本地 Raspberry Pi 命令行验证。

# Set preserve_hostname to true for persistance after reboot
preserve_hostname: true
Run Code Online (Sandbox Code Playgroud)

启用 mDNS。

如果需要,通过安装Avahi启用多播 DNS。Avahi 支持 mDNS/DNS-SD/RFC 3927/Zeroconf/Bonjour 规范。

hostnamectl
#   Static hostname: my-server-name
#         Icon name: computer
#        Machine ID: …
#           Boot ID: …
#  Operating System: Ubuntu 18.04.2 LTS
#            Kernel: Linux 4.15.0-1036-raspi2
#      Architecture: arm64
Run Code Online (Sandbox Code Playgroud)

从另一台计算机远程检查 mDNS 解析。

sudo apt install avahi-daemon 
Run Code Online (Sandbox Code Playgroud)