linux network card binding

1. Bind multiple IPs to a single network card

Sometimes, we need to configure multiple IPs on one network card, for example, in the above example, we also need to configure IPs 192.168.168.2 and 192.168.168.3 for eth0. Then you need to create two new configuration files under /etc/sysconfig/network-scripts:

The contents of ifcfg-eth0:0 are as follows:

DEVICE=eth0:0
BOOTPROTO=static
IPADDR=192.168.168.2
NETMASK=255.255.255.0
NBOOT=yes

The contents of ifcfg-eth0:1 are as follows:

DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.168.168.3
NETMASK=255.255.255.0
NBOOT=yes

2. Multiple network cards are bound into a virtual network card

In order to provide high network availability, we may need to bind multiple network cards into a virtual network card to provide external services, so that even if one of the physical network cards fails, the connection will not be interrupted. For example, we can bind eth0 and eth1 into a virtual network card bond0
First create the configuration file ifcfg-bond0 of the virtual network card bond0 under /etc/sysconfig/network-scripts/ , the content is as follows

DEVICE=bond0
BOOTPROTO=none
BROADCAST=192.168.168.255
IPADDR=192.168.168.1
NETMASK=255.255.255.0
NETWORK=192.168.168.0
NBOOT=yes
TYPE=Ethernet
GATEWAY=192.168.168.250
USERCTL=no

Then modify the content of the configuration files ifcfg-eth0 
of eth0 and eth1 respectively :

DEVICE=eth0
BOOTPROTO=none
NBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes

ifcfg-eth1 content

DEVICE=eth1
BOOTPROTO=none
NBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes

Because the virtual network card of Linux is implemented in the kernel module, the module has been installed when it needs to be installed. Add the following to the /etc/modprobe.conf file (it seems that some old versions are /etc/modules.con) (if there is no such file, create a new one):

alias bond0 bonding
options bond0 miimon=100 mode=1 primary=eth0

其中miimon=100表示每100ms检查一次链路连接状态,如果不通则会切换物理网卡
mode=1表示主备模式,也就是只有一块网卡是active的,只提供失效保护。如果mode=0则是负载均衡模式的,所有的网卡都是active,还有其他一些模式很少用到
primary=eth0表示主备模式下eth0为默认的active网卡

最后,在/etc/rc.local中加入

modprobe bonding miimon=100 mode=1

重启机器后可以看到虚拟网卡已经生效,可以通过插拔两个物理网卡的网线来进行测试,不过linux中网卡接管的时间好象比较长。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324034331&siteId=291194637