我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

在 Raspberry Pi OS 上使用 Cloud-init

正如部分用户可能已经注意到的,最新的 Raspberry Pi OS 版本基于 Debian Trixie,现已包含cloud-init。这标志着我们开始从基于firstrun.sh script.

的旧版首次启动自定义系统过渡。Cloud-init 是一款跨平台、独立于发行版的工具,用于在系统首次启动时进行自动配置。(定义改编自 cloud-init 官方文档。)借助它,您可以为 Raspberry Pi 镜像预配置用户、网络设置、SSH 密钥、存储配置等更多内容——所有这些操作都无需在刷写镜像后手动登录。

如何使用它?

如果您下载了最新镜像(2025年11月24日发布),您会发现在 boot 分区中出现了三个新文件。这是当您插入刚刷写好 Raspberry Pi OS 的 microSD 卡时,计算机会自动挂载的 FAT32 分区。它已经包含了您熟悉的文件,如config.txt, but now you’ll also see:

  • meta-data
  • network-config
  • user-data

对于大多数用户而言,meta-data can be left untouched — it simply enables cloud-init to process the other configuration files correctly. Advanced users may use it for more complex provisioning workflows, but it’s safe to ignore in typical setups.

Cloud-init 使用 YAML 作为其配置文件格式。如果您不熟悉 YAML,建议快速浏览一下官方文档,因为缩进和格式非常重要。目前,我们将重点关注两个最相关的文件:user-data and network-config.

常规配置(user-data)

该user-data file is the central place for your configuration. With the exception of networking, almost everything cloud-init sets up on first boot is controlled from here.

您可以使用它来创建默认用户、定义区域设置、安装附加软件包、配置 SSH 访问等——所有这些内容均涵盖在cloud-init 官方文档.

中。与许多其他发行版不同,Raspberry Pi OS 包含一些针对 cloud-init 配置的 Raspberry Pi 专属扩展。这些扩展允许您启用 I2C、SPI、串口和 1-Wire 等硬件接口,甚至可以激活 USB gadget 模式(rpi-usb-gadget) automatically.

以下是一个示例配置,它创建了一个用户并演示了当前支持的所有 Raspberry Pi 专属选项:

#cloud-config

# Set the hostname for this device. This will also update /etc/hosts if manage_etc_hosts is enabled.
hostname: mypi2025
manage_etc_hosts: true

# Set the system timezone
timezone: Europe/London

# Create a default user account and apply permissions
users:
  - name: pi
    groups: users,adm,dialout,audio,netdev,video,plugdev,cdrom,games,input,gpio,spi,i2c,render,sudo
    shell: /bin/bash
    lock_passwd: false  # Set to true to disable password login entirely
    plain_text_passwd: mysecretpassword123  # Alternatively, use 'passwd:' with a hashed password for better security
    ssh_authorized_keys:
      - ssh-ed25519 mykeystuff  # Replace with your actual SSH public key
    sudo: ALL=(ALL) NOPASSWD:ALL  # Allow passwordless sudo for this user

# Raspberry Pi–specific options (provided by the cc_raspberry_pi module)
rpi:
    spi: true               # Enable SPI interface
    i2c: true               # Enable I2C interface
    serial: true            # Enable serial console and UART interface
    onewire: true           # Enable 1-Wire interface
    enable_usb_gadget: true # Enable USB gadget mode

# Additional Raspberry Pi OS option (not available on generic cloud-init images)
enable_ssh: true  # Enables the SSH server on first boot

# Optional: Disable SSH password authentication if using SSH keys only (recommended for security)
# ssh_pwauth: false

有关更多详细信息,请参阅cc_raspberry_pi module in the cloud-init 官方文档.

注意:
该#cloud-config header at the top of the file is 必填— 没有它,cloud-init 将无法正确处理该文件。

网络配置(network-config)

该network-config file defines how your Raspberry Pi should set up its network interfaces on first boot. As the name suggests, this is where you configure Wi-Fi or Ethernet settings before ever powering on the device.

下面是一个将树莓派连接到 Wi-Fi 网络的简单示例:

