用 netplan 给 Ubuntu 设置固定 IP
编辑 netplan 的 YAML 文件,为 Ubuntu 服务器配置固定的 IP 地址、网关和 DNS,然后应用生效。
编辑 /etc/netplan/01-static.yaml 写入 addresses、routes 和 nameservers,再运行 sudo netplan apply。
01命令
命令
$ sudo nano /etc/netplan/01-static.yaml && sudo netplan apply02作用说明
Ubuntu 服务器通过 /etc/netplan/ 下的 YAML 文件管理网络。先用 ip link 查到网卡名,然后在配置里写 dhcp4: false、CIDR 格式的地址(如 192.168.1.50/24)、routes 里的网关和 nameservers 里的 DNS,最后 sudo netplan apply 立即生效。YAML 对缩进非常敏感,每层固定两个空格,缩进错了 netplan 会直接报解析错误。
03风险
!注意: YAML 写错或网关填错会让网络整个断掉;远程操作时先备份原文件,并保留一个控制台或另一个 SSH 会话再测试。
04撤销
撤销
$ Remove the YAML block you added (or restore /etc/netplan backup) and run sudo netplan apply05来源
- netplan reference — Ubuntu Discourse依据: Canonical reference for netplan configuration files, static addressing, and applying changes to the renderer.
- netplan.yaml reference — Static addressing依据: Documents the YAML schema including dhcp4: false and addresses with CIDR notation for configuring a static IP.
最后验证: 2026-09-18 · 分类: 网络