完美解決網卡配置文件重啓自動還原問題

說明

虛擬機修改了/etc/sysconfig/network-scripts/ifcfg-eth0網卡文件 保存後,重啓網絡服務( systemctl restart network)是ip是存在並且能正常使用的。但是重啓系統之後ifcfg-eth0內容被重置了。
你打開配置文件後,會發現,大概內容和下面一樣,並且頂部都有一行註釋,大概意思就是 有cloud-init自動創建,不要編輯這個文件。

# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
HWADDR=fe:fc:fe:46:95:ba
ONBOOT=yes
TYPE=Ethernet
USERCTL=no

cloud-initopenstack的組件,所以這個問題一般發生在 openstack虛擬機或者配置了openstack的虛擬機上!!! (這種問題大概就是 openstack設置的ip段,這個虛擬機dhcp無法獲取,所以ip就爲空)
所以cloud-init的配置文件這就不做解釋,我的是普通虛擬機,但是我當初配置了openstack做實驗,可能沒卸載乾淨吧,有遺留問題發生了網卡重啓被重置的問題。

解決方法

方式一

這是我在網上找的 步驟 我親測不行,或許是因爲我的openstack已經沒有使用,組件不全導致的無效,如果你的openstack在正常使用,可以使用方式一。

  • 嘗試了關閉自帶網絡管理工具:
[root@compute1 ~]# systemctl stop NetworkManager
[root@compute1 ~]# systemctl disable NetworkManager

無效。重啓後,ifcfg-eth0內容仍被重置。

  • 找一找服務:systemctl list-unit-files | grep cloud*
    是否有下面4項爲enable
[root@compute1 ~]# systemctl list-unit-files | grep cloud*
cloud-config.service                          enabled
cloud-final.service                           enabled
cloud-init-local.service                      enabled
cloud-init.service                            enabled 
cloud-config.target                           static  
  • 禁止 cloud-init服務接管網絡

[root@compute1 ~]$vim /etc/cloud/cloud.cfg

#在最後面輸入下列內容
network:
  config:disabled
  • 清理,檢查cloud-init配置文件
[root@compute1 network-scripts]$ cd /usr/lib/python2.7/site-packages/cloudinit/sources/
[root@compute1 network-scripts]$ rm -rf __init__.pyc 
[root@compute1 network-scripts]$ rm -rf __init__.pyo
[root@compute1 network-scripts]$ rm -rf /var/lib/cloud/* 
[root@compute1 network-scripts]$ rm -rf /var/log/cloud-init* 
[root@compute1 network-scripts]$ cloud-init init --local
[root@compute1 network-scripts]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
[root@compute1 network-scripts]$ reboot

方式二

我這個方式比較暴力,因爲我不用openstack了,而我這個問題是cloud.init引起的問題,所以我就把和cloud.init的所有服務都停了並關閉開機自啓!

[root@compute1 network-scripts]$systemctl disable cloud-init-local cloud-init cloud-config cloud-final 
[root@compute1 network-scripts]$systemctl stop cloud-init-local cloud-init cloud-config cloud-final
[root@compute1 ~]#
[root@compute1 ~]# systemctl list-unit-files | grep cloud*
cloud-config.service                          disabled
cloud-final.service                           disabled
cloud-init-local.service                      disabled
cloud-init.service                            disabled
cloud-config.target                           static  

配置網絡,重啓服務器,ip依舊存在,問題解決!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章