nmcli 指令操作

2024-12-18 linux NetworkManager

處理前動作

檢查文件中已經有配置: /etc/network/interfaces 如果有需要註解,範例如下:

# The primary network interface
# allow-hotplug enp1s0
# iface enp1s0 inet dhcp

新增網卡配置

sudo nmcli connection add type ethernet con-name enp1s0 ifname enp1s0 ipv4.addresses 192.168.56.100/24 gw4 192.168.56.1 ipv4.dns 127.0.0.1  ipv4.method manual

設定完需要重起 NetworkManager

sudo systemctl restart NetworkManager

修改網卡配置

  • 新增額外的DNS
sudo nmcli connection modify enp1s0 +ipv4.dns 8.8.8.8
  • 修改網址
sudo nmcli connection modify enp1s0 ipv4.addresses 192.168.17.208/24
  • 修改 gateway
sudo nmcli connection modify enp1s0 ipv4.gateway 192.168.17.254
  • 修改DNS
sudo nmcli connection modify enp1s0 ipv4.dns 192.168.1.1
  • 設定網卡為手動配置
sudo nmcli connection modify enp1s0 ipv4.method manual

維護常用指令

  • 列出網卡
sudo nmcli device
  • 重起網卡
sudo nmcli connection down enp1s0
sudo nmcli connection up enp1s0