Nmcli 命令全套

Nmcli 命令全套

[root@localhost network-scripts]# nmcli  --help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -o[verview]                                    overview mode (hide default values)
  -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
  -g[et-values] <field1,field2,...>|all|common   shortcut for -m tabular -t -f
  -e[scape] yes|no                               escape columns separators in values
  -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

device 和 connection的区别?

device叫网络接口,是物理设备
connection是连接,是逻辑设置
同一个device可以有多个connection,但同一时间只能启用其中一个connection,
这样对一个网络接口,可以设置多个网络连接,比如静态IP和动态IP,
再根据需要启动相应的connection

dvice 对象

[root@localhost ~]# nmcli  d --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>]
      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>]]

查看所有设备的状态

[root@localhost network-scripts]# nmcli  d
DEVICE      TYPE      STATE         CONNECTION
enp125s0f1  ethernet  connected     enp125s0f1
enp125s0f2  ethernet  connected     enp125s0f2
enp129s0f0  ethernet  connected     enp129s0f0
enp129s0f1  ethernet  disconnected  --
enp125s0f0  ethernet  unavailable   --
enp125s0f3  ethernet  unavailable   --
enp131s0f0  ethernet  unavailable   --
enp131s0f1  ethernet  unavailable   --
enp189s0f0  ethernet  unavailable   --
enp189s0f1  ethernet  unavailable   --
enp189s0f2  ethernet  unavailable   --
enp189s0f3  ethernet  unavailable   --
enp1s0f0    ethernet  unavailable   --
enp1s0f1    ethernet  unavailable   --
lo          loopback  unmanaged     --

查看所有设备的详细信息

[root@localhost ~]# nmcli d show
GENERAL.DEVICE:                         enp125s0f1
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         60:D7:55:3B:84:5B
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     enp125s0f1
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         172.16.12.107/17
IP4.ADDRESS[2]:                         172.16.11.201/17
IP4.GATEWAY:                            172.16.1.10
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 172.16.1.10, mt = 0
IP4.ROUTE[2]:                           dst = 172.16.0.0/17, nh = 0.0.0.0, mt = 100
IP6.ADDRESS[1]:                         2004::7891:cc43:b1ad:7a5f/64
IP6.ADDRESS[2]:                         2003::1c:7017/128
IP6.ADDRESS[3]:                         fe80::d47d:3889:46db:111a/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]:                           dst = 2014::/24, nh = ::, mt = 100
IP6.ROUTE[3]:                           dst = 2004::/64, nh = ::, mt = 100
IP6.ROUTE[4]:                           dst = 2003::/96, nh = ::, mt = 100
IP6.ROUTE[5]:                           dst = 2003::1c:7017/128, nh = ::, mt = 100
IP6.ROUTE[6]:                           dst = ff00::/8, nh = ::, mt = 256, table=255
IP6.DNS[1]:                             2003::1

对设备的激活和断开

[root@localhost network-scripts]# nmcli d disconnect   enp125s0f2
[root@localhost network-scripts]# nmcli d connect  enp125s0f2
# 等价 
[root@localhost ~]# nmcli  d reapply  enp125s0f2
# 立即生效

connection 对象

[root@localhost ~]# nmcli  c  --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>]

重新加载网络连接的配置

[root@localhost network-scripts]# nmcli c reload    # 重载所有ifcfg或route到connection
# 不会立即生效
# 重载指定ifcfg或route到connection(不会立即生效)
nmcli c load /etc/sysconfig/network-scripts/ifcfg-ethX
nmcli c load /etc/sysconfig/network-scripts/route-eth

查看 一个连接的详细信息【默认所有字段】

[root@localhost ~]# nmcli c show enp129s0f0
[root@localhost ~]# nmcli connection show --active # 显示所有激活的连接
# 只显示 该 连接的 以 ipv4 开头的字段信息
[root@localhost ~]# nmcli  -f ipv4 c  show  enp125s0f2
# 或者
[root@localhost ~]# nmcli  -f IP4  c  show  eth125f2-3
    IP4.ADDRESS[1]:                         124.124.124.124/24
    IP4.ADDRESS[2]:                         172.16.13.150/17
    IP4.GATEWAY:                            172.16.1.10
    IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 172.16.1.10, mt = 114
    IP4.ROUTE[2]:                           dst = 124.124.124.0/24, nh = 0.0.0.0, mt = 114
    IP4.ROUTE[3]:                           dst = 172.16.0.0/17, nh = 0.0.0.0, mt = 114
    IP4.DNS[1]:                             172.16.1.10
    IP4.DOMAIN[1]:                          sit.com
# 记忆方法: -f 【field】 代表字段 , 过滤 其他字段 信息同理.

对连接的 启用和 关闭

[root@localhost ~]# nmcli  c up enp125s0f2        # 相当于ifup
[root@localhost ~]# nmcli  c down  enp125s0f2     # 相当于ifdown
# 立即生效

修改连接的名字

[root@localhost ~]# nmcli con mod ens33 connection.id ens33con

