其他分享
首页 > 其他分享> > nmcli命令详解

nmcli命令详解

作者:互联网

目录

简述

语法

nmcli [OPTIONS...] { help | general | networking | radio | connection | device | agent | monitor } [COMMAND] [ARGUMENTS...]
也就是: nmcli [选项...] 对象 [命令] [参数...]
对象 =  help | general | networking | radio | connection | device | agent | monitor

提示: 输入命令的时候请善用tab补全,少打很多单词!

比较有用的选项(OPTION)

-p | --pretty 显示的时候会更加易于人类阅读,尤其是多行显示的时候,可以分块显示
-m | --mode { tabular | multiline }  tabular显示的时候是表格式(有表头)的,不易阅,multiline是多行类似key:value形式。如果省略,大多数命令默认使用tabular。 nmcli connection show ID和nmcli device show这两命令因为结构化信息不好单行显示,如果省略,默认使用multiline
-f | --fields { field1,field2... | all | common }  默认使用common(命令公共字段)。也可以指定特定字段,比如nmcli -f GENERAL.DEVICE,IP4.ADDRESS device show 显示网卡名和ip地址(善用tab补全)。查看可用字段的方法:1-通过对应对象的show来找到字段.对于connection对象,可以使用man 5 nm-settings查看, 或者输入错误字段,让报错来提示可字段,比如输入nmcli -f xxx connection show myAP 就会报错,提示可用字段

对象

general对象(常规信息)

用途

语法

nmcli general { status | hostname | permissions | logging } [ARGUMENTS...]

networking对象(整个网络)

用途

语法

nmcli networking { on | off | connectivity } [ARGUMENTS...]

命令示例

nmcli off #禁用所有被NetworkManager托管的网络接口的网络连接(修改配置文件可以启用或禁止NetworkManager托管,可以自行百度)
nmcli networking connectivity check # 参数check表示重新检查连接状态。连接状态full表示具有完全的internet访问能力,limited表示连接到一个网络,但是internet未接入

radio对象(无线开关)

用途

语法

nmcli radio { all | wifi | wwan } [ARGUMENTS...]

命令示例

nmcli radio wifi      # 打印wifi开关状态
nmcli radio wifi off  # 表示关闭wifi

connection对象(某个连接)

用途

语法

nmcli connection { show | up | down | modify | add | edit | clone | delete | monitor | reload | load | import | export } [ARGUMENTS...]

命令示例:

nmcli connection show  # 列出网络连接的配置(存放于内存和硬盘的配置,nmcli -f active connection show 表示显示存储于内存配置, -f profile表示存放于硬盘的配置)
nmcli connection show --active  # 仅列出处于活动状态的网络配置
nmcli connection show --order name # 按配置名排序,可选排序有type、active、name、path(d-bus路径),+号和-号表示升序和降序,未指定,则默认使用升序。默认排序是:--order active:name:path
nmcli connection show uuid 38781e62-4bab-4ba8-a086-bfaece222794  # 按指定关键字显示,关键字有id,uuid、path、apath。 用途是不能使用常规的nmcli connection show <配置名> 来显示的时候,这种显示方法就可以派上用场了。


nmcli connection up prof1 # 激活一个连接。
nmcli connection down prof1 # 停用一个连接

nmcli connection delete prof1 #删除一个配置, delete [ id | uuid | path ] ID...

nmcli connection add type ethernet ifname enp5s0    # 创建一个连接。dhcp,类型是以太网,类型有以太网、wifi,adsl等,具体参考文章头部给的url
nmcli connection add ifname enp5s0 autoconnect yes type ethernet ip4 10.1.1.1/8 gw4 10.1.0.1    # 创建一个静态ip的以太网连接

device对象(网络接口)

用途

语法

nmcli device { status | show | set | connect | reapply | modify | disconnect | delete | monitor | wifi | lldp } [ARGUMENTS...]

子命令详细解释(这里不全部翻译,理解了上面connection,这里也是差不多的):
set [ifname] ifname [ autoconnect { yes | no } ] [ managed { yes | no } ]
Set device properties. connect ifname Connect the device. NetworkManager will try to find a suitable connection that will be activated. It will also consider connections that are not set to auto connect.If no compatible connection exists, a new profile with default settings will be created and activated. This differentiates nmcli connection up ifname "$DEVICE" from nmcli device connect "$DEVICE" If --wait option is not specified, the default timeout will be 90 seconds.

reapply ifname
Attempt to update device with changes to the currently active connection made since it was last applied.

modify ifname { option value | [+|-]setting.property value } ...
Modify the settings currently active on the device.
This command lets you do temporary changes to a configuration active on a particular device. The changes are not preserved in the connection profile.See nm-settings(5) for the list of available properties. Please note that some properties can't be changed on an already connected device.You can also use the aliases described in Property Aliases section. The syntax is the same as of the nmcli connection modify command.

disconnect ifname...
Disconnect a device and prevent the device from automatically activating further connections without user/manual intervention. Note that disconnecting software devices may mean that the devices will disappear.(这段翻译一下:断开设备连接,并防止设备自动激活。 断开软设备的连接可能会让这些设备消失)
If --wait option is not specified, the default timeout will be 10 seconds.

delete ifname...
Delete a device. The command removes the interface from the system. Note that this only works for software devices like bonds, bridges, teams, etc(仅能删除软设备,例如bond、桥等). Hardware devices (like Ethernet) cannot be deleted by the command.(硬设备无法删除)

wifi [ list [--rescan | auto | no | yes ] [ifname ifname] [bssid BSSID] ]
列出wifi热点,可以指定无线网卡接口名和热点BSSID。列出的热点列表不会超过30秒,必要时会重新扫描(通过--rescan控制重新扫描:取值auto yes no)

wifi connect (B)SSID [password password] [ifname ifname] [bssid BSSID] [name name] [ private { yes | no } ] [ hidden { yes | no } ]
连接到由SSID或BSSID指定的wifi网络。该命令找到匹配的连接或创建一个连接,然后在设备上激活它。如果已经存在配置文件,可以直接使用nmcli connection up <配置文件名>。private表示该连接是否对其他用户可见。hidden用于连接隐藏ssid的wifi。

wifi hotspot [ifname ifname] [con-name name] [ssid SSID] [ band { a | bg } ] [channel channel] [password password]
创建一个热点,con-name是配置名。

wifi rescan [ifname ifname] [ssid SSID...]
重新扫描wifi接入点,可以指定多个SSID。对于隐藏ssid的网络,务必要指定ssid。命令不显示接入点列表。显示接入点列表请使用nmcli device wifi list命令

命令示例:

nmcli  device wifi connect mySSID password '12345678' # 连接热点mySSID, 连接成功后,就会自动生成配置文件,以后要再连接,可以使用nmcli connectio up mySSID命令了
nmcli device wifi hotspot con-name ap001 ifname wlp3s0 ssid myAP001 password 12345678 # 创建热点。以后如果要使用,可以直接nmcli connection up ap001

常用属性缩写

标签:nmcli,wifi,命令,connection,详解,device,ifname,连接
来源: https://www.cnblogs.com/mind-water/p/12079647.html