(十六)Linux nmcli網路管理
阿新 • • 發佈:2022-05-25
一、Linux nmcli 網路管理
RHEL 和 CentOS 系統預設使用 NetworkManager 來提供網路服務,這是一種動態管理網路配置的守護程序,能夠讓網路裝置保持連線狀態。可以使用 nmcli 命令來管理 NetworkManager 服務。nmcli 是一款基於命令列的網路配置工具,功能豐富,引數眾多。
nmcli自帶圖形網路介面配置工具
命令:nmtui
二、常用操作
查詢操作 # 查詢伺服器所有網絡卡 nmcli connection show # 檢視正在使用的網絡卡 nmcli connection show -active # 檢視指定網絡卡資訊詳情 nmcli connection show 裝置名稱 # 檢視所有網路裝置狀態 nmcli device status # 檢視所有網路裝置狀態詳情 nmcli device show # 檢視指定網路裝置狀態詳情 nmcli device show 裝置名稱
三、修改操作
# 建立DHCP型別網路裝置、新建配置檔案 nmcli connection add con-name "連線名" type 裝置型別 ifname 裝置名稱 例項:nmcli connection add con-name "dhcp" type ethernet ifname ens33 # 建立靜態型別網路裝置、新建配置檔案、新增IP地址、新增閘道器。 ↓面單獨拿出來詳解 nmcli connection add con-name "連線名" ifname 裝置名稱 autoconnect no type 裝置型別 ip4 IP地址 gw4 閘道器地址 例項:nmcli connection add con-name "static" ifname ens33 autoconnect no type ethernet ip4 192.168.1.240 gw4 192.168.1.1 注:autoconnect 為是否開啟網路裝置。 對應配置檔案: autoconnect no/yes | ONBOOT=no/yes # 修該模式為手動(static) nmcli connection modify 裝置名稱 ipv4.method manual ipv4.addresses IP地址/閘道器 例項:nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.1.10/24 注:原本DHCP模式需要手動新增IP地址,不然會報錯。 對應配置檔案:ipv4.method manual/auto | BOOTPROTO=static/dhcp # 修該模式為自動(dhcp) nmcli connection modify 裝置名稱 ipv4.method auto 例項:nmcli connection modify ens33 ipv4.method auto 對應配置檔案:ipv4.method manual/auto | BOOTPROTO=static/dhcp # 修該IP地址 nmcli connection modify 裝置名稱 ipv4.addresses IP地址/閘道器 例項:nmcli connection modify ens33 ipv4.addresses 192.168.1.10/24 對應配置檔案:ipv4.addresses x.x.x.x/x | IPADDR=x.x.x.x 、PREFIX=x # 修該閘道器 nmcli connection modify 裝置名稱 ipv4.gateway 閘道器地址 例項:nmcli connection modify ens33 ipv4.gateway 192.168.1.1 對應配置檔案:ipv4.gateway x.x.x.x | GATEWAY=x.x.x.x # 已建立靜態網路裝置、新增DNS nmcli connection modify 裝置名稱 ipv4.dns DNS地址 例項:nmcli connection modify ens33 ipv4.dns 8.8.8.8 對應配置檔案:ipv4.dns x.x.x.x | DNS=x.x.x.x # 已建立靜態網路裝置、新增第二個DNS nmcli connection modify 裝置名稱 +ipv4.dns DNS地址 例項:nmcli connection modify ens33 +ipv4.dns 9.9.9.9 對應配置檔案:ipv4.dns x.x.x.x | DNS2=x.x.x.x # 修該模式為手動(static)、IPV6 nmcli connection modify 裝置名稱 ipv6.addresses 'IPV6地址/掩碼' ipv6.method 手動 例項:nmcli connection modify ens33 ipv6.addresses '2001:ac18::132/64' ipv6.method manual 注:原本DHCP模式需要手動新增IP地址,不然會報錯。
四、刪除操作
# 刪除指定網路裝置、DNS地址
nmcli connection modify 裝置名稱 -ipv4.dns 已存在DNS地址
例項:nmcli connection modify ens33 -ipv4.dns 8.8.8.8
# 刪除網路連線配置檔案
nmcli connection delete 裝置名稱
例項:nmcli connection delete ens33
五、其他操作
# 啟用網路裝置連線 nmcli connection up 裝置名稱 例項:nmcli connection up ens33 # 停用網路連線、可以被自動啟用 nmcli connection down 裝置名稱 例項:nmcli connection up ens33 # 停用網路連線,禁止被啟用 nmcli device disconnect 裝置名稱 例項:nmcli device disconnect ens33 # 重新載入網路配置檔案 nmcli connection reload