[Linux series] nmcli detailed explanation

background

         When doing LVS experiments some time ago, network planning and configuration are very critical. In the past, ip was configured and modified through configuration files in Linux systems. Today we will talk about nmcli commands to configure the network.

         At present, all Linux system configuration information such as network cards have been implemented to manage the Network-Manager service. nmcli is NetworkManager's command-line tool. nm stands for NetworkManager, and cli stands for Command-Line command line.

查看运行状态:
systemctl status NetworkManager

启动:
systemctl start NetworkManager

重启:
systemctl restart NetworkManager

关闭:
systemctl stop NetworkManager

查看是否开机启动:
systemctl is-enabled NetworkManager

开机启动:
systemctl enable NetworkManager

禁止开机启动:
systemctl disable NetworkManager

注意:NetworkManager 中开头的 N 和中间的 M 必须大写。

information query

 1. Show all connections

root@Route:~# nmcli connection  show 
NAME      UUID                                  TYPE      DEVICE 
ethernet  8fb866df-f48f-4f67-af40-e23e0b6dcd09  ethernet  ens38  
ens33     c0f36c4d-9f4a-439d-80e3-eb38df216bb4  ethernet  ens33  

 2. Show available connections

root@Route:~# nmcli connection  show  --active
NAME      UUID                                  TYPE      DEVICE 
ethernet  8fb866df-f48f-4f67-af40-e23e0b6dcd09  ethernet  ens38  
ens33     c0f36c4d-9f4a-439d-80e3-eb38df216bb4  ethernet  ens33 

3. Display device (network card device) detailed information

##deviece的连接状态
root@Route:~# nmcli device status 
DEVICE  TYPE      STATE         CONNECTION 
ens33   ethernet  connected     ens33      
ens38   ethernet  disconnected  --         
lo      loopback  unmanaged     -- 
root@Route:~# nmcli device show
GENERAL.DEVICE:                         ens33
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:C7:1E:A0
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     ens33
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/3
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         10.10.10.129/24
IP4.GATEWAY:                            10.10.10.2
IP4.ROUTE[1]:                           dst = 10.10.10.0/24, nh = 0.0.0.0, mt = 101
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 10.10.10.2, mt = 20101
IP4.ROUTE[3]:                           dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000
IP4.DNS[1]:                             10.10.10.2
IP4.DOMAIN[1]:                          localdomain
IP6.ADDRESS[1]:                         fe80::3c4:6c33:343a:c3bd/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 1024

GENERAL.DEVICE:                         ens38
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:C7:1E:AA
GENERAL.MTU:                            1500
GENERAL.STATE:                          30 (disconnected)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
WIRED-PROPERTIES.CARRIER:               on
IP4.GATEWAY:                            --
IP6.GATEWAY:                            --

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
lines 19-42/42 (END)

Network Configuration

Configuration process:

        1. Determine the available devices in the system;
        2. Confirm whether there is an available connection on the device;
        3. Configure and modify the ip of the connection on the device;
      confirm device----->create connection----->configure and Revise

1. Create a valid connection

root@Route:~# nmcli connection add ifname ens38 con-name ens38_1 type ethernet 
Connection 'ens38_1' (ee550e98-2156-4dd0-805c-d8ab38532f2e) successfully added.

root@Route:~# nmcli connection show 
NAME      UUID                                  TYPE      DEVICE 
ens38_1   ee550e98-2156-4dd0-805c-d8ab38532f2e  ethernet  ens38  
ens33     c0f36c4d-9f4a-439d-80e3-eb38df216bb4  ethernet  ens33  
ethernet  8fb866df-f48f-4f67-af40-e23e0b6dcd09  ethernet  --     
root@Route:~# 

    con-name 指定连接名字,没有特殊要求,自定义
    ifname 指定网卡设被名,既就是次配置所生效的网卡
    type   指定连接的类型,ethernet为以太网(默认)


 2. Configure network information

root@Route:~# nmcli connection modify ens38_1 ipv4.addresses 172.16.93.140/24 ipv4.getway 172.16.93.3 ipv4.dns  172.16.93.2

    ipv4.addresses 指定IPv4地址
    ipv4.getway    指定网关
    ipv4.dns       指定dns

 3. Starting and closing the connection

root@Route:~# nmcli connection up ens38_1                                                                                          
root@Route:~# nmcli connection down  ens38_1

4. Delete network

root@Route:~#  nmcli connection delete ens38_1

 modify network

 1. Modify the IP address and gateway

root@Route:~# nmcli connection modify ens38_1 ipv4.addresses 200.100.100.100/24 ipv4.getway 200.100.100.1     

2. Add and delete dns

root@Route:~# #nmcli connection modify ens38_1 +ipv4.dns 114.114.114.114 
                                                           
root@Route:~# nmcli connection modify ens38_1  -ipv4.dns 114.114.114.114                                                             

 3. Add and delete ip

root@Route:~# nmcli connection modify ens38_1 +ipv4.addresses 10.10.10.10/24 
                                                      
root@Route:~# nmcli connection modify ens38_1 -ipv4.addresses 10.10.10.10/24                                                       

4. Reactivate after modifying the connection configuration

root@Route:~# nmcli connecti reload

Guess you like

Origin blog.csdn.net/qq_43714097/article/details/126848311