服务器在两个内网环境中,一个为192.168.1.0/24,一个10.0.0.1/24,需要访问这两个网段中的服务器

一开始修改配置/etc/network/interfaces文件

1
2
3
4
5
6
7
8
9
10
11
auto enp4s0f0
iface enp4s0f0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1

auto enp4s0f1
iface enp4s0f1 inet static
address 10.0.0.2
netmask 255.255.255.0
gateway 10.0.0.1

重启网络服务service networking restart失败,一个服务器不能有两个网关,也尝试过使用其中一个网关,但另一个网段的地址就无法访问了,后面的解决办法是不设置网关参数,在/etc/network/interfaces文件添加路由,配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
auto enp4s0f0
iface enp4s0f0 inet static
address 192.168.13.2
netmask 255.255.255.0
#gateway 192.168.13.1

auto enp4s0f1
iface enp4s0f1 inet static
address 210.27.169.25
netmask 255.255.255.0
#gateway 210.27.169.254
up route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.0.1 dev enp4s0f1
up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev enp4s0f0

要访问哪个网断,添加对应的路由即可,dev后面的参数为网卡名称,up route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.0.1 dev enp4s0f1这条路由的意思是访问10.0.0.0./24网段的地址数据包都通过网卡enp4s0f1出去

service networking restart命令重启网络前(重启发现之前的ip配置信息还存在,结果造成一些测试问题),会先执行ip addr flush dev enp4s0f1命令,该命令会清除网卡接口配置信息包括该网卡相关的路由信息,该命令要慎用,如果是远程执行会导致断网!请读者注意

修改配置文件之前也可以使用route add命令添加临时路由测试网络,测试好了再修改/etc/network/interfaces文变成永久路由