Linux随笔9-IP地址分类以及IP地址管理和网卡绑定

1. IP地址分类

IP地址是用于网络上的主机进行相互通信、数据传输而配置的,没有IP地址,则网络上的各个主机之间将无法进行方便的数据传输和通信。随着网络上的主机越来越多,传统的IPv4地址已经不够用了,IPv6地址已经开始在互联网上应用起来了。但是IPv4在企业内部仍然是无可替代的存在。
而要了解IPv4地址的设置,就需要了解IPv4地址的分类方法以及构成方式。一般来讲,分为5类,具体如下图所示:
在这里插入图片描述
上图中,将IPv4地址划分为5类,D类地址通常用于组播地址,E类地址则作为保留地址;所以最常用到的IPv4地址是A类、B类和C类这三个。这三类地址中:

  • A类地址:网络位占8位(最高的1位固定为0,网络位的剩余7位可变,所以可以划分的网段个数为27=128个,其有效网段范围为1-126,0为预留网段,不做分配,127网段用于表示本地回环地址,即代表当前主机自身,最常用的为127.0.0.1。其中10网段为私有IP地址网段,即10.0.0.0-10.255.255.255为私有网段,不能划分为公网上使用),主机位占24位。而每一位主机位的可选值都是0和1,所以这24位主机位可以表示的主机个数位224个,但是所有位都为0和所有位都为1的情况为特殊情况,需要减去这两种情况,因为主机位全部为0表示这个网段;主机位全部为1表示这个网段的所有主机,即广播地址。所以实际的可以划分的主机个数为224-2=16777214台。
  • B类地址:网络位占16位(最高的2位固定,其中最高位固定为1,次高位固定为0,剩余的14位网络位可变,所以可划分的网段个数位214=16384个,有效的地址范围为128-191,其中172.16.0.0-172.31.255.255为私有网段,不能在公网环境划分),主机位占16位。每一位主机位的可选值均为0和1,所以这16位可以表示的主机个数为216-2=65534台。
  • C类地址:网络为占24位(最高的3位固定,分别为110,其余的21位网络位可变,所以这21位可变网络位可以表示的网段个数为221=2097152个。其网段范围为192-223,其中192.168.0.0-192.168.255.255为私有网段,是不能在公网环境进行划分的),主机位占8位,所以这8位主机位可以表示的主机个数为28-2=128台。

上述划分方式为有类地址划分方式,实际上更常用的是无类地址,即CIDR(Classless Inter-Domain Routing)即无类域间路由。CIDR表示的IP地址通常是“IP地址/子网掩码”这种方式进行表示,比如192.168.155.102/24这种形式。
关于CIDR的详尽介绍,,参见下面两篇文章:

实际中更多的是采用CIDR的方式进行IP地址划分和管理。

2. IP地址配置方法

在Linux上进行IP地址管理的工具,包括传统的ifconfig这个趋于过时的命令,包括其后继命令ip命令家族,以及NetworkManager服务提供的nmcli命令行管理命令和nmtui字符界面管理命令(字符界面的命令用的较少,为交互式)。除此之外,还有直接通过文本编辑命令修改配置文件的方式。总结下主要的修改方式如下:

  • ifconfig命令:过时,不推荐,老工程师可能更习惯使用这个命令。实际上,在这个命令的man手册中,已经表示该命令为过时命令。原文为:“This program is obsolete! For replacement check ip addr and ip link. For statistics use ip -s link.”。所以此处不做过多介绍。
  • ip命令家族:功能强大的网络管理命令,除了可以用于基本的查询、设置网卡的IP地址之外,还可以用于管理路由、隧道以及网络名称空间等。具体可参见其帮助手册man 8 ip。设置的IP地址当次立即生效,无需重启服务;但是系统重启则丢失。
  • nmcli命令:由NetworkManager服务提供,如果要使用这个命令需要启动NetworkManager这个服务。功能强大,可以用于管理网络连接,以及显示网卡状态等。设置的IP地址需要在执行nmcli con reload; nmcli con up iface之后才会生效,该命令直接修改网卡的配置文件,所以系统重启配置的IP地址信息也不会丢失,是永久有效的配置。
  • 文本编辑器修改配置文件:不同的发行版配置文件路径不一样,其中典型的为CentOS/RHEL系列的网卡配置文件,位于"/etc/sysconfig/network-scripts/ifcfg-eth0";以及Ubuntu系统的"/etc/netplan/00-installer-config.yaml"。这两种方式对应的管理命令也不一样。前者通过nmcli命令可管理,而后者则需要通过netplan命令进行管理。修改配置文件的方式为永久有效,重启后仍然保留。

下面分别以这种两发行版为例进行网卡IP地址设置的示例。

2.1. CentOS7.6系统的IP地址配置

