西楚骄子 發表於 2025-6-25 00:00:00

Ubuntu Server 22.04.5 LTS重启后IP被重置问题的具体解决方案

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>Ubuntu Server 22.04.5 LTS重启后IP被重置问题<ul class="second_class_ul"><li>1 问题描述</li><li>2 解决方案</li></ul></li><li>拓展:Ubuntu22.04服务器重启后没网络IP丢失<ul class="second_class_ul"><li>1、打开<code>01-network-manager-all.yaml</code>&nbsp;文件</li><li>2、添加下面的内容</li><li>3、配置生效</li><li>4、验证</li></ul></li></ul></div><p class="maodian"></p><h2>Ubuntu Server 22.04.5 LTS重启后IP被重置问题</h2>
<p>最近在使用Ubuntu Server 22.04做项目开发测试时发现每次重启和关机后,所设置的静态IP地址都会回复到安装系统时所设置的ip</p>
<p>Ubuntu Server 22.04 官网下载地址:https://cn.ubuntu.com/download/server/step1</p>
<p>所下载的系统版本如下图所示</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-250625115445492.png" /></p>
<p class="maodian"></p><h3>1 问题描述</h3>
<p>首先进入Ubuntu22.04系统后,查看当前IPv4地址:</p>
<div class="dxycode"><pre class="brush:bash;">ip -4 addr show</pre></div>
<p>可以看到目前本机ip地址是192.168.3.228</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-250625115445443.png" /></p>
<p>查看当前网络配置文件名</p>
<div class="dxycode"><pre class="brush:bash;">ls /etc/netplan</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-250625115445B1.png" /></p>
<p>使用nano进行编辑配置网络配置文件</p>
<div class="dxycode"><pre class="brush:bash;">nano /etc/netplan/50-cloud-init.yaml</pre></div>
<p>进入如下编辑页面后编辑需要修改的网络地址,例如此处修改为192.168.3.229</p>
<p>编辑完毕后按ctrl+O,然后按回车进行保存,再按ctrl+x退出</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-250625115445108.png" /></p>
<p>当时没注意看上面画红框的提示部分,大意就是此文件的配置可能会在每次系统重启时被cloud云文件配置的内容所进行覆盖,可以通过禁用 cloud-init 的网络配置来防止这种情况发生。</p>
<p>这就比较坑了,因为之前用centos7.6做开发时没有类似问题,之前还以为设置为静态IP地址就完事了,包括很多网上的修改静态IP地址教程也是这样写的,没想到Ubuntu居然还有默认云覆盖的机制(也可能是在我安装系统时没注意看勾选错了还是怎么的,至少我装了两个系统都有这个问题)</p>
<p>果然设置完192.168.3.229的IP地址后,系统重启马上又被修改成192.168.3.228</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-250625115446256.png" /></p>
<p>再进入网络配置文件查看发现也已经被覆盖</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-25062511544D18.png" /></p>
<p>比较好奇,另一个配置文件在哪里,毕竟如果要覆盖配置文件的话,那么另外一个地方必然有保存原始云配置文件的,在启动后进行读取强行覆盖到当前配置文件才对。</p>
<p>进入云配置目录,可以看到有个90-installer-network.cfg的文件</p>
<div class="dxycode"><pre class="brush:bash;">ls /etc/cloud/cloud.cfg.d</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-25062511544O03.png" /></p>
<p>查看90-installer-network.cfg文件,可以看到此处保留了系统安装时配置的IP地址</p>
<div class="dxycode"><pre class="brush:bash;">cat /etc/cloud/cloud.cfg.d/90-installer-network.cfg</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-25062511544M20.png" /></p>
<p>通过上面,已经知道了设置netplan的静态ip地址时重启后IP地址被覆盖的原因,接下来就是解决问题了。</p>
<p class="maodian"></p><h3>2 解决方案</h3>
<p><strong>1.直接修改云配置文件IP</strong></p>
<p>直接把90-installer-network.cfg文件的IP配置进行修改成需要的参数,这样虽然也行但是比较容易给后面使用Ubuntu服务器不熟悉的人挖坑,例如服务器需要迁移网络环境部署需要修改网络配置折腾半天弄不了。</p>
<p><strong>2.禁用cloud-init的网络配置</strong></p>
<p>这是netplan里推荐的做法,接下来我们使用这个方式解决重启后IP地址被覆盖问题。</p>
<p><strong>编辑 <code>cloud-init</code> 配置文件</strong>:</p>
<p>如果没有此文件直接创建再进行编辑</p>
<div class="dxycode"><pre class="brush:bash;">sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg</pre></div>
<p><strong>添加以下内容</strong>:</p>
<div class="dxycode"><pre class="brush:bash;">network: {config: disabled}</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250625/2-25062511544J36.png" /></p>
<p>编辑完毕后按ctrl+O,然后按回车进行保存,再按ctrl+x退出</p>
<p><strong>重启 <code>cloud-init</code> 服务</strong>:</p>
<div class="dxycode"><pre class="brush:bash;">sudo cloud-init clean
sudo reboot</pre></div>
<p>重启完毕后今后再使用netplan设置ip地址就不会被重置覆盖了,上述问题得到解决。</p>
<p class="maodian"></p><h2>拓展:Ubuntu22.04服务器重启后没网络IP丢失</h2>
<p class="maodian"></p><h3>1、打开<code>01-network-manager-all.yaml</code>&nbsp;文件</h3>
<div class="dxycode"><pre class="brush:bash;">vim /etc/netplan/01-network-manager-all.yaml</pre></div>
<p>也有的是<code>00-network-manager-all.yaml</code></p>
<p class="maodian"></p><h3>2、添加下面的内容</h3>
<div class="dxycode"><pre class="brush:yaml;"># Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
    eno1:
      dhcp4: no
      addresses:
      routes:
      - to: default
          via: 192.168.10.254
      nameservers:
      addresses: </pre></div>
<p>需要改动的地方</p>
<p><strong>1、addresses</strong></p>
<p>这里填写你自己的IP地址<code>/24</code>的意思是子网掩码位数,24位代表的是24个1,即255.255.255.0,不用管</p>
<p><strong>2、via</strong></p>
<p>这里写网关地址</p>
<p class="maodian"></p><h3>3、配置生效</h3>
<div class="dxycode"><pre class="brush:bash;">sudo netplan apply</pre></div>
<p>重启网络服务,如果没有任何提示,证明配置文件内容正确</p>
<p class="maodian"></p><h3>4、验证</h3>
<div class="dxycode"><pre class="brush:bash;">sudo reboot</pre></div>
<p>以上就是Ubuntu Server 22.04.5 LTS重启后IP被重置问题的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: Ubuntu Server 22.04.5 LTS重启后IP被重置问题的具体解决方案