network:
  version: 2
  wifis:
    # Make sure the target is NetworkManager which is the default on Raspberry Pi OS
    renderer: NetworkManager
    # The connection name
    wlan0:
      dhcp4: true
      # !VERY IMPORTANT! Change this to the ISO/IEC 3166 country code for the country you want to use this microSD card in.
      regulatory-domain: "GB"
      access-points:
        "My Net-Work":
          password: "mysupersecretpassword"
      # Don’t wait at boot for this connection to connect successfully
      optional: true

插入此 microSD 卡并启动树莓派后,cloud-init 将处理此配置并尝试自动连接到指定网络——让您无需连接显示器或键盘即可通过 SSH 登录或继续工作。

您可以配置的内容远不止基本的 Wi-Fi 凭证:支持多网络、优先级回退、静态 IP 分配、VLAN 等。完整参考请参阅cloud-init 官方网络文档.

随着 cloud-init 的引入,Raspberry Pi OS 还集成了 Netplan,这是一个被多个现代 Linux 发行版使用的统一网络配置抽象层。

关于 Netplan 的更多信息

Netplan 现在是 Raspberry Pi OS 网络配置的主要真实数据源。它使用自己基于 YAML 的配置格式,并可根据您选择的渲染器为 systemd-networkd 和 NetworkManager 渲染网络设置。这种方法的主要优势在于可移植性——Netplan 配置可在任何支持它的 Linux 发行版中重复使用,无论其底层使用的是 NetworkManager 还是 networkd。

要直接使用 Netplan,请将配置文件放在/etc/netplan/ — this is also where cloud-init stores your generated network configuration from network-config without modification. From there, you can generate the active configuration using:

sudo netplan generate

这将为所选后端(Raspberry Pi OS 上的 NetworkManager)写入相应的配置文件。要激活配置,请运行:

sudo netplan apply

您仍然可以使用nmcli as usual to inspect or manage connections. Since many existing tools and scripts rely on nmcli or the NetworkManager D-Bus API, there needs to be a communication layer between Netplan and NetworkManager. Canonical provides three patches that enable this two-way interoperability, allowing NetworkManager to signal configuration changes back to Netplan.

针对 Raspberry Pi OS,我们更进一步,引入了额外的补丁来改进此工作流:

  • NetworkManager 仅与使用netplan- prefix. If you want a new connection to be persisted by Netplan, give it that prefix when creating it, and it will be stored in Netplan’s configuration.
  • 在生成配置文件时,NetworkManager 会加载所有 Netplan 定义的连接,并将其解释后的运行时配置以 Netplan 能够理解的格式写回。
  • 在此过程中,所有.yaml and .yml files under /etc/netplan/ are cleared to avoid conflicting definitions across multiple layers.
  • 这不会先拔下连接器的一端影响存储在/etc/NetworkManager/system-connections/.
  • Netplan 在运行时生成的任何配置文件都将显示在/run/NetworkManager/system-connections/ and will also use the netplan- prefix.

这种方法确保了两个系统之间的一致性,并防止了通过 NetworkManager 工具编辑或重新加载配置文件时出现配置漂移。

总结

随着 cloud-init 和 Netplan 现已集成到 Raspberry Pi OS 中,首次启动配置变得更加强大、可重复,且在不同环境中更具可移植性。无论您是配置单台设备,还是为教室、实验室或物联网部署准备数十台树莓派,这些新工具都能让您在系统通电前轻松预先定义所有内容——用户、网络、接口等。

随着Raspberry Pi Imager 2.0的发布,Raspberry Pi OS 的 cloud-init 配置现在默认生成。这使得在写入镜像后进一步自定义设置变得容易——只需编辑生成的user-data or network-config files on the boot partition. Imager 2.0 also understands the Raspberry Pi–specific rpi: options, so features like SPI or I2C can be enabled directly in the customisation UI.

传统方法仍然有效,但 cloud-init 和 Netplan 为更灵活、更现代的工作流打开了大门。我们将在未来的版本中继续扩展对 Raspberry Pi 专属 cloud-init 模块和简化配置功能的支持。

如果您创建了有趣的user-data or network-config templates, or have feedback about the new system, we’d love to hear from you in 论坛.


页眉图片资源: 背景设计由Freepik提供。 图标由ziadarts, Dimas Anom、 以及Gregor Cresnar通过Flaticon.

25条评论

Milliways avatar

