RHEL8和CentOS8怎么重啟網(wǎng)絡(luò)
本文主要講解如何重啟RHEL 8或者CentOS 8網(wǎng)絡(luò)以及如何解決RHEL8和CentOS8系統(tǒng)的網(wǎng)絡(luò)管理服務(wù)報錯,當(dāng)我們安裝好RHEL 8或者 CentOS 8,重啟啟動網(wǎng)絡(luò)時,會出現(xiàn)以下報錯:
# systemctl restart network.service
報錯信息如下
Failed to restart network.service: Unit network.service not found.
意思為無法重啟網(wǎng)絡(luò)服務(wù),原因是無法找到network.service網(wǎng)絡(luò)服務(wù)。
出現(xiàn)錯誤的原因是在RHEL 8和CentOS 8系統(tǒng)里,已廢棄network.service。因此只能通過其它方法進(jìn)行網(wǎng)絡(luò)配置,包括NM命令工具集。換言之,在rhel8上,必須開啟NM,否則無法使用網(wǎng)絡(luò)。
下文假設(shè)你的網(wǎng)卡標(biāo)識為xxx,可以通過以下命令確定網(wǎng)卡標(biāo)識符
# ip addr
比如ens33等等這類標(biāo)識。
- RHEL8或者CentOS8配置網(wǎng)絡(luò)的三種方法
RHEL8或者CentOS8應(yīng)該使用nmcli管理網(wǎng)絡(luò)
手工配置ifcfg,通過NM來生效
通過NM自帶工具配ip,比如nmcli
手工配置ifcfg,通過傳統(tǒng)network.service來生效
建議:推薦使用上述第1種網(wǎng)絡(luò)配置方法(手工配置ifcfg,通過NM生效),因為這樣既兼容了傳統(tǒng)的ifcfg配置,又能熟悉nmcli。舉例:
1.1 方法一:手工配置ifcfg,使用nmcli來生效新的網(wǎng)絡(luò)配置
假設(shè)網(wǎng)卡標(biāo)識符為xxx
# vi /etc/sysconfig/network-scripts/ifcfg-xxx
使用nmcli重新回載網(wǎng)絡(luò)配置
# nmcli c reload
如果之前沒有xxx的connection,則上一步reload后就已經(jīng)自動生效了
# nmcli c up xxx
1.2 方法二:RHEL8和CentOS8完全使用nmcli來管理網(wǎng)絡(luò)
查看網(wǎng)卡信息
# nmcli connection
NAME UUID TYPE DEVICE
ens33 a92fa07b-9b68-4d2b-a2e7-e55146099b1b ethernet ens33
ens36 418da202-9a8c-b73c-e8a1-397e00f3c6b2 ethernet ens36
# nmcli con xxx
顯示具體的網(wǎng)絡(luò)接口信息
# nmcli connection show xxx
顯示所有活動連接
# nmcli connection show --active
刪除一個網(wǎng)卡連接
# nmcli connection delete xxx
給xxx添加一個IP(IPADDR)
# nmcli connection modify xxx ipv4.addresses 192.168.0.58
給xxx添加一個子網(wǎng)掩碼(NETMASK)
# nmcli connection modify xxx ipv4.addresses 192.168.0.58/24
IP獲取方式設(shè)置成手動(BOOTPROTO=static/none)
# nmcli connection modify xxx ipv4.method manual
添加一個ipv4
# nmcli connection modify xxx +ipv4.addresses 192.168.0.59/24
刪除一個ipv4
# nmcli connection modify xxx -ipv4.addresses 192.168.0.59/24
添加DNS
# nmcli connection modify xxx ipv4.dns 114.114.114.114
刪除DNS
]# nmcli connection modify xxx -ipv4.dns 114.114.114.114
添加一個網(wǎng)關(guān)(GATEWAY)
]# nmcli connection modify xxx ipv4.gateway 192.168.0.2
可一塊寫入:
]# nmcli connection modify xxx ipv4.dns 114.114.114.114 ipv4.gateway 192.168.0.2
添加DNS
]# nmcli connection modify xxx ipv4.dns 114.114.114.114
刪除DNS
]# nmcli connection modify xxx -ipv4.dns 114.114.114.114
添加一個網(wǎng)關(guān)(GATEWAY)
]# nmcli connection modify xxx ipv4.gateway 192.168.0.2
可一塊寫入:
]# nmcli connection modify xxx ipv4.dns 114.114.114.114 ipv4.gateway 192.168.0.2
使用nmcli重新回載網(wǎng)絡(luò)配置
]# nmcli c reload
如果之前沒有xxx的connection,則上一步reload后就已經(jīng)自動生效了
]# nmcli c up xxx
1.3 方法三:安裝network.service服務(wù)
可以通過yum install network-scripts來安裝傳統(tǒng)的network.service,不過redhat說了,在下一個RHEL的大版本里將徹底廢除,因此不建議使用network.service。
- RHEL8或者CentOS8修改網(wǎng)卡名稱
2.1 刪除網(wǎng)卡連接,比如xxx
]# nmcli connection delete xxx
2.2 修改內(nèi)核參數(shù)配置文件
]# vi /etc/default/grub
2.3 植入內(nèi)核
]# grub2-mkconfig -o /boot/grub2/grub.cfg
3.4 重啟
]# reboot
3.5 添加網(wǎng)卡
]# nmcli connection add type ethernet con-name xxx
- RHEL8或者CentOS8使用nmtui工具配置網(wǎng)絡(luò)(圖形化工具)
]# nmtui
- 總結(jié)
CentOS8和RHEL8上一定要習(xí)慣使用nmcli來管理網(wǎng)絡(luò),network.service在未來會被正式廢除,沒有學(xué)習(xí)的必要了。
/etc/sysconfig/network-scripts/ifcfg-eth0 每一行表示的意思對應(yīng)如下:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 #網(wǎng)卡設(shè)備名稱
ONBOOT=yes #啟動時是否激活 yes | no
BOOTPROTO=static #協(xié)議類型
IPADDR=192.168.1.90 #網(wǎng)絡(luò)IP地址
NETMASK=255.255.255.0 #網(wǎng)絡(luò)子網(wǎng)地址
GATEWAY=192.168.1.1 #網(wǎng)關(guān)地址
BROADCAST=192.168.1.255 #廣播地址
HWADDR=00:0C:29:FE:1A:09 #網(wǎng)卡MAC地址
TYPE=Ethernet #網(wǎng)卡類型為以太網(wǎng)