• Centos7网络管理NetworkManager中nmcli工具使用


    nmcli是NetWorkManager软件名的缩写,也是该软件下的一个命令,专门用于管理Linux服务器的网络配置。

    在Centos7中已经出现该命令,在Centos8中,官方放弃了network服务,主推Nmcli服务,以下是Nmcli的用法介绍。

    查看NetworkManager服务的状态,Linux服务器向我们传递了两个信息:该服务默认开机自启动,默认在激活

    [root@x160 ~]# systemctl status NetworkManager
    ● NetworkManager.service - Network Manager
    Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2020-07-24 16:19:26 CST; 12min ago
    Docs: man:NetworkManager(8)
    Main PID: 766 (NetworkManager)
    Tasks: 4
    Memory: 10.6M
    CGroup: /system.slice/NetworkManager.service
    ├─ 766 /usr/sbin/NetworkManager --no-daemon
    └─2087 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/...

    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7514] dhcp4 (ens....1
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7514] dhcp4 (ens...00
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7514] dhcp4 (ens...1'
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7514] dhcp4 (ens...1'
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7514] dhcp4 (ens...nd
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7521] device (en...')
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7524] device (en...')
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7525] device (en...')
    Jul 24 16:27:42 x160 NetworkManager[766]: <info> [1595579262.7570] device (en...d.


    #查看设备状态 [root@x160 ~]# nmcli device status DEVICE TYPE STATE CONNECTION ens33 ethernet connected ens33 virbr0 bridge connected virbr0 docker0 bridge unmanaged -- lo loopback unmanaged -- virbr0-nic tun unmanaged -- #查看网卡连接状态 [root@x160 ~]# nmcli connection show NAME UUID TYPE DEVICE ens33 c96bc909-188e-ec64-3a96-6a90982b08ad ethernet ens33 virbr0 d4be0d7e-3c2f-4559-94bb-a4b7f41b7ba2 bridge virbr0 ens33 52dbb266-a3c8-ddb4-c843-d63dde6075d5 ethernet --
    #显示所有连接
    [root@x160 ~]# nmcli connection 
    NAME                UUID                                  TYPE      DEVICE 
    ens33               c96bc909-188e-ec64-3a96-6a90982b08ad  ethernet  ens33  
    Wired connection 1  0e9ad4c8-9abb-3d79-96b8-9451abf64bc0  ethernet  ens36  
    virbr0              29ef4522-ef5e-4450-b47c-3e1cdda2f61f  bridge    virbr0 
    ens33               52dbb266-a3c8-ddb4-c843-d63dde6075d5  ethernet  --     
    eth1                226b86d6-2ad1-4155-bae3-37344e4cdd5f  ethernet  --     
    
    #这条命令将 NetworkManager 的所有状态都打印出来
    [root@x160 ~]# nmcli general status
    STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN    
    connected  full          enabled  enabled  enabled  enabled 
    [root@x160 ~]# 
    
    #仅显示当前活动的连接
    [root@x160 ~]# nmcli connection show -active
    NAME                UUID                                  TYPE      DEVICE 
    ens33               c96bc909-188e-ec64-3a96-6a90982b08ad  ethernet  ens33  
    Wired connection 1  0e9ad4c8-9abb-3d79-96b8-9451abf64bc0  ethernet  ens36  
    virbr0              29ef4522-ef5e-4450-b47c-3e1cdda2f61f  bridge    virbr0 
    [root@x160 ~]# 
    
    #列出 NetworkManager 识别出的设备列表及他们的状态
    [root@x160 ~]# nmcli device status
    DEVICE      TYPE      STATE      CONNECTION         
    ens33       ethernet  connected  ens33              
    ens36       ethernet  connected  Wired connection 1 
    virbr0      bridge    connected  virbr0             
    docker0     bridge    unmanaged  --                 
    lo          loopback  unmanaged  --                 
    virbr0-nic  tun       unmanaged  --                 
    [root@x160 ~]# 
    
    #停止某个接口
    [root@x160 ~]# nmcli device disconnect ens36
    Device 'ens36' successfully disconnected.
    
    
    #启动接口
    [root@x160 ~]# nmcli device connect ens36
    Device 'ens36' successfully activated with '0e9ad4c8-9abb-3d79-96b8-9451abf64bc0'.
    

    device叫网络接口,是物理设备;connection是连接,偏重于逻辑设置。  

    多个connection可以应用到同一个device,但同一时间只能启用其中一个connection。这样的好处是针对一个网络接口,我们可以设置多个网络连接,比如静态IP和动态IP,再根据需要up相应connection  

    修改IP地址等属性:

    # nmcli connection modify IFACE [+|-]setting.propertyvalue

    修改配置文件执行生效:systemctl restart network 或 nmcli con reload

    nmcli命令生效:nmclicon down eth0 ; nmclicon up eth0

    个人实践

    centos7与之前的操作系统不同之处,

    一、centos7的网络管理方式使用NetworkManager工具,传统方式是network服务;

    二、centos7网卡的命名规则使用en开头,ens代表热插拔网卡类型,enp代表pci接口的网卡类型,eno代表on-board板载网卡类型;

    NetworkManager网络管理2个重要知识点:

    device    设备

    connection  网络

    设备即网络接口,连接是对网络接口的配置。一个网络接口可有多个连接配置,但同时只有一个连接配置生效。

    NetworkManager(动态网络管理工具)提供的工具:

    nmcli    nm-command-line    命令行管理工具

    nmtui    nm-text-user-interface  文本管理工具

    nm-connect-editor          图形化管理工具

      

    [root@x112 ~]# nmcli device 
    DEVICE  TYPE      STATE      CONNECTION 
    eth0    ethernet  connected  eth0       
    lo      loopback  unmanaged  --         
    
    [root@x112 ~]# nmcli connection
    NAME  UUID                                  TYPE            DEVICE 
    eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  802-3-ethernet  eth0   
    
    [root@x112 ~]# nmcli connection add con-name eth2 ifname eth0 autoconnect yes ipv4.addresses 192.168.0.110/24 gw4 192.168.0.1 type ethernet
    Connection 'eth2' (4634ec80-1f55-43b4-9f7a-32296c517e8c) successfully added.
    
    [root@x112 ~]# nmcli connection 
    NAME  UUID                                  TYPE            DEVICE 
    eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  802-3-ethernet  eth0   
    eth2  4634ec80-1f55-43b4-9f7a-32296c517e8c  802-3-ethernet  --   

    激活连接 [root@x112 ~]# nmcli connection up eth2 [root@x112 ~]# nmcli connection NAME UUID TYPE DEVICE eth2 4634ec80-1f55-43b4-9f7a-32296c517e8c 802-3-ethernet eth0 eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet -- [root@x112 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4b:06:6b brd ff:ff:ff:ff:ff:ff inet 192.168.0.101/24 brd 192.168.0.255 scope global dynamic eth0 valid_lft 7085sec preferred_lft 7085sec inet 192.168.0.110/24 brd 192.168.0.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet6 fe80::10b9:f353:3cd:f91/64 scope link valid_lft forever preferred_lft forever [root@x112 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp IPADDR=192.168.0.110 PREFIX=24 GATEWAY=192.168.0.1 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth2 DEVICE=eth0 ONBOOT=yes [root@x112 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none IPADDR=192.168.0.112 PREFIX=24 BROADCAST=192.168.0.255 DNS1=8.8.8.8 GATEWAY=192.168.0.1 ONBOOT=yes NAME=eth0 修改配置文件执行生效,运行nmcli connection reload;然后运行nmcli connection down eth0;nmcli connection up eth0 [root@x112 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth2 [root@x112 ~]# nmcli connection reload [root@x112 ~]# nmcli connection down eth2;nmcli connection up eth2 Connection 'eth2' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4) [root@x112 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4b:06:6b brd ff:ff:ff:ff:ff:ff inet 192.168.0.110/24 brd 192.168.0.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::10b9:f353:3cd:f91/64 scope link valid_lft forever preferred_lft forever [root@x112 ~]# nmcli connection NAME UUID TYPE DEVICE eth2 4634ec80-1f55-43b4-9f7a-32296c517e8c 802-3-ethernet eth0 eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet --

     

    为了方便操作,一般都需要安装命令补全功能软件,方便使用tab补全命令
    [root@x112 ~]#yum -y install bash-com*
    bash-completion-2.1-8.el7.noarch
    [root@x112 ~]# 
    

      

     

     

      

  • 相关阅读:
    SQL逻辑查询语句执行顺序
    数据库索引的实现原理
    JAVAMail邮件服务器
    TCP/IP基础
    Java IO流学习总结
    [Lydsy1806月赛] 超速摄像头
    CodeForces
    SCU
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/xiaofeng666/p/13372699.html
Copyright © 2020-2023  润新知