添加 一个 连接

[root@localhost ~]# nmcli con add con-name eth125s1 type ethernet  autoconnect yes ifname enp125s0f1
# 说明
	    ifname是物理设备,网络接口
        type的类型设置为ethernet:以太网
        autoconnect:有网络时自动连接
# 注意: 当该设备 不存在 激活连接时, 则自动激活 当前所新创建连接, 否则 为 down。 设备唯一性原则。
# 添加连接的同时 设置 IP 地址, 
[root@localhost ~]# nmcli connection add type ethernet con-name ens ifname ens3 ip4 192.168.1.9/24 gw4 192.168.1.1

删除一个连接

[root@localhost ~]# nmcli connection delete  enp125s0f1   # 类似于 ifdown 并删除 ifcfg

添加一个 IP 地址

[root@localhost ~]# nmcli connection modify  eth125f2-3 +ipv4.addresses 126.126.126.126/24
[root@localhost ~]# nmcli c up eth125f2-3  
# 注意: 添加完 IP 信息 , 需重启 该连接, 变化信息才可生效.
[root@localhost ~]# nmcli d reaply eth125f2-3 

删除一个 IP 地址

[root@localhost ~]# nmcli connection  modify eth125f2-3  -ipv4.addresses 125.125.125.125/24
[root@localhost ~]# nmcli c up eth125f2-3
# 注意: 删除 完 IP 信息 , 需重启 该连接, 变化信息才可生效.

👉 修改 连接其他字段信息 与 修改 IP 信息同理

重置 所有 IP 地址 信息

# 不使用 + - 号,表示是修改
[root@localhost ~]# nmcli connection  modify  eth125f2-3 ipv4.addresses  '125.125.125.125/24, 127.127.127.127/24'
[root@localhost ~]# nmcli  c up eth125f2-3
# 注意: 重置 完 IP 信息 , 需重启 该连接, 变化信息才可生效.

# 创建connection,配置静态 IPV4 ip(等同于配置ifcfg,其中BOOTPROTO=none,并ifup启动)
nmcli c add type ethernet con-name ethX ifname ethX ipv4.addr 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.method manual
# 或者添加 IPV6 地址
nmcli c add type ethernet con-name ethX ifname ethX ipv6.addresses "2001:4f8:212:1c60:20c:29ff:fe2d:eaa0/64" ipv6.method manual autoconnect yes
# 或者同时添加
nmcli c add type ethernet con-name ethX ifname ethX ipv4.addr 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.method manual ipv6.addresses "2001:4f8:212:1c60:20c:29ff:fe2d:eaa0/64" ipv6.method manual autoconnect yes
# 创建connection,配置动态ip(等同于配置ifcfg,其中BOOTPROTO=dhcp,并ifup启动)
nmcli c add type ethernet con-name ethX ifname ethX ipv4.method auto

添加/删除 DNS 与 IP 同理

[root@localhost ~]# nmcli connection  modify  eth125f2-3 +ipv4.dns 114.114.114.114   # 添加 DNS 
[root@localhost ~]# nmcli  c up eth125f2-3
[root@localhost ~]# nmcli connection  modify  eth125f2-3 ipv4.dns '114.114.114.114,8.8.8.8,112.112.112.112'
[root@localhost ~]# nmcli  c up eth125f2-3
[root@localhost ~]# nmcli connection  modify  eth125f2-3 -ipv4.dns 8.8.8.8           # 删除 DNS 
[root@localhost ~]# nmcli  c up eth125f2-3

修改 网关

[root@localhost ~]# nmcli  connection  modify  eth125f2-3 +ipv4.gateway  125.125.125.1
[root@localhost ~]# nmcli  c up eth125f2-3
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/119)
[root@localhost ~]# nmcli -f IP4 connection  show eth125f2-3  
# 注意: 网关 只 可有一个, 添加不了多个

设置主机名

#查询当前主机名
nmcli general hostname
#修改主机名
nmcli general hostname new-hostname
#重启hostname(主机名)服务
systemctl restart systemd-hostnamed

networking 对象

网络状态的打开和关闭

[root@localhost network-scripts]# nmcli networking off
[root@localhost network-scripts]# nmcli networking on

其他

# 关闭无线网络(NM默认启用无线网络)
nmcli r all off
# 查看NM纳管状态
nmcli n
# 开启NM纳管
nmcli n on
# 关闭NM纳管(谨慎执行)
nmcli n off
# 监听事件
nmcli m
# 查看NM本身状态
nmcli
# 检测NM是否在线可用
nm-online

参考网址

centos8上-----nmcli 网络管理

参考 02

Open Euler 系统网络使用教程 --- nmcli

​ Unix 命令一般都带有参数, 而参数的传递可以直接给,也可间接地转化,例如自动将标准输入作为参数。

lspci -vvv | grep -i ethernet

ssd

posted @ 2021-01-06 22:19  梭梭666  阅读(9)  评论(0编辑  收藏  举报
返回顶部