nmcli 命令使用

nmcli 命令说明

nmcli 命令是用于控制 NetworkManager 的命令行工具

常见用法

子命令 connection

  • show:查看

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@RS1 ~]# nmcli connection show
    NAME UUID TYPE DEVICE
    ens33 92b21e34-1611-4ed2-aa3c-26716b5ccb21 ethernet ens33
    [root@RS1 ~]#
    [root@RS1 ~]# nmcli connection show ens33 | grep ipv4.*
    ipv4.method: manual
    ipv4.addresses: 192.168.20.13/24
    ipv4.gateway: 192.168.20.2
    ipv4.dns: 192.168.20.2
    ......
  • modify:修改

    1
    2
    [root@RS1 ~]# nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.20.13/24 ipv4.gateway 192.168.20.2 ipv4.dns 192.168.20.2
    [root@RS1 ~]# nmcli connection up ens33
  • edit:交互式编辑器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    [root@RS1 ~]# nmcli connection edit ens33 
    ===| nmcli 交互式连接编辑器 |===

    正在编辑已存的连接“802-3-ethernet”:“ens33”

    对于可用的命令输入“help”或“?”。
    输入“describe [<设置>.<属性>]”来获得详细的属性描述。

    您可编辑下列设置:connection, 802-3-ethernet (ethernet), 802-1x, dcb, ipv4, ipv6, tc, proxy
    nmcli> help
    ------------------------------------------------------------------------------
    ---[ Main menu ]---
    goto [<setting> | <prop>] :: go to a setting or property
    remove <setting>[.<prop>] | <prop> :: remove setting or reset property value
    set [<setting>.<prop> <value>] :: set property value
    describe [<setting>.<prop>] :: describe property
    print [all | <setting>[.<prop>]] :: print the connection
    verify [all | fix] :: verify the connection
    save [persistent|temporary] :: save the connection
    activate [<ifname>] [/<ap>|<nsp>] :: activate the connection
    back :: go one level up (back)
    help/? [<command>] :: print this help
    nmcli <conf-option> <value> :: nmcli configuration
    quit :: exit nmcli
    ------------------------------------------------------------------------------
    nmcli> print ipv4.dns
    ipv4.dns: 192.168.20.2
    nmcli> set ipv4.dns
    输入“dns”值:172.16.1.1
    nmcli> print ipv4.dns
    ipv4.dns: 192.168.20.2,172.16.1.1
    nmcli> save
    成功地更新了连接 'ens33'(92b21e34-1611-4ed2-aa3c-26716b5ccb21)。
    nmcli> quit

    [root@RS1 ~]# nmcli connection up ens33
  • 其它用法说明

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    [root@RS1 ~]# nmcli connection --help
    Usage: nmcli connection { COMMAND | help }

    COMMAND := { show | up | down | add | modify | clone | edit | delete | monitor | reload | load | import | export }

    show [--active] [--order <order spec>]
    show [--active] [id | uuid | path | apath] <ID> ...

    up [[id | uuid | path] <ID>] [ifname <ifname>] [ap <BSSID>] [passwd-file <file with passwords>]

    down [id | uuid | path | apath] <ID> ...

    add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS SLAVE_OPTIONS IP_OPTIONS [-- ([+|-]<setting>.<property> <value>)+]

    modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+

    clone [--temporary] [id | uuid | path ] <ID> <new name>

    edit [id | uuid | path] <ID>
    edit [type <new_con_type>] [con-name <new_con_name>]

    delete [id | uuid | path] <ID>

    monitor [id | uuid | path] <ID> ...

    reload

    load <filename> [ <filename>... ]

    import [--temporary] type <type> file <file to import>

    export [id | uuid | path] <ID> [<output file>]

子命令 device

使用方法与 connection 类似

  • show: 查看

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    [root@RS1 ~]# nmcli device show
    GENERAL.DEVICE: ens33
    GENERAL.TYPE: ethernet
    GENERAL.HWADDR: 00:0C:29:C8:6C:B0
    GENERAL.MTU: 1500
    GENERAL.STATE: 100 (连接的)
    GENERAL.CONNECTION: ens33
    GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/9
    WIRED-PROPERTIES.CARRIER: 开
    IP4.ADDRESS[1]: 192.168.20.13/24
    IP4.GATEWAY: 192.168.20.2
    IP4.ROUTE[1]: dst = 192.168.20.0/24, nh = 0.0.0.0, mt = 100
    IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 192.168.20.2, mt = 100
    IP4.DNS[1]: 192.168.20.2
    IP4.DNS[2]: 172.16.1.1
    IP6.ADDRESS[1]: fe80::20c:29ff:fec8:6cb0/64
    IP6.GATEWAY: --
    IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, table=255
    IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256

    GENERAL.DEVICE: lo
    GENERAL.TYPE: loopback
    GENERAL.HWADDR: 00:00:00:00:00:00
    GENERAL.MTU: 65536
    GENERAL.STATE: 10 (未托管)
    GENERAL.CONNECTION: --
    GENERAL.CON-PATH: --
    IP4.ADDRESS[1]: 127.0.0.1/8
    IP4.GATEWAY: --
    IP6.ADDRESS[1]: ::1/128
    IP6.GATEWAY: --
  • modify:修改

    1
    2
    3
    [root@RS1 ~]# nmcli device modify ens33 ipv4.dns 8.8.8.8
    [root@RS1 ~]# nmcli device show ens33 | grep IP4.DNS
    IP4.DNS[1]: 8.8.8.8
  • 更多用法说明

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    [root@RS1 ~]# nmcli device --help
    Usage: nmcli device { COMMAND | help }

    COMMAND := { status | show | set | connect | reapply | modify | disconnect | delete | monitor | wifi | lldp }

    status

    show [<ifname>]

    set [ifname] <ifname> [autoconnect yes|no] [managed yes|no]

    connect <ifname>

    reapply <ifname>

    modify <ifname> ([+|-]<setting>.<property> <value>)+

    disconnect <ifname> ...

    delete <ifname> ...

    monitor <ifname> ...

    wifi [list [ifname <ifname>] [bssid <BSSID>]]

    wifi connect <(B)SSID> [password <password>] [wep-key-type key|phrase] [ifname <ifname>]
    [bssid <BSSID>] [name <name>] [private yes|no] [hidden yes|no]

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

    wifi rescan [ifname <ifname>] [[ssid <SSID to scan>] ...]

    lldp [list [ifname <ifname>]]

nmtui 命令

nmcli 命令是用于控制 NetworkManager 的图形化工具,不多说上个图。

nmtui1
nmtui2
nmtui3
nmtui4

3个功能对应的命令分别为:nmtui-editnmtui-connectnmtui-hostname