NetworkManager 管理网卡设备

linux将物理网络设备抽象为interface,屏蔽了物理网络设备的多样性,通过驱动程序检测固件信息在linux内核中登记,在数据包收发时通过interface进行交互。网络管理工具包括network服务,NetworkManager服务,ipconfig服务、netstat服务等。在REHL8/centos8中弃用了之前版本中网络管理的network服务,使用NetworkManager服务替代。

NetworkManager主要管理两类对象:connection(网卡连接配置)和device(网卡设备),device对应的是interface,而connection是供设备使用的配置,二者是多对一的关系,但同时只有一个connection对象生效,其中配置文件依然默认存放在/etc/sysconfig/network-scripts/下,配置方式有两种:

  1. 通过nmcli connection add命令配置,会自动生成配置文件;
  2. 手动配置ifcfg文件,通过nmcli connection reload来使其生效;

1 nmcli命令参数

# nmcli --help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }                              
OPTIONS
  -a, --ask                                ask for missing parameters         
  -c, --colors auto|yes|no                 whether to use colors in output    
  -e, --escape yes|no                      escape columns separators in values         
  -f, --fields <field,...>|all|common      specify fields to output           
  -g, --get-values <field,...>|all|common  shortcut for -m tabular -t -f      
  -h, --help                               print this help                    
  -m, --mode tabular|multiline             output mode                        
  -o, --overview                           overview mode                      
  -p, --pretty                             pretty output                      
  -s, --show-secrets                       allow displaying passwords         
  -t, --terse                              terse output                       
  -v, --version                            show program version               
  -w, --wait <seconds>                     set timeout waiting for finishing operations
                                                            
OBJECT 
  g[eneral]       NetworkManagers general status and operations              
  n[etworking]    overall networking control                                  
  r[adio]         NetworkManager radio switches                               
  c[onnection]    NetworkManagers connections                                
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes

2 创建connection

  1. 查看当前网络状态
# nmcli connection show
NAME         UUID                                  TYPE      DEVICE 
eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0

输出每一行对应一个connection条目。

  1. 删除当前的connection
# nmcli connection delete eth0
Connection 'eth0' (5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03) successfully deleted.
  1. 新建一个connection,使其生效
# nmcli connection add ifname eth0 con-name eth1 type ethernet
Connection 'eth1' (7871698e-1a4e-41d0-80c5-24374ced9526) successfully added.
# nmcli connection show
NAME         UUID                                  TYPE      DEVICE 
eth1         e09fedfe-fe0a-4a30-b304-8bc662427e65  ethernet  --

此时在/etc/sysconfig/network-scripts/下自动生成了一个ifcfg-eth1的配置文件。

# nmcli connection up eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnec
tion/7)

3 修改connection

修改一个已经存在connection,直接编辑/etc/sysconfig/network-scripts/下对应的配置文件,修改完后:

# nmcli connection reload

或者直接通过nmcli connection modify命令进行修改。

# nmcli connection modify help
Usage: nmcli connection modify { ARGUMENTS | help }

ARGUMENTS := [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+

Modify one or more properties of the connection profile.
The profile is identified by its name, UUID or D-Bus path. For multi-valued
properties you can use optional '+' or '-' prefix to the property name.
The '+' sign allows appending items instead of overwriting the whole value.
The '-' sign allows removing selected items instead of the whole value.

ARGUMENTS := remove <setting>

Remove a setting from the connection profile.

Examples:
nmcli con mod home-wifi wifi.ssid rakosnicek
nmcli con mod em1-1 ipv4.method manual ipv4.addr "192.168.1.2/24, 10.10.1.5/8"
nmcli con mod em1-1 +ipv4.dns 8.8.4.4
nmcli con mod em1-1 -ipv4.dns 1
nmcli con mod em1-1 -ipv6.addr "abbe::cafe/56"
nmcli con mod bond0 +bond.options mii=500
nmcli con mod bond0 -bond.options downdelay
nmcli con mod em1-1 remove sriov

4 修改device名称

# ip link set dev eth0 down
# ip link set dev eth0 name eth01

然后将/etc/sysconfig/network-scripts/下的配置文件中的device字段也进行更新。

# ip link set dev eth01 up 
# nmcli connection reload