使用nmcli-network-manager管理wifi网络

// 使用 nmtui 可以在命令行界面进行网络配置
// 配置文件:/etc/NetworkManager/NetworkManager.conf
// 注意:OBJECT和COMMAND可以用全程也可以用简称,最少可以只用一个字母,但我们一般用头三个字母,这样不容易搞错

root@desktop:~# nmcli -help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -t[erse]                                   terse output
  -p[retty]                                  pretty output
  -m[ode] tabular|multiline                  output mode
  -c[olors] auto|yes|no                      whether to use colors in output
  -f[ields] <field1,field2,...>|all|common   specify fields to output
  -e[scape] yes|no                           escape columns separators in values
  -n[ocheck]                                 don't check nmcli and NetworkManager versions
  -a[sk]                                     ask for missing parameters
  -s[how-secrets]                            allow displaying passwords
  -w[ait] <seconds>                          set timeout waiting for finishing operations
  -v[ersion]                                 show program version
  -h[elp]                                    print this help

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes
// WIFI 相关 ...
nmcli device wifi // 扫描

root@desktop:~# nmcli device wifi 
*  SSID                 MODE   CHAN  RATE       SIGNAL  BARS  SECURITY  
   --                   Infra  1     54 Mbit/s  100     ▂▄▆█            
   dnet                 Infra  11    54 Mbit/s  100     ▂▄▆█  WPA2      
   danoo-007            Infra  11    54 Mbit/s  84      ▂▄▆█  WPA1 WPA2 
   danoo100             Infra  3     54 Mbit/s  74      ▂▄▆_  WPA1 WPA2 

// 使用方法:
connect <(B)SSID> [password <password>] [wep-key-type key|phrase] [ifname <ifname>]
                [bssid <BSSID>] [name <name>] [private yes|no] [hidden yes|no]

// 关闭/打开 所有无线网卡
nmcli r wifi off
nmcli r wifi on

// 禁用网卡
nmcli device disconnect ifname wlan0

// 停用 wlan0 某一个活动的连接
nmcli con to check
nmcli con down <wifi name> to disconnect

// 连接 WIFI 网络
nmcli dev wifi con "wifi name" password "wifi password"

// 启用连接 WIFI 
nmcli con up <wifi name>

// 删除WIFI连接 - 配置
nmcli con delete <wifi name>
// 同样支持 hotspot 热点 ...

ARGUMENTS := wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>]
              [band a|bg] [channel <channel>] [password <password>]

Create a Wi-Fi hotspot. Use 'connection down' or 'device disconnect'
to stop the hotspot.
Parameters of the hotspot can be influenced by the optional parameters:
ifname - Wi-Fi device to use
con-name - name of the created hotspot connection profile
ssid - SSID of the hotspot
band - Wi-Fi band to use
channel - Wi-Fi channel to use
password - password to use for the hotspot
// 启用/禁用所有网络连接
nmcli net on/off
// 查看网卡信息
nmcli device show // 所有
nmcli device show eth0
nmcli device show wlan0

root@desktop:~# nmcli device show eth0
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:30:18:57:3D:23
GENERAL.MTU:                            1400
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
WIRED-PROPERTIES.CARRIER:               off
IP4.ADDRESS[1]:                         169.254.4.9/16
IP4.GATEWAY:                            0.0.0.0
IP6.ADDRESS[1]:                         fe80::230:18ff:fe57:3d23/64
IP6.GATEWAY:   
// 查看设备状态
nmcli device status

root@desktop:~# nmcli device status
DEVICE  TYPE      STATE         CONNECTION 
tun0    tun       connected     tun0       
wlan0   wifi      disconnected  --         
eth0    ethernet  unmanaged     --         
lo      loopback  unmanaged     --         
ppp0    unknown   unmanaged     --   
// 禁用设备 ... 活跃设备
nmcli device dis wlan0
// 开关连接
nmcli con up tun0
nmcli con down tun0
// 查看所有状态
nmcli general status

root@desktop:~# nmcli general status
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN    
connected  full          enabled  enabled  enabled  enabled 
// 查看所有连接
// nmcli con show // 查看所有连接
// nmcli con show -a // 活动的连接 --active
// nmcli con show tun0 // 详细信息

root@aaa:~# nmcli con show
NAME                UUID                                  TYPE            DEVICE 
Wired connection 1  68fccaf2-58cc-3ba1-86a4-aa5cb026c836  802-3-ethernet  eth1   
tap0                d6dda4d5-b457-4fd7-ac84-e46b82cf4850  tun             tap0   
tun0                05d6d9f7-9eab-4541-b110-7ea0dd4ac8b3  tun             tun0