2025年04月14日 19:45:41

根据你的描述,问题根源在于 cloud-init 服务自动还原了网络配置。由于 /etc/netplan/50-cloud-init.yaml 是由 cloud-init 生成的(文件头注释已明确提示),直接修改此文件会被 cloud-init 在下次启动时覆盖。以下是完整的解决方案:


一、永久禁用 cloud-init 的网络配置管理

  1. 创建禁用 cloud-init 网络配置的覆盖文件

    bash
    • 01
    • 02
    • 03
    sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg << EOF network: {config: disabled} EOF
    • 这个文件会告知 cloud-init 不再接管网络配置。
  2. 清理 cloud-init 缓存和旧配置

    bash
    • 01
    • 02
    sudo rm /etc/netplan/50-cloud-init.yaml # 删除自动生成的配置 sudo cloud-init clean --logs # 清理缓存

二、手动配置静态网络(替换原有配置)

  1. 创建新的 Netplan 配置文件

    bash
    • 01
    • 02
    • 03
    • 04
    • 05
    • 06
    • 07
    • 08
    • 09
    • 10
    • 11
    • 12
    • 13
    • 14
    sudo tee /etc/netplan/01-static-config.yaml << EOF network: version: 2 renderer: networkd ethernets: ens2: addresses: [192.168.1.99/24] routes: - to: default via: 192.168.1.1 # 修改网关为 192.168.1.1 nameservers: addresses: [192.168.1.1] # 修改 DNS 为 192.168.1.1 search: [] EOF
  2. 应用新配置

    bash
    • 01
    sudo netplan apply

三、验证配置持久性

  1. 重启系统

    bash
    • 01
    sudo reboot
  2. 检查网关和 DNS 是否生效

    bash
    • 01
    • 02
    ip route show default # 应显示 via 192.168.1.1 resolvectl status # 检查 DNS 是否指向 192.168.1.1

关键问题说明

  • 为什么直接修改会失效
    cloud-init 是云环境和虚拟化平台(如 VMware)中用于初始化系统的工具,默认会接管网络配置。每次启动时,它会根据云平台元数据或本地配置重新生成网络文件,覆盖手动修改。
  • 替代方案(如果仍需使用 cloud-init)
    如果你希望继续使用 cloud-init 管理网络,需通过 cloud-init 的配置文件(如 /etc/cloud/cloud.cfg)修改网络参数,但这需要更复杂的配置,不推荐在此场景使用。

2024年04月08日 11:51:19

  • 01
https://docs.aws.amazon.com/cli/v1/userguide/install-linux.html#install-linux-bundled

2024年03月25日 00:14:42

读取输入文件中的域名,并删除重复的二级域名,只保留主域名

  • 01
awk -F'.' '{split($0, parts, "."); print parts[length(parts)-1]"."parts[length(parts)]}' 1.txt | sort | uniq > 2.txt

2024年01月20日 19:52:53

链接标记指定跳转

  • 01
https://yangpin.link/categories/vyos-open.html#:~:text=Vyos

2024年01月11日 21:11:10

新赛季第一把拿下!

2023年12月29日 17:03:29

友链留言

  • 01
  • 02
  • 03
  • 04
名称:杨杨得亿 地址:https://yangpin.link 图标:https://yangpin.link/favicon.ico 描述:分享自己瞎折腾得一些痕迹。

2023年12月26日 21:01:07

文章前言

  • 01
<p style="font-family: '宋体', 'SimSun', sans-serif; color: darkgreen; font-size: 1.5em; font-weight: bold;">文章介绍:</p>

2023年12月26日 18:25:03

HTML链接新标签页打开

  • 01
<mew-message type="info"><a href="域名" target="_blank">内容</a></mew-message>

HTML链接本标签页打开

  • 01
<mew-message type="info"><a href="域名" target="blank">内容</a></mew-message>

2023年12月09日 12:25:53

三张长图并排展示代码:

  • 01
  • 02
  • 03
  • 04
  • 05
<div style="display: flex; justify-content: space-between;"> <img src="1.jpg" alt="Image 1" style="width: 100%;"> <img src="2.jpg" alt="Image 2" style="width: 100%;"> <img src="3.jpg" alt="Image 3" style="width: 100%;"> </div>

2023年12月08日 15:47:39

网盘链接跳转

  • 01
<mew-cloud type="default" url="https://alist.yydy.link:2023">Alist</mew-cloud>
  • 01
<mew-cloud type="360" url="" password="82kD">从360安全云盘分享文件</mew-cloud>
  • 01
<mew-cloud type="bd" url="" password="82kD">从百度网盘分享文件</mew-cloud>
  • 01
<mew-cloud type="ali" url=""></mew-cloud>
  • 01
<mew-cloud type="wy" url=""></mew-cloud>
  • 01
<mew-cloud type="github" url=""></mew-cloud>
  • 01
<mew-cloud type="gitee" url=""></mew-cloud>
  • 01
<mew-cloud type="lz" url=""></mew-cloud>