如果在向毫无防备的公众发布之前就把这些写好文档,那就太好了。不幸的是,这似乎成了新常态;先发布东西,然后有时才补文档。
我刚刚进行了一次全新安装(为了测试新的镜像工具),并设法弄明白了这个问题,但我平时的流程却失败了。

Ashley Whittaker avatar
James W Williams avatar

我查看了完整文档,但没有看到如何指定账户的 uid 和 gid。在使用 NFS 的环境中这是必需的,因为 uid:gid 必须在所有主机上保持一致(至少对于 NFS v3 而言)。我希望这个功能是存在的,只是我漏看了。

Jesse Korhonen avatar

你好,

您可以在以下位置找到添加用户的完整 schema:https://cloudinit.readthedocs.io/en/latest/reference/modules.html#mod-cc-users-groups

因此,您可以使用整数来指定 uid 属性。

uid:(integer/string) 用户 ID。默认值 [system default]。

在 22.3 版本中更改。已弃用“string”类型。请改用“integer”。

对于用户组,我认为目前没有明确的方法来实现这一点,而且该功能甚至不在路线图中:https://github.com/canonical/cloud-init/issues/2314

此致敬礼,
Jesse Korhonen

Mark Adler avatar

这能解释为什么 1.9.6 版本的烧录工具不再进行任何设置,只给我留下一个原生操作系统了吗?我试了 2.0.0 版本的烧录工具,但根本没法用,花了半个小时才写入 7%,然后就卡住不动了。

Tom Dewey avatar

没错。正如帖子里所说:

这标志着我们开始过渡,不再使用基于 firstrun.sh 脚本的传统首次启动自定义系统。

cloud-init 的优势在于,即使你决定不使用 Raspberry Pi Imager,编写配置文件来进行自定义也比 firstrun.sh 风格的系统简单得多。

2.0.0 之前版本的 Raspberry Pi Imager 如果指向自定义仓库文件,或许能执行有限的自定义操作——但它们缺乏本文中介绍的 cloud-init 扩展功能。

Alistair Buxton avatar

是的。我今天浪费了好几个小时,想弄明白为什么自定义功能莫名其妙就不起作用了。因为 Imager 当然不会告诉你它下载了新版本。那样就太简单了。

Alistair Buxton avatar

这篇文章说“传统方法仍然有效”,但在 rpi-imager 的 GitHub 上,开发者却表示:

“【2025年11月24日之后发布的 Raspberry Pi OS Trixie】现已切换至‘init_format’: ‘cloudinit-rpi’,以利用 Imager 2.0 的所有新功能。这带来了一个不幸的副作用,即 Imager 1.9.x 现在无法自定义 Raspberry Pi OS Trixie,因为旧版 Imager 不知道如何处理 cloudinit-rpi。”

所以到底哪个是对的?

Jeff Geerling avatar

很高兴看到 cloud-init 终于被用于所有首次启动配置。这终于统一了我各种设备设置脚本,能在众多发行版中拥有这样一个标准层真是太好了。

时机上有点尴尬,因为这既是镜像的变更,也是 Imager 工作方式的改变,感觉有个过渡期会比较好,但既然它解决了我自己的问题,我也就不抱怨了!

Rich Robinson avatar

对新手和缺乏经验的用户来说,学习曲线可能太陡峭了。需要在某个地方提供问答式的引导,给出简单的设置流程。这并不完全符合 KISS 原则,尽管对于设备集群部署来说会更好。第一眼看上去挺吓人的。

jordana avatar

对 USB gadget 模式的支持非常棒,当 WiFi 只能通过强制门户网络使用时,这是一个非常有价值的功能。

Milliways avatar

帖子中说“基于 Debian Trixie 的最新 Raspberry Pi OS 版本现已包含 cloud-init”。这链接到了https://www.raspberrypi.com/news/trixie-the-new-version-of-raspberry-pi-os/2025年10月2日,但那个版本并没有使用 cloud-init;直到11月24日的版本才加入。安装初始版本的用户用的是传统的首次启动自定义。

Milliways avatar

现在这里又说“最新镜像(发布于2025年10月20日)”,但在以下地址根本没有这个镜像:https://downloads.raspberrypi.org/raspios_arm64/images/也没有发布说明!

bensimmo avatar

