侧边栏壁纸
博主头像
WinJay博主等级

博观而约取 厚积而薄发

  • 累计撰写 317 篇文章
  • 累计创建 169 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

CentOS8 网卡命令使用|CentOS8 重启网卡方法

WinJay
2021-10-12 / 0 评论 / 0 点赞 / 63 阅读 / 7,728 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2021-12-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

CentOS8 网卡命令使用|CentOS8 重启网卡方法

nmcli命令整理

概述

因为CentOS 8 弃用network.service,采用NetworkManager(NM)为网卡启用命令。当然,CentOS 8 可以安装network.service作为网卡服务,但为了适应版本,此次还是得介绍NM命令的使用,同时CentOS7也支持nmcli命令
image.png

特性

可自动补全

另一种形式的ifconfig

命令介绍

nmcli

image.pngimage.png

总命令介绍

nmcli
image.png

nmcli
enp2s0: connected to enp2s0											  #网卡名称
        "Realtek RTL8111/8168/8411"									  #网卡驱动,型号
        ethernet (r8169), 18:60:24:75:C0:CD, hw, mtu 1500			  #网卡详细信息
        ip4 default													  #默认ipv4配置
        inet4 10.191.142.7/24										  #设置的ip地址以及子网掩码
        route4 10.191.142.0/24										  #网关 
        route4 0.0.0.0/0											  #网关 
        inet6 fe80::1a60:24ff:fe75:c0cd/64							  #ipv6无设置  				
        route6 fe80::/64											  #ipv6网关无设置
        route6 ff00::/8												  #ipv6网关无设置
        
wlp3s0: disconnected
        "Intel Wireless-AC 3168NGW"
        wifi (iwlwifi), B6:36:E1:E5:A3:F2, hw, mtu 1500

p2p-dev-wlp3s0: disconnected
        "p2p-dev-wlp3s0"
        wifi-p2p, hw

virbr0: unmanaged
        "virbr0"
        bridge, 52:54:00:1D:7F:97, sw, mtu 1500

lo: unmanaged													    #本地连接默认为umanaged
        "lo"
        loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536

virbr0-nic: unmanaged
        "virbr0-nic"
        tun, 52:54:00:1D:7F:97, sw, mtu 1500

DNS configuration:													#DNS服务器设置
        servers: 10.191.142.129 202.205.160.3 219.142.50.126
        interface: enp2s0

Use "nmcli device show" to get complete information about known devices and
"nmcli connection show" to get an overview on active connection profiles.

Consult nmcli(1) and nmcli-examples(7) manual pages for complete usage details.

查看命令整理

查看网卡

查看网卡设备信息

nmcli

