CentOS 7.x 網路管理工具 nmcli

nmcli 是 CentOS 7.x 預設的網路管理工具,不過之前一直習慣使用 ifconfig,所以並沒有特別去研究。
# which nmcli
/usr/bin/nmcli

如果找不到,請安裝 NetworkManager 套件
# yum install NetworkManager
# systemctl start NetworkManager.service

參考網頁:
2.3. 使用 NetworkManager 命令行工具 nmcli
RHEL / CentOS 7 網路管理工具 — nmcli – Linux 技術手札[@more@]1 顯示所有連線:
# nmcli connection show
# nmcli c s
NAME     UUID                                  TYPE            DEVICE
enp0s25  d28b1a52-51d3-482c-b36e-37b7b80d7ded  802-3-ethernet  enp0s25
enp1s0   6d31aafd-1033-4853-ba71-e062608189b0  802-3-ethernet  enp1s0
enp1s1   6af9cc1f-4fac-40e8-80e6-6bda5b66886b  802-3-ethernet  enp1s1

只顯示 active 的連線:
# nmcli connection show –active
# nmcli connection show -a
# nmcli c s -a
NAME     UUID                                  TYPE            DEVICE
enp0s25  d28b1a52-51d3-482c-b36e-37b7b80d7ded  802-3-ethernet  enp0s25
enp1s0   6d31aafd-1033-4853-ba71-e062608189b0  802-3-ethernet  enp1s0
enp1s1   6af9cc1f-4fac-40e8-80e6-6bda5b66886b  802-3-ethernet  enp1s1

顯示所有 NetworkManager 辨識到的裝置及它們目前的狀態:
# nmcli device status
# nmcli d s
DEVICE   TYPE      STATE      CONNECTION
enp0s25  ethernet  connected  enp0s25
enp1s0   ethernet  connected  enp1s0
enp1s1   ethernet  connected  enp1s1
lo       loopback  unmanaged  —

停用網卡:
# nmcli device disconnect enp1s1
# nmcli d d enp1s1
Device ‘enp1s1’ successfully disconnected.

啟用網卡:
# nmcli device connect enp1s1
# nmcli d c enp1s1
Device ‘enp1s1’ successfully activated with ‘6af9cc1f-4fac-40e8-80e6-6bda5b66886b’.

加入固定 IP:
# nmcli connection add type ethernet con-name test-lab ifname enp1s1 ip4 10.10.10.10/24 gw4 10.10.10.254
# nmcli con add type ethernet con-name test-lab ifname enp1s1 ip4 10.10.10.10/24 gw4 10.10.10.254
Connection ‘test-lab’ (‘6af9cc1f-4fac-40e8-80e6-6bda5b66886b) successfully added.
# nmcli con add type ethernet con-name test-lab ifname enp1s1 ip4 10.10.10.10/24 gw4 10.10.10.254 ip6 abbe::cafe gw6 2001:db8::1

設定使用的 Dns Servers:
# nmcli connection modify test-labipv4.dns “168.95.1.1 140.111.66.1 8.8.8.8”
# nmcli con mod test-lab ipv4.dns “168.95.1.1 140.111.66.1 8.8.8.8”
# nmcli con mod test-lab ipv6.dns “2001:b000:168::2 2001:288:a201::66:1 2001:4860:4860::8888”

啟動新連結線:
# nmcli connection up  test-lab ifname enp1s1
# nmcli con up  test-lab ifname enp1s1