以CentOS7.6系统为例,分别介绍IP地址配置的3中不同的方式。

2.1.1. 使用ip命令配置IP地址

查看当前网卡的IP地址信息:

[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]#

给网卡eth0添加一个额外的IP地址:

[root@c7u6m1 ~]# ip addr add 172.16.102.11/24 dev eth0
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.102.11/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# 

上述就给网卡eth0添加了IP地址172.16.102.11/24。除了使用上述这种方式直接给网卡eth0添加第二个IP地址之外,还可以使用网卡别名的形式进行添加,具体如下所示:

[root@c7u6m1 ~]# ip addr add 10.0.0.5/16 dev eth0 label eth0:1
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.102.11/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.5/16 scope global eth0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]#

上述就通过ip命令给网卡eth0的别名eth0:1添加了一个IP地址。如果要删除上述别名的IP地址,则执行如下命令即可:

[root@c7u6m1 ~]# ip addr del 10.0.0.5/16 dev eth0 label eth0:1
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.102.11/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# 

上述添加的IP地址均为立即生效,且为临时的,重启系统之后,IP地址就消失了。

2.1.2. 使用nmcli命令配置IP地址

查看此时网卡eth0的IP地址以及网卡信息,具体如下所示:

[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.102.11/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          20780f7b-9baf-41d9-a6a0-767aa11433b4  bridge    docker0         
docker_gwbridge  0428d7fd-f315-460f-8d91-3424553acc92  bridge    docker_gwbridge 
System eth0      a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
# Generated by parse-kickstart
DEVICE="eth0"
IPV6INIT="yes"
BOOTPROTO="static"
UUID="a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688"
ONBOOT="yes"
IPADDR=192.168.122.2
NETMASK=255.255.255.0
GATEWAY=192.168.122.1
DNS1=192.168.1.1
[root@c7u6m1 ~]#

从上述输出中可以看出,系统上通过ip addr show命令显示的eth0网卡的IP地址信息中,默认名字为eth0,但是在nmcli命令中显示的网卡名称则为’System eth0’,而并不是’eth0’,这是由于在网卡的配置文件中没有指定’NAME=eth0’这个字段导致的。可以修改配置文件的内容,也可以通过nmcli命令进行修改。
下面通过nmcli命令进行修改,如下所示:

[root@c7u6m1 ~]# nmcli con mod 'System eth0' connection.id eth0
[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          20780f7b-9baf-41d9-a6a0-767aa11433b4  bridge    docker0         
docker_gwbridge  0428d7fd-f315-460f-8d91-3424553acc92  bridge    docker_gwbridge 
eth0             a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
[root@c7u6m1 ~]#

从上述输出中可以看出,网卡的名称已经被修改了。关于更多的网卡属性,可以参见man nm-settings,其中分门别类列出了各种对象的各种属性信息。
执行完上述修改操作之后,查看网卡的配置文件信息,具体如下所示:

[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
# Generated by parse-kickstart
DEVICE=eth0
IPV6INIT=yes
BOOTPROTO="static"
UUID=a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688
ONBOOT=yes
IPADDR=192.168.122.2
NETMASK=255.255.255.0
GATEWAY=192.168.122.1
DNS1=192.168.1.1
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
[root@c7u6m1 ~]# 

从上述网卡的配置文件中可以看出,增加了一些信息,尤其是最后一行的’NAME=eth0’。
此时,如果要通过nmcli命令给网卡eth0添加一个额外的IP地址,则需要执行如下命令,具体如下所示:

[root@c7u6m1 ~]# nmcli con mod eth0 +ipv4.addresses '10.0.0.5/16'
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.102.11/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          20780f7b-9baf-41d9-a6a0-767aa11433b4  bridge    docker0         
docker_gwbridge  0428d7fd-f315-460f-8d91-3424553acc92  bridge    docker_gwbridge 
eth0             a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
[root@c7u6m1 ~]# nmcli con reload
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.102.11/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
# Generated by parse-kickstart
DEVICE=eth0
IPV6INIT=yes
BOOTPROTO="static"
UUID=a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688
ONBOOT=yes
IPADDR=192.168.122.2
NETMASK=255.255.255.0
GATEWAY=192.168.122.1
DNS1=192.168.1.1
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
IPADDR1=10.0.0.5
PREFIX1=16
NETMASK1=255.255.0.0
[root@c7u6m1 ~]# nmcli con up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.5/16 brd 10.0.255.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# 

从上述输出中可以看出,nmcli命令添加了IP地址之后,并不会立即生效,而是将更改操作写入到了配置文件中,所以上述在执行了添加ip地址操作之后,还无法查看到添加的IP地址信息,需要执行一下nmcli con reload,但是此时仍然无法显示新添加的IP地址信息,还需要再执行以下nmcli con up eth0这个命令启动更改之后的eth0网卡,才可以使刚才通过nmcli con mod命令添加的IP地址信息生效。
另外,nmcli命令添加IP地址之前,有一个ip命令添加的临时IP地址172.16.102.11/24,在执行了上述的nmcli con mod命令添加了新的IP地址并且执行了nmcli con reloadnmcli con up eth0命令之后,这个临时IP地址就消失了,因为这两条命令相当于重启了网卡,所以此前添加的临时IP地址信息就自然会消失了。

2.1.3. 通过修改配置文件的方式配置IP地址

使用vim编辑器修改网卡的配置文件,具体如下所示:

[root@c7u6m1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by parse-kickstart
DEVICE=eth0
IPV6INIT=yes
BOOTPROTO="static"
UUID=a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688
ONBOOT=yes
IPADDR=192.168.122.2
NETMASK=255.255.255.0
GATEWAY=192.168.122.1
DNS1=192.168.1.1
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
IPADDR1=10.0.0.5
PREFIX1=16
NETMASK1=255.255.0.0
IPADDR2=172.16.101.125
PREFIX2=16
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.5/16 brd 10.0.255.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# nmcli con reload
[root@c7u6m1 ~]# nmcli con up eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@c7u6m1 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8c:76:34 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.2/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.5/16 brd 10.0.255.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.16.101.125/16 brd 172.16.255.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe8c:7634/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# 

上述操作中,通过vim编辑器直接修改配置文件,在其中增加了"IPADDR2=172.16.101.125 PREFIX2=16"这两行内容,此时查询系统的IP地址信息,并没有更新。在执行了nmcli con reload; nmcli con up eth0这两个命令之后,修改的配置文件就生效了。添加的IP地址信息就显示出来了。

2.2. Ubuntu20.04系统的IP地址配置

Ubuntu20.04上的配置文件管理并不是通过nmcli这个命令,而是通过netplan这个命令,而且配置文件也不是上述的key=value的形式,而是YAML格式的配置文件。修改过的配置文件需要执行netplan apply命令之后才会生效。下面从两方面来介绍Ubuntu20.04上的IP地址配置和管理操作。

2.2.1. 使用ip命令配置IP地址

通过ip命令配置IP地址,与在CentOS7.6上的配置方式并没有什么不同,具体如下所示:

root@ubuntu20u04:~# ip addr add 172.16.100.20/16 dev enp1s0
root@ubuntu20u04:~# ip addr show enp1s0
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:cc:fe:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.30/24 brd 192.168.122.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet 172.16.100.20/16 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fecc:fe2d/64 scope link 
       valid_lft forever preferred_lft forever
root@ubuntu20u04:~# 

上述就给网卡添加了第二个IP地址。
同样的,也可以给网卡别名添加IP地址,具体如下所示:

root@ubuntu20u04:~# ip addr add 10.0.0.7/16 dev enp1s0 label enp1s0:1
root@ubuntu20u04:~# ip addr show enp1s0
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:cc:fe:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.30/24 brd 192.168.122.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet 172.16.100.20/16 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet 10.0.0.7/16 scope global enp1s0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fecc:fe2d/64 scope link 
       valid_lft forever preferred_lft forever
root@ubuntu20u04:~# 

上述就给网卡别名enp1s0:1添加了10.0.0.7/16这个IP地址。
如果要删掉网卡别名的IP地址,则可以执行如下命令:

root@ubuntu20u04:~# ip addr del 10.0.0.7/16 dev enp1s0 label enp1s0:1
root@ubuntu20u04:~# ip addr show enp1s0
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:cc:fe:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.30/24 brd 192.168.122.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet 172.16.100.20/16 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fecc:fe2d/64 scope link 
       valid_lft forever preferred_lft forever
root@ubuntu20u04:~# 

上述操作就将网卡别名以及其对应的IP地址删除了。

2.2.2. 通过修改配置文件的方式配置IP地址

Ubuntu20.04上的网卡配置文件从以前的"/etc/network/interfaces"切换为了YAML格式的配置文件"/etc/netplan/00-installer-config.yaml",查看该YAML文件的内容如下:

root@ubuntu20u04:~# ls /etc/netplan/00-installer-config.yaml 
/etc/netplan/00-installer-config.yaml
root@ubuntu20u04:~# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    enp1s0:
      dhcp4: false
      addresses:
      - 192.168.122.30/24
      gateway4: 192.168.122.1
      nameservers:
        addresses: [127.0.0.53, 192.168.1.1, 192.168.18.1]
      routes:
      - to: 192.168.122.1
        via: 0.0.0.0
  version: 2
root@ubuntu20u04:~# 

可以使用vim编辑器直接修改上述的YAML配置文件,比如将前面删除的10.0.0.7/16这个临时IP地址增加到上述的配置文件中。修改后的配置文件内容如下:

root@ubuntu20u04:~# vim /etc/netplan/00-installer-config.yaml
root@ubuntu20u04:~# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    enp1s0:
      dhcp4: false
      addresses:
      - 192.168.122.30/24
      - 10.0.0.7/16
      gateway4: 192.168.122.1
      nameservers:
        addresses: [127.0.0.53, 192.168.1.1, 192.168.18.1]
      routes:
      - to: 192.168.122.1
        via: 0.0.0.0
  version: 2

上述在enp1s0.addresses下面增加了一个列表项,即10.0.0.7/16这个CIDR的IP地址。修改完这个配置文件之后,要使其生效,需要执行命令netplan apply才可以。具体如下所示:

root@ubuntu20u04:~# netplan apply
root@ubuntu20u04:~# ip addr show enp1s0
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:cc:fe:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.30/24 brd 192.168.122.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet 10.0.0.7/16 brd 10.0.255.255 scope global enp1s0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fecc:fe2d/64 scope link 
       valid_lft forever preferred_lft forever
root@ubuntu20u04:~#

由于netplan apply相当于重启了网卡,所以此前通过ip addr add命令添加的临时IP地址就消失了。而写在配置文件中的第二个IP地址则添加成功。

3. 用nmcli命令实现多网卡绑定

由于nmcli命令可以通过间接修改配置文件的方式对网卡设备进行管理,所以其更改和配置是永久生效的。下面还是以c7u6m1这个虚拟机为例,实现nmcli命令的多网卡绑定。

3.1. 在宿主机中给虚拟机添加网卡

查看虚拟机上的网卡设备,具体如下所示:

[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          20780f7b-9baf-41d9-a6a0-767aa11433b4  bridge    docker0         
docker_gwbridge  0428d7fd-f315-460f-8d91-3424553acc92  bridge    docker_gwbridge 
eth0             a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
[root@c7u6m1 ~]# 

从上述输出可以看出,实际的网卡设备,即类型为ethernet的设备只有一个eth0。为了完成此实验,需要再添加2块额外的网卡,并将这两块网卡实现绑定。
查看宿主机的网络环境,具体如下所示:

[root@localhost ~]# virsh net-list
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes
 vnet11    active   yes         yes
 vnet12    active   yes         yes
 vnet13    active   yes         yes
 vnet14    active   yes         yes

[root@localhost ~]# virsh net-info default
Name:           default
UUID:           7b03db65-aa5e-44ed-8961-d72ece83ce95
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0

[root@localhost ~]# ip addr show virbr0
4: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:45:1e:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

从上述输出中可以看出,default这个网络为默认的网络,即virbr0,所有的虚拟机都连接到这个网卡设备上。新添加的两个网卡可以是接到这个网桥上,也可以接到其他网桥上。此处我们将其接入到这个默认网桥设备上。具体如下所示:

[root@localhost ~]# virsh attach-interface c7u6m1 --type bridge --source virbr0 --config --live
Interface attached successfully

[root@localhost ~]# virsh attach-interface c7u6m1 --type bridge --source virbr0 --config --live
Interface attached successfully

上述就成功添加了两个网卡设备,在虚拟机中查看添加的网卡设备,具体如下所示:

[root@c7u6m1 ~]# lspci | egrep -i ethernet
00:02.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:0b.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8100/8101L/8139 PCI Fast Ethernet Adapter (rev 20)
00:0c.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8100/8101L/8139 PCI Fast Ethernet Adapter (rev 20)
[root@c7u6m1 ~]# 

从上述输出中可以看出,添加的两个网卡并不是virtio类型的。
在宿主机上查看添加的网卡设备,具体如下所示:

[root@localhost ~]# virsh domiflist c7u6m1
 Interface   Type     Source   Model     MAC
------------------------------------------------------------
 vnet1       bridge   virbr0   virtio    52:54:00:8c:76:34
 vnet10      bridge   virbr0   rtl8139   52:54:00:40:37:87
 vnet11      bridge   virbr0   rtl8139   52:54:00:3b:11:ba

从上述输出可以看出,默认的网卡Model为virtio,而上述通过命令行添加的两块网卡的Model为rtl8139,前者使用了半虚拟化驱动,所以性能会更好;后者则是完全软件虚拟化的网卡,性能会差一些。所以此处将其删除,然后再重新添加Model为virtio的网卡。删除过程具体如下所示:

[root@localhost ~]# virsh dumpxml c7u6m1 | egrep 8139 -C 5
    </interface>
    <interface type='bridge'>
      <mac address='52:54:00:40:37:87'/>
      <source bridge='virbr0'/>
      <target dev='vnet10'/>
      <model type='rtl8139'/>
      <alias name='net1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
    </interface>
    <interface type='bridge'>
      <mac address='52:54:00:3b:11:ba'/>
      <source bridge='virbr0'/>
      <target dev='vnet11'/>
      <model type='rtl8139'/>
      <alias name='net2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0c' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/25'/>
[root@localhost ~]# virsh detach-interface c7u6m1 --config --type bridge --mac '52:54:00:40:37:87'
Interface detached successfully

[root@localhost ~]# virsh detach-interface c7u6m1 --config --type bridge --mac '52:54:00:3b:11:ba'
Interface detached successfully

为了删除上述网卡,需要先获知其MAC地址,然后通过virsh detach-interface命令即可删除网卡。
此时在虚拟机中查看网卡信息,具体如下所示:

[root@c7u6m1 ~]# lspci | egrep -i ethernet
00:02.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:0b.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8100/8101L/8139 PCI Fast Ethernet Adapter (rev 20)
00:0c.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8100/8101L/8139 PCI Fast Ethernet Adapter (rev 20)
[root@c7u6m1 ~]# reboot

仍然具有上述网卡信息,重启虚拟机。然后在宿主机上进行查看,发现网卡设备仍然处于附加状态,而且无法被删除。具体如下所示:

[root@localhost ~]# virsh domiflist c7u6m1
 Interface   Type     Source   Model     MAC
------------------------------------------------------------
 vnet1       bridge   virbr0   virtio    52:54:00:8c:76:34
 vnet10      bridge   virbr0   rtl8139   52:54:00:40:37:87
 vnet11      bridge   virbr0   rtl8139   52:54:00:3b:11:ba

[root@localhost ~]# virsh detach-interface c7u6m1 --type bridge --mac 52:54:00:40:37:87 --persistent --config
error: No interface with MAC address 52:54:00:40:37:87 was found
error: Failed to detach interface

[root@localhost ~]# virsh detach-interface c7u6m1 --type bridge --persistent --config --mac 52:54:00:3b:11:ba
error: No interface with MAC address 52:54:00:3b:11:ba was found
error: Failed to detach interface

从上述输出可以看出,这两个网卡设备应该确实已经从配置文件删除了。
此时查看该虚拟机的配置文件,发现确实没有上述两个网卡配置信息了,具体如下所示:

[root@localhost ~]# cat /etc/libvirt/qemu/c7u6m1.xml | egrep 'mac address=' -C 5
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:8c:76:34'/>
      <source bridge='virbr0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </interface>
    <serial type='pty'>
[root@localhost ~]#

关闭虚拟机,然后重新开机,再查看是否彻底将附加的网卡删除。具体如下所示:

[root@c7u6m1 ~]# systemctl poweroff
Connection to c7u6m1 closed by remote host.
Connection to c7u6m1 closed.

然后重启启动虚拟机,具体如下:

[root@localhost ~]# virsh start c7u6m1
Domain c7u6m1 started

然后重新链接到虚拟机查看,是否移除了此前附加的网卡,具体如下所示:

[root@c7u6m1 ~]# lspci | egrep -i ethernet
00:02.0 Ethernet controller: Red Hat, Inc. Virtio network device
[root@c7u6m1 ~]#

此时网卡已经被移除了。
重新添加Model为virtio的网卡,具体如下所示:

[root@localhost ~]# virsh attach-interface c7u6m1 --type bridge --source virbr0 --model virtio --persistent --config --live
Interface attached successfully

[root@localhost ~]# virsh attach-interface c7u6m1 --type bridge --source virbr0 --model virtio --persistent --config --live
Interface attached successfully

[root@localhost ~]# 

在虚拟机上查看添加好的网卡设备,具体如下所示:

[root@c7u6m1 ~]# lspci | egrep -i ethernet
00:02.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:08.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:09.0 Ethernet controller: Red Hat, Inc. Virtio network device
[root@c7u6m1 ~]#
[root@c7u6m1 ~]# nmcli con show
NAME                UUID                                  TYPE      DEVICE          
docker0             4330980f-3eeb-47a9-8c9d-8e1acfad2155  bridge    docker0         
docker_gwbridge     b37e93b4-36a5-49f1-9627-7725570c2754  bridge    docker_gwbridge 
eth0                a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
Wired connection 1  aa66ab25-76d8-3aac-864e-fe4100e94935  ethernet  eth1            
Wired connection 2  c1bce38d-b05a-3267-986e-3e7e05d799dd  ethernet  eth2            
[root@c7u6m1 ~]# nmcli con mod 'Wired connection 1' connection.id eth1
[root@c7u6m1 ~]# nmcli con mod 'Wired connection 2' connection.id eth2
[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          4330980f-3eeb-47a9-8c9d-8e1acfad2155  bridge    docker0         
docker_gwbridge  b37e93b4-36a5-49f1-9627-7725570c2754  bridge    docker_gwbridge 
eth0             a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
eth1             aa66ab25-76d8-3aac-864e-fe4100e94935  ethernet  eth1            
eth2             c1bce38d-b05a-3267-986e-3e7e05d799dd  ethernet  eth2            
[root@c7u6m1 ~]# 

从上述输出中可以看出,已经添加了虚拟网卡,且名称分别为’Wired connection 1’和’Wired connection 2’,这两个名称并不方便管理,所以此处通过nmcli命令将其重命名为eth1和eth2。
至此,Model为virtio的2块虚拟网卡就添加完成了。

3.2. 使用nmcli命令实现多网卡绑定

3.2.1. 绑定网卡的工作模式介绍

关于网卡绑定的各个模式,以及是否需要交换机做特殊配置,RedHat官方手册给出的说明如下图所示:
在这里插入图片描述上图中指出,模式为0, 2, 3是需要交换价做出特殊设置的,而其他模式则一般不需要交换机做出特殊设置。

网卡绑定的各个模式之间的说明,参见上图给出的内核文档的链接:kernel-document-networking-bonding,其中关于模式说明的部分,摘录如下:

mode

Specifies one of the bonding policies. The default is
balance-rr (round robin).  Possible values are:

balance-rr or 0

	Round-robin policy: Transmit packets in sequential
	order from the first available slave through the
	last.  This mode provides load balancing and fault
	tolerance.

active-backup or 1

	Active-backup policy: Only one slave in the bond is
	active.  A different slave becomes active if, and only
	if, the active slave fails.  The bond's MAC address is
	externally visible on only one port (network adapter)
	to avoid confusing the switch.

	In bonding version 2.6.2 or later, when a failover
	occurs in active-backup mode, bonding will issue one
	or more gratuitous ARPs on the newly active slave.
	One gratuitous ARP is issued for the bonding master
	interface and each VLAN interfaces configured above
	it, provided that the interface has at least one IP
	address configured.  Gratuitous ARPs issued for VLAN
	interfaces are tagged with the appropriate VLAN id.

	This mode provides fault tolerance.  The primary
	option, documented below, affects the behavior of this
	mode.

balance-xor or 2

	XOR policy: Transmit based on the selected transmit
	hash policy.  The default policy is a simple [(source
	MAC address XOR'd with destination MAC address XOR
	packet type ID) modulo slave count].  Alternate transmit
	policies may be	selected via the xmit_hash_policy option,
	described below.

	This mode provides load balancing and fault tolerance.

broadcast or 3

	Broadcast policy: transmits everything on all slave
	interfaces.  This mode provides fault tolerance.

802.3ad or 4

	IEEE 802.3ad Dynamic link aggregation.  Creates
	aggregation groups that share the same speed and
	duplex settings.  Utilizes all slaves in the active
	aggregator according to the 802.3ad specification.

	Slave selection for outgoing traffic is done according
	to the transmit hash policy, which may be changed from
	the default simple XOR policy via the xmit_hash_policy
	option, documented below.  Note that not all transmit
	policies may be 802.3ad compliant, particularly in
	regards to the packet mis-ordering requirements of
	section 43.2.4 of the 802.3ad standard.  Differing
	peer implementations will have varying tolerances for
	noncompliance.

	Prerequisites:

	1. Ethtool support in the base drivers for retrieving
	the speed and duplex of each slave.

	2. A switch that supports IEEE 802.3ad Dynamic link
	aggregation.

	Most switches will require some type of configuration
	to enable 802.3ad mode.

balance-tlb or 5

	Adaptive transmit load balancing: channel bonding that
	does not require any special switch support.

	In tlb_dynamic_lb=1 mode; the outgoing traffic is
	distributed according to the current load (computed
	relative to the speed) on each slave.

	In tlb_dynamic_lb=0 mode; the load balancing based on
	current load is disabled and the load is distributed
	only using the hash distribution.

	Incoming traffic is received by the current slave.
	If the receiving slave fails, another slave takes over
	the MAC address of the failed receiving slave.

	Prerequisite:

	Ethtool support in the base drivers for retrieving the
	speed of each slave.

balance-alb or 6

	Adaptive load balancing: includes balance-tlb plus
	receive load balancing (rlb) for IPV4 traffic, and
	does not require any special switch support.  The
	receive load balancing is achieved by ARP negotiation.
	The bonding driver intercepts the ARP Replies sent by
	the local system on their way out and overwrites the
	source hardware address with the unique hardware
	address of one of the slaves in the bond such that
	different peers use different hardware addresses for
	the server.

	Receive traffic from connections created by the server
	is also balanced.  When the local system sends an ARP
	Request the bonding driver copies and saves the peer's
	IP information from the ARP packet.  When the ARP
	Reply arrives from the peer, its hardware address is
	retrieved and the bonding driver initiates an ARP
	reply to this peer assigning it to one of the slaves
	in the bond.  A problematic outcome of using ARP
	negotiation for balancing is that each time that an
	ARP request is broadcast it uses the hardware address
	of the bond.  Hence, peers learn the hardware address
	of the bond and the balancing of receive traffic
	collapses to the current slave.  This is handled by
	sending updates (ARP Replies) to all the peers with
	their individually assigned hardware address such that
	the traffic is redistributed.  Receive traffic is also
	redistributed when a new slave is added to the bond
	and when an inactive slave is re-activated.  The
	receive load is distributed sequentially (round robin)
	among the group of highest speed slaves in the bond.

	When a link is reconnected or a new slave joins the
	bond the receive traffic is redistributed among all
	active slaves in the bond by initiating ARP Replies
	with the selected MAC address to each of the
	clients. The updelay parameter (detailed below) must
	be set to a value equal or greater than the switch's
	forwarding delay so that the ARP Replies sent to the
	peers will not be blocked by the switch.

	Prerequisites:

	1. Ethtool support in the base drivers for retrieving
	the speed of each slave.

	2. Base driver support for setting the hardware
	address of a device while it is open.  This is
	required so that there will always be one slave in the
	team using the bond hardware address (the
	curr_active_slave) while having a unique hardware
	address for each slave in the bond.  If the
	curr_active_slave fails its hardware address is
	swapped with the new curr_active_slave that was
	chosen.

3.2.2. 网卡绑定实施

此处采用模式5,即balance-tlb进行网卡绑定设置。具体配置过程如下:

  1. 添加绑定连接
[root@c7u6m1 ~]# nmcli con add type bond ifname mybond0 bond.options "mode=balance-tlb,miimon=100"
Connection 'bond-mybond0' (0689011d-f24e-4013-8338-db70a524f006) successfully added.
[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          4330980f-3eeb-47a9-8c9d-8e1acfad2155  bridge    docker0         
docker_gwbridge  b37e93b4-36a5-49f1-9627-7725570c2754  bridge    docker_gwbridge 
eth0             a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
eth1             aa66ab25-76d8-3aac-864e-fe4100e94935  ethernet  eth1            
eth2             c1bce38d-b05a-3267-986e-3e7e05d799dd  ethernet  eth2            
bond-mybond0     0689011d-f24e-4013-8338-db70a524f006  bond      mybond0         
[root@c7u6m1 ~]#
[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond-mybond0 
BONDING_OPTS="miimon=100 mode=balance-tlb"
TYPE=Bond
BONDING_MASTER=yes
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
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=bond-mybond0
UUID=0689011d-f24e-4013-8338-db70a524f006
DEVICE=mybond0
ONBOOT=yes
[root@c7u6m1 ~]#

从上述输出中可以看出,使用nmcli命令添加了绑定连接之后,就自动创建了该连接的配置文件。

  1. 向绑定连接中添加从网卡
[root@c7u6m1 ~]# nmcli con add type ethernet ifname eth1 master mybond0
Connection 'bond-slave-eth1' (678f769d-5d5b-49d1-9c60-921e04e998f5) successfully added.
[root@c7u6m1 ~]# nmcli con add type ethernet ifname eth2 master mybond0
Connection 'bond-slave-eth2' (0424737c-ea97-4ab2-b0ed-ca807a17ca63) successfully added.
[root@c7u6m1 ~]# nmcli con show
NAME             UUID                                  TYPE      DEVICE          
docker0          4330980f-3eeb-47a9-8c9d-8e1acfad2155  bridge    docker0         
docker_gwbridge  b37e93b4-36a5-49f1-9627-7725570c2754  bridge    docker_gwbridge 
eth0             a5f686e6-0d1b-45a2-8b9a-acdd6dbaa688  ethernet  eth0            
eth1             aa66ab25-76d8-3aac-864e-fe4100e94935  ethernet  eth1            
eth2             c1bce38d-b05a-3267-986e-3e7e05d799dd  ethernet  eth2            
bond-mybond0     0689011d-f24e-4013-8338-db70a524f006  bond      mybond0         
bond-slave-eth1  678f769d-5d5b-49d1-9c60-921e04e998f5  ethernet  --              
bond-slave-eth2  0424737c-ea97-4ab2-b0ed-ca807a17ca63  ethernet  --              
[root@c7u6m1 ~]# 

上述就向此前创建好的绑定中添加了两块网卡,即eth1和eth2。分别查看这两块网卡的配置文件,具体如下所示:

[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 
HWADDR=52:54:00:6D:82:EA
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
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=eth1
UUID=aa66ab25-76d8-3aac-864e-fe4100e94935
ONBOOT=yes
AUTOCONNECT_PRIORITY=-999
[root@c7u6m1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2
HWADDR=52:54:00:46:AE:36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
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
UUID=c1bce38d-b05a-3267-986e-3e7e05d799dd
ONBOOT=yes
AUTOCONNECT_PRIORITY=-999
[root@c7u6m1 ~]# 

至此,从属网卡就添加完成了。

  1. 激活从属网卡以及绑定
[root@c7u6m1 ~]# ip addr show mybond0
8: mybond0: <NO-CARRIER,BROADCAST,MULTICAST,MASTER,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 42:4b:28:5b:9a:ea brd ff:ff:ff:ff:ff:ff
[root@c7u6m1 ~]# nmcli con up bond-slave-eth1
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
[root@c7u6m1 ~]# nmcli con up bond-slave-eth2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
[root@c7u6m1 ~]# nmcli con up bond-mybond0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/10)
[root@c7u6m1 ~]# ip addr show mybond0
8: mybond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:6d:82:ea brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.139/24 brd 192.168.122.255 scope global noprefixroute dynamic mybond0
       valid_lft 3594sec preferred_lft 3594sec
    inet6 fe80::4871:b1ad:8fa2:647/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@c7u6m1 ~]# ip addr show eth1
5: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master mybond0 state UP group default qlen 1000
    link/ether 52:54:00:6d:82:ea brd ff:ff:ff:ff:ff:ff
[root@c7u6m1 ~]# ip addr show eth2
6: eth2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master mybond0 state UP group default qlen 1000
    link/ether 52:54:00:46:ae:36 brd ff:ff:ff:ff:ff:ff
[root@c7u6m1 ~]# ip addr show bond-slave-eth1
Device "bond-slave-eth1" does not exist.
[root@c7u6m1 ~]# 

从上述输出中可以看出,在未激活两个从属网卡以及绑定连接之前,mybond0是没有IP地址的。在激活了这3者之后,mybond0通过DHCP协议获得了192.168.122.139/24这个IP地址。至此,通过nmcli操作的网卡绑定操作就完成了。

4. References

Chapter 7. Configure Network Bonding

ThinkPHP 是一个优秀的PHP框架,可以帮助我们快速开发高质量的 Web 应用程序。因此,使用 ThinkPHP 来开发 OA 系统是非常方便和实用的。下面我们来讨论一下如何使用 ThinkPHP 来开发一个人力资源管理系统。 1. 创建数据库 首先,我们需要创建一个数据库来存储我们的数据。在该数据库中,我们需要创建一些表来存储员工信息、部门信息、职位信息、薪资信息等等。我们可以使用 MySQL 数据库来创建这些表。 2. 创建 ThinkPHP 项目 在创建完数据库之后,我们需要创建一个 ThinkPHP 项目来实现我们的 OA 系统。我们可以使用 ThinkPHP 官方提供的命令行工具来创建项目,具体命令如下: ``` composer create-project topthink/think oa ``` 执行完上述命令后,会在当前目录下创建一个名为 oa 的 ThinkPHP 项目。 3. 配置数据库 在项目创建好之后,我们需要配置数据库连接信息。在项目的 `.env` 文件中,我们可以配置数据库的相关信息,如下所示: ``` # 数据库类型 DB_CONNECTION=mysql # 数据库地址 DB_HOST=127.0.0.1 # 数据库端口 DB_PORT=3306 # 数据库名 DB_DATABASE=oa # 数据库用户名 DB_USERNAME=root # 数据库密码 DB_PASSWORD= ``` 我们需要根据自己的实际情况来修改这些配置项。 4. 创建控制器和模型 在 ThinkPHP 中,我们可以通过创建控制器和模型来实现 OA 系统的功能。我们可以使用命令行工具来快速创建控制器和模型,具体命令如下: ``` # 创建控制器 php think make:controller Index # 创建模型 php think make:model User ``` 执行完上述命令后,会在项目中生成一个名为 Index 的控制器和一个名为 User 的模型。 5. 实现功能 在创建好控制器和模型之后,我们就可以开始实现 OA 系统的功能了。比如,我们可以实现员工信息的添加、修改、删除、查询等操作,部门信息的添加、修改、删除、查询等操作,职位信息的添加、修改、删除、查询等操作,薪资信息的添加、修改、删除、查询等操作等等。我们可以在控制器中编写处理逻辑,在模型中编写数据库操作。最后,我们可以使用视图来展示数据。 6. 测试系统 在实现完功能之后,我们需要对系统进行测试。我们可以使用浏览器或者 Postman 等工具来测试系统的功能是否正常。如果有问题,我们可以根据错误提示来进行调试和修复。 总之,使用 ThinkPHP 来开发 OA 系统是非常方便和实用的。我们可以根据自己的实际情况来定制系统的功能和界面,从而更好地管理公司的人力资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值