双网卡冗余是通过绑定实现的,我们需要通过以下几个步骤实现:
1.配置内核
选中CONFIG_BONDING
2.配置
安装ifenslave
1 sudo apt-get install ifenslave
配置/etc/network/interfaces文件,在文件中添加以下:
#source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
bond-master bond0
auto eth1
iface eth1 inet manual
bond-master bond0
auto bond0
iface bond0 inet static
address 192.168.12.99
netmask 255.255.255.0
broadcast 192.168.12.255
gateway 192.168.12.1
bond-mode 0
bond-miimon 100
dns-nameservers 114.114.114.114
3.配置/etc/rc.local文件,在文件中添加以下:
ifenslave bond0 eth0 eth1
ifconfig bond0 up
4.修改/etc/resolv.conf文件:
nameserver 8.8.8.8
5.重新启动,验证网络是否ok。
分别拔下网口1和网口2使用ping命令验证网络是否ok:
ping 192.168.12.1
结果在两个网口插上和只有任意一个网口插上的时候都可以成功ping通
6.启动后发现串口登录的控制台需要很久才能登录,我这里是5分钟
7.使用命令分析启动时间: systemd-analyze blame
可以看到开机的时候networking.service这个服务消耗了5分钟
8.使用命令关闭消耗时间多的服务的自启动: systemctl disable networking.service
9.重启,开机只用了20秒,再验证一下网络是否ok
ping 192.168.12.100
成功pingtong
正常来说我们是需要仔细分析networking.service这个服务为什么消耗了这么多时间,然后修改该服务,但是现在我们网络是ok的,启动时间也偶ok,最近非常忙,这里就简单记录一下。我估计耗时长是因为两个网卡被用去绑定了,所以networking.service这个服务识别到网卡,但是对这个网卡的设置失败了,所以才消耗了这么长时间。
现在有空看看networking.service这个服务在干啥,我们先把这个服务开启回来:
systemctl ensable networking.service
1
然后看看这个服务的状态:
root@jian-os:/home/jian# systemctl status networking.service
● networking.service - Raise network interfaces
Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor preset: enabled)
Drop-In: /run/systemd/generator/networking.service.d
└─50-insserv.conf-$network.conf
Active: failed (Result: timeout) since 四 2022-01-13 21:00:31 CST; 1min 7s ago
Docs: man:interfaces(5)
Process: 1265 ExecStart=/sbin/ifup -a --read-environment (code=killed, signal=TERM)
Process: 1227 ExecStartPre=/bin/sh -c [ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle (code=exited, status=0/SUCCESS)
Main PID: 1265 (code=killed, signal=TERM)
2月 12 00:28:04 jian-os systemd[1]: Starting Raise network interfaces...
1月 13 21:00:27 jian-os ifup[1265]: /sbin/ifup: waiting for lock on /run/network/ifstate.eth0
1月 13 21:00:31 jian-os systemd[1]: networking.service: Start operation timed out. Terminating.
1月 13 21:00:31 jian-os systemd[1]: Failed to start Raise network interfaces.
1月 13 21:00:31 jian-os systemd[1]: networking.service: Unit entered failed state.
1月 13 21:00:31 jian-os systemd[1]: networking.service: Failed with result 'timeout'.
我们可以看到这个服务是failed 的原因是超时(Result: timeout)。我们也可以看到服务的文件路径:/lib/systemd/system/networking.service
我们看看这个文件:
root@jian-os:/home/jian# cat /lib/systemd/system/networking.service
[Unit]
Description=Raise network interfaces
Documentation=man:interfaces(5)
DefaultDependencies=no
Wants=network.target
After=local-fs.target network-pre.target apparmor.service systemd-sysctl.service systemd-modules-load.service
Before=network.target shutdown.target network-online.target
Conflicts=shutdown.target
[Install]
WantedBy=multi-user.target
WantedBy=network-online.target
[Service]
Type=oneshot
EnvironmentFile=-/etc/default/networking
ExecStartPre=-/bin/sh -c '[ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle'
ExecStart=/sbin/ifup -a --read-environment
ExecStop=/sbin/ifdown -a --read-environment
RemainAfterExit=true
TimeoutStartSec=5min
我们看到最后一行,5分钟超时的参数,验证了为什么我们的网络服务消耗了5分多钟,我们可以把这个超时时间改小一点,改为5s我们就不必关闭网络服务了。