nmcli command line network configuration

nmcli is a command-line tool for controlling NetworkManager (command-line tool for controlling NetworkManager), this command can complete all network configuration work, and directly write the configuration file, which takes effect permanently (no need to restart the network connection service).
Syntax: nmcli [options] object {parameter | help}
The object and parameter here can use full names or abbreviations, at least one letter can be used. Commonly used objects are device (network interface object) and connection (network connection object)

1. View network interface information

nmcli         ##查看ip信息
nmcli device status   ##所有接口的简略信息
nmcli device show      ##所有接口的详细信息,device可以缩写为d,de,dev...
nmcli device show ens160    ##指定的ens160网络接口的详细信息

2. View network connection information

nmcli connection show   ##所有连接的简略信息,connection可以缩写为c,co,con...
nmcli connection show --active  ##显示激活的连接
nmcli connection show inteface-name   ##某个接口的详细连接信息

Please understand: the network connection information of the ethernet type is stored in the /etc/sysconfig/network-scripts directory, which is also the network connection configuration directory

[root@hollowman ~]# cd /etc/sysconfig/network-scripts/ 
[root@hollowman network-scripts]# ls  
ifcfg-ens160 

You can see that there is a network connection configuration file with "ifcif-" + "network connection name ens160" in this directory . This is the network connection name that is generated by the system by default and is the same as the network interface name. We can create other network connections later. Similarly, different network connection configuration files will be generated in this directory accordingly.
Check the network connection:

[root@hollowman network-scripts]# nmcli c show
NAME      UUID                                  TYPE      DEVICE 
ens160  12c0daf8-0019-47fb-b763-64e5cbfda958  ethernet  ens160 
virbr0    0a8c94be-61e4-4b82-84d1-afebef9a2aab  bridge    virbr0 

Can be found in two network connections, which ens160 network connection profile that is above the ifcfg-ens160 , and virbr0 is a virtual bridge connection, do not ignore temporarily.

3. Create a network connection

nmcli connection add con-name ens160-2 type ethernet ifname ens160 ipv4.method manual ipv4.addresses 192.168.100.20/24 ipv4.gateway 192.168.100.1 ipv4.dns 192.168.100.1

con-name: connection name;
ifname: network interface name;
type: usually ethernet;
ipv4.address: ip address/24;
ipv4.gateway: gateway;
ipv4.dns: domain name;
ipv4.addresses: specify the address of the network card ipv4;
ipv4.gateway: Specify the ipv4 gateway of the network card.
In fact, it doesn't matter if you can't remember, it's clear if you use double Tab more.
Pay attention to two important parameters:
autoconnect yes : corresponds to ONBOOT=yes in the configuration file, the default is yes
autoconnect no : corresponds to ONBOOT=no in the configuration file
ipv4.method auto : corresponds to BOOTPROTO =dhcp in the configuration file, the default is auto
ipv4 .method manual : corresponding to BOOTPROTO =none in the configuration file

[root@hollowman ~]# nmcli connection add con-name ens160-2 type ethernet ifname ens160  ipv4.addresses 192.168.100.20/24 ipv4.gateway 192.168.100.1 ipv4.dns 192.168.100.1
Connection 'ens160-2' (12c0daf8-0019-47fb-b763-64e5cbfda958) successfully added.

After the Q connection is created , there will be an ifcfg-ens160-2 configuration file under the network connection configuration directory /etc/sysconfig/network-scripts/ , but because he uses the ens160 network interface, and the system already has one by default ens160 is connected and occupies this network interface, so it cannot take effect temporarily.

[root@hollowman network-scripts]# nmcli c show
NAME      UUID                                  TYPE      DEVICE 
ens160  12c0daf8-0019-47fb-b763-64e5cbfda958  ethernet  ens160 
virbr0    0a8c94be-61e4-4b82-84d1-afebef9a2aab  bridge    virbr0 
ens160-2    307da4b0-6327-4b1d-ad63-06103994c145  ethernet  --    

Above, ens160-2 of DEVICEis - that is not in force, how it take effect? That's the next step.

4. Close the connection and activate the connection

Close link

nmcli connection down 网络连接名
nmcli device disconnect 网络接口名

Activate connection

nmcli connection up 网络连接名
nmcli device connect 网络接口名

What is the difference between these two methods?
After closing one of the network connections through the connection object, the other network connections will be automatically activated, unless the other network connections are set to manual connections (method auto, that is, BOOTPROTO=dhcp) or there are no other network connections.
For example: nmcli c down ens160afterwards, the ens160 connection will be closed, and the ens160-2 will be automatically activated.
However, if a network interface is closed through the device object, the network interface (including the activated network connection) will be directly closed, so other network connections will not be Automatically activate.
It is recommended nmcli device disconnect 网络接口名because it will be more controllable.

5. Modify the network connection configuration

Set the method of obtaining the network connection to manual (if BOOTTROTO=dhcp, then modify it to BOOTPROTO=none)

nmcli connection modify ens160-2 ipv4.method manual

After setting, go to close ens160 through nmcli c down ens160, then ens160-2 will not be activated.
Other parameter settings are the same as above. If the configuration is not set, then create a new one. If it already exists but the parameters are different, then modify it, otherwise it will not be modified.

Delete DNS
nmcli connection modify ens160-2 -ipv4.dns 192.168.100.1 (note the minus sign here, the ip behind must be the same as the ip address of the original configuration dns)

6. Heavy network connection

nmcli connection reload 

Reload all network connections, but will not take effect immediately, you need to restart the NetworkManager service to take effect

nmcli connection reload ens160-2 

Reload ens160-2 this network connection The
reload network connection is generally used after manually modifying the network connection configuration file (such as ifcfg-ens160-2), and it also needs systemctl restart NetworkManagerto take effect. It takes one more step than closing and activating the network connection, and it can be considered one more method to use.

7. Delete network connection

nmcli connection delete 网络连接名

Guess you like

Origin blog.csdn.net/ymz641/article/details/111465599