最後更新: 2020-07-21
介紹
nmcli - command line tool for controlling NetworkManager(它是 Redhat 系的東西來)
nm = NetworkManager
目錄
- Usage
- WiFi
- Tunnel Interface
- Cheat List
查看是否在使用 nmcli
nmcli con show
Error: NetworkManager is not running.
Usage
通用 Opts
-p | --pretty # Output is pretty
-t | --terse # Output is terse. Suitable for computer (script) processing.
-f | --fields { field1,field2... | all | common } # Specify what fields (column names) should be printed.
-a | --ask # stop and ask for any missing required arguments
一般 Status
nmcli general [status]
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN connected full enabled enabled enabled enabled
Tells you whether NetworkManager is running or not.
nmcli -t -f RUNNING general
running
NIC Up / Down
nmcli -f GENERAL.STATE con show lxcbr0
GENERAL.STATE: activated
Networking
nmcli networking { on | off | connectivity }
Enable or disable networking control by NetworkManager.
!! All interfaces managed by NetworkManager are deactivated when networking is disabled.
nmcli connection ...
nmcli [OPTIONS] connection [COMMAND] [ARGUMENTS]
[show]
NAME UUID TYPE DEVICE ens4 38f8f80e-940f-467e-80cf-bd25fec3f950 ethernet ens4 Bridge lxcbr0 94968977-6656-cf4f-5a76-0810844a61de bridge lxcbr0
* NetworkManager stores all network configuration as "connections"
(在 /etc/sysconfig/network-scripts 內每個 file 也是 connections)
i.e.
# show auto-connect property
nmcli -f name,autoconnect conn [show]
NAME AUTOCONNECT ens4 yes Bridge lxcbr0 yes
fields
它是 OPTIONS 來, 所以不能 "nmcli conn show -f f1,f2,f3"
-f | --fields field1,field2... | all
Output 的大細階
nmcli conn show eth0 | grep -i gateway
ipv4.gateway # settings IP4.GATEWAY # 當前情況
up/down [ id | uuid | path ] ID [ifname ifname]
up: Activate a connection
down: Deactivate a connection
i.e.
nmcli con up lxcbr1
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
Remark:
# Apply changes to an already active connection
nmcli con up br0
add [save {yes | no}] {option value | [+|-]setting.property value}...
save Controls whether the connection should be persistent(default: yes)
type ethernet, wifi, .. vlan .. bond .. team .. bridge .. vpn .. tun
i.e.
# 建立了 /etc/sysconfig/network-scripts/ifcfg-bridge-lxcbr0
# nmcli c s => NAME: bridge-lxcbr0
nmcli con add type bridge ifname lxcbr0
P.S.
# 很多時也會立即改名:
nmcli con mod bridge-lxcbr0 connection.id lxcbr0
clone [--temporary] [id | uuid | path] ID new_name
i.e.
nmcli con clone "Bridge lxcbr0" lxcbr1
Bridge lxcbr0 (94968977-...) cloned as lxcbr1 (78a224c1-...).
Remark
* 會自動建立 ifcfg-lxcbr1
* Clone 出來的係 Profile 所以佢地係會相同 "DEVICE=lxcbr0"
modify [--temporary] [ id | uuid | path ] ID { option value | [+|-]setting.property value } ...
Add, modify or remove properties in the connection profile.
An empty value ("") resets the property value to the default.
"+" append
"-" remove
The + and - modifiers only have a real effect for properties that support them.
常用 Setting Example
# Set NIC name
nmcli con modify lxcbr1 connection.interface-name lxcbr1
# Set Connection Name
# "nmcli conn show" 的 NAME
nmcli conn modify "Current Name" connection.id "New Name"
# Disabling IPv6
nmcli con modify MyNIC ipv6.method disabled
# Set IP address
# Format: IP/M GW
nmcli con mod lxcbr1 ipv4.method manual ipv4.addr "192.168.201.254/24"
# One more IP address on NIC
nmcli con modify lxcbr1 +ipv4.addresses "192.168.100.25/24"
# Set Gateway
nmcli con modify <name> ipv4.gateway <gateway> # 空接 set gateway
nmcli con modify <name> ipv4.addresses "<ip addr and mask> <gateway>" # Set IP 時同時 set gateway
nmcli con modify <name> ipv4.gateway "" # Remove gateway
* 要行 "nmcli conn up NAME" 去 apply settings
# Remove On DNS Server
# To ignore the DNS servers sent to a host by a DHCP server
nmcli con mod br0 ipv4.ignore-auto-dns yes
# 當有 8.8.8.8 及 8.8.4.4 時只 remove 8.8.4.4
nmcli con mod br0 -ipv4.dns 8.8.4.4
# Set DNS Server
nmcli con mod br0 ipv4.dns 8.8.8.8,8.8.4.4
nmcli con up br0
# Bridge Setting (range <2-30>)
nmcli con mod lxcbr1 bridge.forward-delay 2
# 改 MTU
nmcli con mod lxcbr0 mtu 9000
# Reboot autoconnect
connection.autoconnect
# FW Zone Setting
connection.zone
edit { [ id | uuid | path ] ID | [type type] [con-name name] }
Edit an existing connection or add a new one, using an interactive editor
delete id | uuid | path ...
Delete a configured connection
成功 Delete 後, 在 /etc/sysconfig/network-scripts 相對應的檔案會被刪除
reload
Reload all connection files from disk.
ie. 修改了 config 檔
/etc/sysconfig/network-scripts/ifcfg-ens160
load filename...
Device
Quick list ethernet card
# d = device - show and manage network interfaces
nmcli dev
DEVICE TYPE STATE CONNECTION ens4 ethernet connected ens4 lxcbr0 bridge connected Bridge lxcbr0 c1-eth0 ethernet unmanaged -- lo loopback unmanaged --
show [ifname]
Show detailed information about devices.
GENERAL.DEVICE:eth0 ... IP4.ADDRESS[1]:R.R.R.R/25 IP4.GATEWAY:G.G.G.G IP4.ROUTE[1]:dst = R.R.R.R/25, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]:dst = 0.0.0.0/0, nh = 202.181.146.1, mt = 100 IP4.DNS[1]:8.8.8.8 IP4.DNS[2]:8.8.4.4 ...
set ifname [ autoconnect { yes | no } ] [ managed { yes | no } ]
Set device properties.
modify ifname { option value | [+|-]setting.property value } ...
Modify the settings currently active on the device.
connect / disconnect ifname
Disconnect a device and prevent the device from automatically activating further connections without user/manual intervention.
software devices may mean that the devices will disappear
delete ifname...
Delete a device. The command removes the interface from the system.
only works for software devices
WiFi
# Listing available Wi-Fi APs
nmcli device wifi list
# Connect to a password-protected wifi network
nmcli --ask device wifi connect "$SSID"
# Showing general information and properties for a Wi-Fi interface
nmcli -p -f general,wifi-properties device show wlan0
Tunnel Interface
nmcli conn add type tun ifname tun0 con-name tun0
nmcli conn mod tun0 ipv4.addr "10.10.10.1/24"
nmcli con modify tun0 ipv6.method disabled
nmcli conn up tun0
Cheat List
Show NIC
nmcli con
nmcli -t device show # 必須要加上 show 才看到 IP
Set Name
nmcli con mod bridge-lxcbr0 connection.id lxcbr0 # NAME
nmcli con mod lxcbr1 connection.interface-name lxcbr1 # DEVICE
Set manual IP (必須)
# Default 是 auto, 當人手設定 IP 時, 會突然斷線. 所以必須設定成 manual
nmcli con mod $MyNAME ipv4.method manual
Set IP
nmcli con mod $MyNAME ipv4.addr "192.168.88.188/24"
Set GW
nmcli con mod $MyNAME ipv4.gateway 192.168.88.1
Set DNS
nmcli con mod br0 -ipv4.dns 8.8.4.4
nmcli con mod $MyNAME ipv4.dns 8.8.8.8,8.8.4.4
Set Search
nmcli con mod br0 ipv4.dns-search ""
Reboot 機後自動 up 返
nmcli con mod $MyNAME connection.autoconnect yes
Apply Setting
nmcli con up $MyNAME
From Static IP to DHCP
nmcli con mod ens4 ipv4.method auto # 轉用 DHCP
nmcli con up ens4 # 套用 setting
ip addr show ens4 # 會有兩粒 IP
ip route # Gateway 不是由 DHCP 獲得
cat /etc/resolv.conf # DNS Server 不是 DHCP 那個
Fix
nmcli con mod ens4 ipv4.gateway "" nmcli con mod ens4 ipv4.dns "" nmcli con mod ens4 ipv4.addr "" nmcli con up ens4