[见“总命令介绍” ](### 总命令介绍)

查看网卡设备详细信息

nmcli device show / nmcli device show enp2s0

[root@CentOS8 ~]# nmcli device show
GENERAL.DEVICE:                         enp2s0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         18:60:24:75:C0:CD
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     enp2s0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         10.191.142.7/24
IP4.GATEWAY:                            10.191.142.1
IP4.ROUTE[1]:                           dst = 10.191.142.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 10.191.142.1, mt = 100
IP4.DNS[1]:                             10.191.142.129
IP4.DNS[2]:                             202.205.160.3
IP4.DNS[3]:                             219.142.50.126
IP6.ADDRESS[1]:                         fe80::1a60:24ff:fe75:c0cd/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100
IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255

GENERAL.DEVICE:                         wlp3s0
GENERAL.TYPE:                           wifi
GENERAL.HWADDR:                         F2:10:DB:8E:57:EB
GENERAL.MTU:                            1500
GENERAL.STATE:                          30 (disconnected)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --

GENERAL.DEVICE:                         p2p-dev-wlp3s0
GENERAL.TYPE:                           wifi-p2p
GENERAL.HWADDR:                         (unknown)
GENERAL.MTU:                            0
GENERAL.STATE:                          30 (disconnected)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --

GENERAL.DEVICE:                         virbr0
GENERAL.TYPE:                           bridge
GENERAL.HWADDR:                         52:54:00:1D:7F:97
GENERAL.MTU:                            1500
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = ::1/128, nh = ::, mt = 256

GENERAL.DEVICE:                         virbr0-nic
GENERAL.TYPE:                           tun
GENERAL.HWADDR:                         52:54:00:1D:7F:97
GENERAL.MTU:                            1500
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                   	--

image.png
image.png

网卡相关信息查看

查看网卡设备状态
[root@CentOS8 ~]# nmcli device status 									#查看网卡设备状态
DEVICE          TYPE      STATE         CONNECTION 
enp2s0          ethernet  connected     enp2s0     
wlp3s0          wifi      disconnected  --         
p2p-dev-wlp3s0  wifi-p2p  disconnected  --         
virbr0          bridge    unmanaged     --         
lo              loopback  unmanaged     --         
virbr0-nic      tun       unmanaged     --         
[root@CentOS8 ~]# nmcli connection show									#查看网卡信息					
NAME    UUID                                  TYPE      DEVICE 
enp2s0  0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0 
[root@CentOS8 ~]# nmcli con show										#查看网卡信息
NAME    UUID                                  TYPE      DEVICE 
enp2s0  0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0 
[root@CentOS8 ~]# nmcli c show											#查看网卡信息
NAME    UUID                                  TYPE      DEVICE 
enp2s0  0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0 
[root@CentOS8 ~]# nmcli c 												#查看网卡信息
NAME    UUID                                  TYPE      DEVICE 
enp2s0  0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0

image.png

image.png

查看所有活动连接
[root@GKFZ ~]# nmcli connection show										#查看所有连接
NAME             UUID                                  TYPE      DEVICE          
em1              be94ea60-4cc5-41ab-9331-dd680beed08f  ethernet  em1             
br-00c37415a37a  0875ceb9-e285-40a6-9bed-3d09cbf3c588  bridge    br-00c37415a37a 
em2              406f7d44-ce8f-4c9d-9285-b7edc7f8ea8d  ethernet  em2             
em3              3dc981b1-67df-4201-ae7b-07e608f8f47e  ethernet  em3             
em4              4e6a1d72-617e-4d3e-b71d-23563ab1efea  ethernet  -- 

-----------------------------------------------------------------------------------------------


[root@CentOS8 ~]#  nmcli connection show --active							#查看所有活动连接
NAME    UUID                                  TYPE      DEVICE 
enp2s0  0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0 

image.png

image.png

查看网卡信息
[root@CentOS8 ~]# nmcli c show enp2s0						#查看指定网卡具体信息

[root@CentOS8 ~]# nmcli con show enp2s0						#查看指定网卡具体信息

[root@CentOS8 ~]# nmcli connection show enp2s0				#查看指定网卡具体信息
connection.id:                          enp2s0
connection.uuid:                        0326d4e0-8e71-48d0-97f9-88028b31badb
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              enp2s0
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.autoconnect-retries:         -1 (default)
connection.multi-connect:               0 (default)
connection.auth-retries:                -1
connection.timestamp:                   1634019437
connection.read-only:                   no
connection.permissions:                 --
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:                 --
connection.gateway-ping-timeout:        0
connection.metered:                     unknown
connection.lldp:                        default
connection.mdns:                        -1 (default)
connection.llmnr:                       -1 (default)
connection.wait-device-timeout:         -1
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          no
802-3-ethernet.mac-address:             --
802-3-ethernet.cloned-mac-address:      --
802-3-ethernet.generate-mac-address-mask:--
802-3-ethernet.mac-address-blacklist:   --
802-3-ethernet.mtu:                     auto
802-3-ethernet.s390-subchannels:        --
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:            --
802-3-ethernet.wake-on-lan:             default
802-3-ethernet.wake-on-lan-password:    --
ipv4.method:                            manual
ipv4.dns:                               10.191.142.129,202.205.160.3,219.142.50.126

image.png

启动、停止与重载

[root@GKFZ ~]# nmcli connection up em1 												# 启用网卡enp2s0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/17)


[root@CentOS8 ~]# nmcli connection down enp2s0										# 停用网卡enp2s0--若已配置开机自动连接,重启后可被自动激活
Connection 'enp2s0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)


[root@GKFZ ~]# nmcli device disconnect em1 											# 停用网卡enp2s0--重启后也不能自动激活
Device 'em1' successfully disconnected.

								
[root@CentOS8 ~]# nmcli connection reload enp2s0									# 重启网卡命令

image.png

添加与删除

[root@CentOS8 ~]# nmcli connection add type ethernet con-name eth-winjay ifname enp2s0 			
		#添加一个名为eth-winjay的网卡连接,_____于enp2s0;配置文件目录/etc/sysconfig/network-scripts/* ,添加配置时,自动生成网卡配置文件
Connection 'eth-winjay' (f1c6cced-502d-45da-9e04-cde129ad4237) successfully added.
[root@CentOS8 ~]# nmcli connection show
NAME        UUID                                  TYPE      DEVICE 
enp2s0      0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0 
eth-winjay  f1c6cced-502d-45da-9e04-cde129ad4237  ethernet  --
[root@CentOS8 ~]# nmcli connection up eth-winjay			#网卡切换了,原enp2s0连接已断开,启用了eth-winjay这个新连接以上网

image.png

可以看到原enp2s0连接已断开。

[root@CentOS8 ~]# nmcli connection show
NAME        UUID                                  TYPE      DEVICE 
eth-winjay  f1c6cced-502d-45da-9e04-cde129ad4237  ethernet  enp2s0 
enp2s0      0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  -- 

image.png

[root@CentOS8 ~]# nmcli connection delete eth-winjay 										#删除eth-winja这个连接
Connection 'eth-winjay' (f1c6cced-502d-45da-9e04-cde129ad4237) successfully deleted.
[root@CentOS8 ~]# nmcli connection show --active
NAME    UUID                                  TYPE      DEVICE 
enp2s0  0326d4e0-8e71-48d0-97f9-88028b31badb  ethernet  enp2s0 

image.png

配置

修改的是网卡配置文件

nmcli connection modify enp2s0 connection.autoconnect yes									#设置为自启动网卡(ONBOOT=yes)
nmcli connection modify enp2s0 ipv4.method manual ipv4.addresses 192.168.123.206/24			#设置IP地址获取方式是手动(BOOTPROTO=none)
nmcli connection modify enp2s0 ipv4.method auto												#设置IP地址获取方式是DHCP(BOOTPROTO=dhcp)
nmcli connection modify enp2s0 ipv4.addresses 10.191.142.8/24								#修改IP地址(IPADDR=10.191.142.8 PREFIX=24)
nmcli connection modify enp2s0 ipv4.gateway 10.191.142.0									#修改网关(GATEWAY=10.191.142.0)
nmcli connection modify enp2s0 +ipv4.addresses 10.191.142.6/24								#添加第二个IP地址(IPADDR1=172.16.10.10 PREFIX1=24)
nmcli connection modify enp2s0 ipv4.dns 10.191.142.9										#添加DNS(DNS1=10.191.142.9)
nmcli connection modify enp2s0 +ipv4.dns 8.8.8.8											#添加第二个DNS(DNS2=8.8.8.8)
nmcli connection modify enp2s0 -ipv4.dns 8.8.8.8											#删除第二个DNS(DNS2=8.8.8.8)

image.png

linux 关闭virbr0网卡

ifconfig virbr0 down 						#关闭virbr0网卡
brctl delbr virbr0  						#删除virbr0网卡
systemctl disable libvirtd.service  		#因为虚拟网卡是libvirtd服务创建的,而该服务在Linux中没有太大用处

image.png

image.png

0

评论区