就是需要一个 Raspberry Pi Cloud-init 配置程序/应用,让大家用起来更简单。
为我们提供所有 Pi 选项并加以解释,全部以友好的桌面化方式呈现(而不是纯文本加祈祷),甚至将其集成到 Pi Imager 中,这样我们只需保存/导入文件,就能跳过其他配置选项。

AndrewS avatar

“就是需要一个 Raspberry Pi Cloud-init 配置程序/应用,让大家用起来更简单。”

这就是 Imager 2.0 的作用 :-)
https://www.raspberrypi.com/news/a-new-raspberry-pi-imager/

bensimmo avatar

我相信我可以在里面加载一个文件。
但要用所有选项实际创建该文件本身,而不仅仅是提供一些“新用户”设置。
而且无论如何,如果没有可安装的驱动器,我们也无法进入设置界面!
没有 i2c、1-wire、高级 WiFi、静态 IP、创建 AP、串口……也就是你们 raspberrypi 文档中提到的一些功能。

但总的来说,它确实让重复安装变得更轻松了 :-)
这是个不错的举措。

Esbeeb avatar

我不喜欢 yaml。在 netplan 的 yaml 配置中处理 IPv6 地址时可能会踩坑。默认情况下,大家不会知道必须给 IPv6 地址加上双引号。

我喜欢通过 VNC 登录 Pi,然后在“托盘”(或者不管它叫什么)里用图形界面操作 NetworkManager。*只要你想做任何非标准的操作*,比如创建网桥设备或调整 wireguard 隧道(我经常这么做),图形化配置能比 netplan 的 yaml 这个“暗黑折磨室”好上 100 倍,清晰地展现所有配置选项。

Thierry de Coulon avatar

Cloud-init 在重启时抛出错误,毁了我的安装。我不需要这东西,它太烦人了,所以现在我在安装后第一件事就是把它卸载掉。

tommylovell avatar

是的,我注意到了。好主意,但我同意 Milliways 的看法,如果能有个“不那么隐晦”的提前通知就好了。这打破了我所有的流程。我用 Imager;把输出复制回磁盘;用 ‘dd’ 写入 Pi 磁盘;把磁盘移到 Pi 上;启动 Pi;最后用 Ansible 全面定制 Pi。比平时出了更多问题。怪我没有持续关注我的 Pi。

DDSCentral avatar

刚在重新设置 Pi 4 时注意到。
Netplan/cloud-init 这些东西确实有其用处,但我个人很讨厌它们,这也是我选用标准 Debian 而不是 Ubuntu 的原因之一。
简单老旧的 ifupdown 对我来说就足够了,而且就是好用。
而且我已经有一个正常工作的网络配置,不想把它重写成 Ubuntu 风格。
“apt purge netplain.io cloud-init”
不过人各有好吧。

JR avatar

不适用于 Trixie Lite 32 bit。

Monika Singh avatar

这个 cloud-init 在启动前需要连接 Wi-Fi 或 LAN 才能联网吗?

davkau avatar

给仍在为此问题困扰的朋友更新一下:如果您使用的是
基于 Trixie 的 Raspberry Pi OS Lite(64 位),请务必使用 Imager v2.0.6(或更新版本)。1.8.5 等旧版本无法应用您的自定义配置(主机名、SSH、WiFi)。
我不完全清楚为什么旧版 Imager 会静默失败——这可能与 Trixie 改用 cloud-init 进行首次启动自定义有关,似乎只有 Imager 2.x 能识别。无论如何:升级 Imager 就解决了问题。已在 Pi 4 上测试并正常运行。

thomas avatar

如果引入此类更改,系统随后在尝试使用现有工具或方法(例如使用‘hostname -b’或 raspi-config 更改主机名)时,应该做出透明的响应。我今天使用 Raspberry Pi 镜像设置了系统并尝试更改主机名,结果发现重启后所有更改都被撤销了。只有上网搜索才找到一些关于如何修复此问题及其背后原因的线索。在我看来,系统现在的表现简直形同损坏。对于新手来说,这完全是个致命障碍。

FM_81 avatar

关于“network-config”的一个问题:假设您想在首次启动时连接到 WiFi,但不想以明文形式提供 WLAN 密码,而是使用哈希值?(就像以前在‘wpa-supplicant.conf’中可以做的那样。也许文档的某个地方有写,但如果有的话,那隐藏得也太深了,我根本找不到……)谢谢!

评论已关闭