Stories

Detail Return Return

新版Ubuntu網絡配置不生效問題 - Stories Detail

[toc]

新版Ubuntu網絡配置不生效問題

在使用 Ubuntu Cloud Image 或帶有 cloud-init 的鏡像時,系統的網絡配置默認由 cloud-init 託管。cloud-init 會生成 netplan 配置文件,因此手動修改 netplan 文件可能會被覆蓋,看起來像 netplan 配置“不生效”。

在新版的ubuntu-22.04.5中默認開啓cloud-init(其他版本暫未發現)。解決辦法如下:

1 軟禁用(保留軟件包)

創建禁用標誌文件:(前文安裝的ubuntu-24.04.3默認就是此方式)

touch /etc/cloud/cloud-init.disabled

# 再次修改網絡配置即可生效
vim /etc/netplan/50-cloud-init.yaml
netplan apply   # 立即生效網絡配置

reboot    # 重啓後也會生效

通過配置禁用網絡管理模塊:(ubuntu-22.04.5網卡配置文件中介紹可用此方式)

echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

# 再次修改網絡配置即可生效
vim /etc/netplan/50-cloud-init.yaml
netplan apply  # 立即生效網絡配置

reboot    # 重啓後也會生效

ubuntu-22.04.5默認網絡配置文件

2 徹底卸載

移除軟件包及配置文件:

sudo apt purge cloud-init -y
sudo rm -rf /etc/cloud /var/lib/cloud
風險‌:可能導致依賴 cloud-init 的雲平台功能異常。

3 內核啓動參數完全禁用 cloud-init

# 在 GRUB 啓動參數中添加:
cloud-init=disabled

# 然後更新 GRUB
sudo update-grub
這個方法會讓 cloud-init 完全不啓動,適合完全手動管理系統。

4 DNS 配置

從 Ubuntu 18.04 開始默認啓用了 systemd-resolved 來管理 DNS。在該版本之後,/etc/resolv.conf 通常是一個指向 /run/systemd/resolve/stub-resolv.conf 的符號鏈接,文件的內容通常是 nameserver 127.0.0.53,表示使用本地的 systemd-resolved 作為 DNS 解析器。

新版本DNS配置文件中有説明:

新版本直接修改 /etc/resolv.conf 文件將不再生效,建議使用 systemd-resolved 管理。

# 修改dns配置
vim /etc/systemd/resolved.conf
[Resolve]
DNS=8.8.8.8 8.8.4.4    # 首選DNS
FallbackDNS=1.1.1.1 1.0.0.1    # 備用DNS
Domains=waluna.local    # 搜索域(域名後綴)

# 重啓 systemd-resolved 服務
systemctl restart systemd-resolved

# 檢查生效後的 DNS
resolvectl status

systemd-resolve --status    # 需要安裝命令

# 查看緩存中生效的配置
cat /run/systemd/resolve/stub-resolv.conf

配置DNS

如果不想使用 systemd-resolved 服務管理,可以採用下面方式:

# 禁用 systemd-resolved 服務
systemctl disable --now systemd-resolved.service

# 修改 dns 配置文件
vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search waluna.local

這樣就恢復成之前的配置方式了~


關於我
全網可搜《阿賢Linux》
CSDN、知乎、嗶哩嗶哩、博客園、51CTO、掘金、思否、阿里雲、騰訊雲、華為雲、今日頭條、百家號、GitHub、個人博客
公眾號:阿賢Linux
個人博客:blog.waluna.top
https://blog.waluna.top/

原文鏈接: 新版Ubuntu網絡配置不生效問題.

user avatar dhan Avatar zdyz Avatar Javaer1995 Avatar chen_christins Avatar startshineye Avatar tangqingfeng Avatar jimru Avatar abelethan Avatar ouysh1981 Avatar y_luoe_hai_61a734cbf3c94 Avatar
Favorites 10 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.