A very useful network card configuration command---nmcli command detailed explanation

nmcli is a command after redhat7 or Centos7. When using nmcli, you can directly write the configuration you modify into the configuration file, and it will take effect permanently.

nmcli is a command-line tool of NetworkManager, and it is also a relatively powerful network management command-line tool for Centos (currently applicable to most linux distributions).

NetworkManager can be used for the following types of connections: Ethernet, VLANS, Bridges, Bonds, Teams, Wi-Fi, mobile boradband, and IP-over-InfiniBand. For these network types, NetworkManager can configure their network aliases, IP addresses, DHCP, DNS, VPN connections and many other special parameters.

The nmcli command uses

1. Check whether NetworkManager takes over the network settings

nmcli network  # 返回结果为enabled则表示已经接管
也可以写成下面这样
nmcli n

Set NetworkManager network takeover:

nmcli n on

Cancel takeover: 

nmcli n off

2. Check the network connection status

nmcli network connectivity
或者
nmcli n c
Five output results of network connection status:
full: normal network connection
limited: connected to the network, but unable to access the Internet
portal: connected to the network, but requires authentication and login to access the
Internet none: not connected to the
network unknown: unable to identify the network

 3. Display the status of NetworkManager

nmcli general status
或者
nmcli g s

显示结果:
STATE   CONNECTIVITY  WIFI-HW  WIFI    WWAN-HW  WWAN   
已连接       完全       已启用   已启用    已启用   已启用

STATE: whether the network is connected

CONNECTIVITY: Whether NetworkManager takes over network settings

WIFI-HW: indicates whether the hardware of WIFI is enabled

WIFI: indicates whether the WIFI software is enabled

WWAH-HW: Indicates WWAN hardware

WWAN: software representing WWAN

 4. Display all connected network information

nmcli connection show
或者
nmcli c s

显示结果:
NAME  UUID                                  TYPE      DEVICE 
eth0  dc756a95-c8f1-4c57-b340-9ef54d6e74e1  ethernet  eth0
nmcli connection show --active  # 只显示当前连接的网络
# 指定连接网络,如果指定网络已连接,该命令则表示重启网络
nmcli connection up eth0

# 关闭连接
nmcli connection down eth0

5. Display the devices identified by NetworkManager and their status

nmcli device status
或者
nmcli d s

显示结果:
DEVICE             TYPE      STATE                       CONNECTION 
wlp0s20f3          wifi      已连接                         白日梦     
docker0            bridge    连接(外部)                    docker0    
virbr0             bridge    连接(外部)                    virbr0     
vnet0              tun       连接(外部)                    vnet0      
br0                bridge    连接中(正在获取 IP 配置)        br0        
p2p-dev-wlp0s20f3  wifi-p2p  已断开                          --         
enp8s0             ethernet  不可用                          --         
lo                 loopback  未托管                          -- 

6. Delete a network card connection

nmcli connection delete eth0
或者:
nmcli c d eth0

7. Add a network card connection

# 创建一个动态ip的以太网连接;连接有以太网,WIFI,adsl等
nmcli connection add ethernet ifname eth0

# 创建一个静态ip的以太网连接
nmcli connection add ifname enp5s0 autoconnect yes type ethernet ipv4.addresses ip地址/子网掩码 ipv4.gateway 网关

8. Restart and load the configuration file of the network connection

nmcli connection reload

9. Modify the network card configuration

# 修改指定网卡的ip地址和子网掩码
nmcli connection modify 网卡名 ipv4.addresses ip地址/子网掩码

# 修改指定网卡的ip地址和子网掩码以及网关
nmcli connection modify 网卡名 ipv4.addresses ip地址/子网掩码 ipv4.gateway 网关

# 修改指定网卡的ip地址和子网掩码、网关以及DNS
nmcli connection modify 网卡名 ipv4.addresses ip地址/子网掩码 ipv4.gateway 网关 ipv4.dns dns地址

# 添加第二个ip
nmcli connection modify eth0 +ipv4.addresses ip地址/子网掩码
# 删除第二个ip
nmcli connection modify eth0 -ipv4.addresses ip地址/子网掩码

# 添加第二个DNS
nmcli connection modify eth0 +ipv4.dns dns地址
# 删除第二个DNS
nmcli connection modify eth0 +ipv4.dns dns地址

# 修改为静态配置,默认是 auto
nmcli c m eth0 ipv4.method manual

# 将 IPv6 禁用
nmcli c m ens33 ipv6.method disabled

# 开机启动
nmcli c m ens33 connection.autoconnect yes

 10. Create a bridge and establish a connection

# 创建网桥
nmcli connection add type bridge con-name br0 ifname br0 autoconnect yes

#查卡br0的状态
nmcli device status

DEVICE             TYPE           STATE           CONNECTION     
br0               bridge   连接中(正在获取 IP 配置)    br0

# 将我们本地的有线网卡enp8s0连接到br0
nmcli connection add type bridge-slave ifname enp8s0  master br0

# 启动br0
nmcli connection up br0

ipv4.method manual: configure a static ip address
ipv4.method auto: dynamically obtain an ip address from the DHCP address pool, if no IP is obtained, the configured static address will be activated
ipv4.address 192.168.1.10/24: configure a static ip address
ipv4.dns 114.114.114.114: Configure DNS
ipv4.dns-search test.com: Modify /etc/resolv.conf to use this domain in the search command
autoconnect yes: Automatically activate this network connection when the system starts

When we modify the configuration of the network card, we must remember to execute "nmcli connection up network card name", otherwise the configuration will not take effect immediately.

Guess you like

Origin blog.csdn.net/qydjss/article/details/127693829
Recommended