欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nmcli命令详解

程序员文章站 2022-06-03 12:22:10
...

nmcli是redhat7或者centos7之后的命令
该命令可以完成网卡上所有的配置工作,并且可以写入配置文件,永久生效

1、查看网卡信息命令

1、查看所有网络连接

[[email protected] ~]# nmcli connection show
NAME         UUID                                  TYPE            DEVICE
eno50338560  1f77d6f7-ad3a-4061-82d5-332ed0894034  802-3-ethernet  --
eno16780032  44b5ae70-4c85-4475-b7ff-6b584b393739  802-3-ethernet  eno16780032
eno33559296  1ccf4590-335a-441f-ba17-854a78d6b613  802-3-ethernet  --
virbr0-nic   60c999b0-34ba-4dd6-85a3-f7a63cbc4350  generic         virbr0-nic
virbr0       bffd0bf1-5503-4de3-afb0-4d0fe75ea007  bridge          virbr0

2、查看活动的网络连接

[[email protected] ~]# nmcli connection show -active
NAME         UUID                                  TYPE            DEVICE
eno16777984  329a55f6-56f8-4f58-89fe-d3221d353a8f  802-3-ethernet  eno16777984
virbr0-nic   2479e6f3-7227-4199-8893-8a1ad815a10d  generic         virbr0-nic
virbr0       3705152b-654a-4740-b244-efbddd068741  bridge          virbr0

3、查看指定网卡的详细信息

[[email protected] ~]# nmcli connection show eno16777984
connection.id:                          eno16777984
connection.uuid:                        329a55f6-56f8-4f58-89fe-d3221d353a8f
connection.interface-name:              eno16777984
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.timestamp:                   1548145948
connection.read-only:                   no
省略部分输出

4、显示设备的连接状态

[[email protected] ~]# nmcli device status
DEVICE       TYPE      STATE      CONNECTION
virbr0       bridge    connected  virbr0
eno16777984  ethernet  connected  eno16777984
virbr0-nic   tap       connected  virbr0-nic
lo           loopback  unmanaged  --

5、显示所有设备网络设备详情信息

[[email protected] ~]# nmcli device show
GENERAL.DEVICE:                         virbr0
GENERAL.TYPE:                           bridge
GENERAL.HWADDR:                         52:54:00:DB:0A:63
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     virbr0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/3
IP4.ADDRESS[1]:                         192.168.122.1/24
IP4.GATEWAY:
IP6.GATEWAY:

GENERAL.DEVICE:                         eno16777984
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:50:56:B0:77:F5
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     eno16777984
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         172.16.6.16/16
IP4.GATEWAY:                            172.16.1.1
IP4.DNS[1]:                             114.114.114.114
IP6.ADDRESS[1]:                         fe80::250:56ff:feb0:77f5/64
IP6.GATEWAY:
    省略部分输出

6、显示指定网络设备的详细信息

[[email protected] ~]# nmcli device show eno16777984
GENERAL.DEVICE:                         eno16777984
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:50:56:B0:77:F5
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     eno16777984
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/0
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         172.16.6.16/16
IP4.GATEWAY:                            172.16.1.1
IP4.DNS[1]:                             114.114.114.114
IP6.ADDRESS[1]:                         fe80::250:56ff:feb0:77f5/64
IP6.GATEWAY:

2、网卡状态修改命令

1、启用网络连接

[[email protected] network-scripts]# nmcli connection up eno16780032
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5771)

2、停用网络连接-可以被自动**

[[email protected] ~]# nmcli connection down ens37
成功取消**连接 'ens37'(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/16)

3、禁用网卡放置被**

[[email protected] ~]# nmcli device disconnect ens37
成功断开设备 'ens37'。

4、删除网络连接的配置文件

[[email protected] ~]# nmcli connection delete ens37
成功删除连接 'ens37'(6c6bf2e6-111b-3f09-80bb-c177263585e6)。

5、重新加载网络配置文件

[[email protected] ~]# nmcli connection reload

3、修改网卡配置

1、设置自动启动网卡

[[email protected] network-scripts]# nmcli connection modify ens37 connection.autoconnect yes

实际修改的是网卡配置文件ONBOOT=yes

2、设置IP地址获取方式是手动或者DHCP

手动设置IPv4地址,如果原本是DHCP获取地址,改手动后,如果后面不接IP地址,可能会报错
[[email protected] network-scripts]# nmcli connection modify ens37 ipv4.method manual ipv4.addresses 172.16.10.1/16

设置IP地址为DHCP
network-scripts]# nmcli connection modify ens37 ipv4.method auto

实际修改的是网卡配置文件BOOTPROTO,BOOTPROTO=none 表示手动;BOOTPROTO=dhcp 表示dhcp

3、修改IP地址

[[email protected] network-scripts]# nmcli connection modify ens37 ipv4.addresses 172.16.10.100/16

实际修改的是网卡配置文件:
IPADDR=172.16.10.100
PREFIX=16

4、修改网关

[[email protected] network-scripts]# nmcli connection modify ens37 ipv4.gateway 172.16.1.1

实际修改的是网卡配置文件:
GATEWAY=172.16.1.1

5、添加第二个IP地址

[[email protected] network-scripts]# nmcli connection modify ens37 +ipv4.addresses 172.16.10.10/16

实际修改的是网卡配置文件:
IPADDR1=172.16.10.10
PREFIX1=16

6、添加DNS

[[email protected] network-scripts]# nmcli connection modify ens37 ipv4.dns
114.114.114.114
     
实际修改的是网卡配置文件: DNS1=114.114.114.114

7、添加第二个DNS

[[email protected] network-scripts]# nmcli connection modify ens37 +ipv4.dns 8.8.8.8

实际修改的是网卡配置文件:
DNS2=8.8.8.8

8、删除第二个DNS

[[email protected] network-scripts]# nmcli connection modify ens37 -ipv4.dns 8.8.8.8