服务器配置需求如下:
测试地址(IP):88.17.1.146
外网地址(IP):61.184.138.35
外网掩码:255.255.255.128
外网网关:61.184.138.1
内网地址(IP):172.17.111.49
内网掩码:255.255.255.192
内网网关:172.17.111.62
我们将其中一块网卡(比如外网网卡 enp6s0)设置成外网网卡,另外一块网卡enp7s0设置内网网卡。
一、输入命令:
sudo vim /etc/network/interfaces
得到的结果如下:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto enp6s0
iface enp6s0 inet static
address 61.184.138.35
netmask 255.255.255.128
network 61.184.138.0
broadcast 61.184.138.127
gateway 61.184.138.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 61.184.138.1
auto enauto enp7s0
iface enp7s0 inet static
address 172.17.111.49
netmask 255.255.255.192
重启网络:
sudo /etc/init.d/networking restart
此时网卡会报错,不用管报错信息。
二、增加2个路由表分别是内网:camp、外网:outer
vi /etc/iproute2/rt_tables
得到并写入如下信息
三、修改路由信息
sudo vim /etc/rc.local
文本信息如下:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
service network-manager stop
sudo ip route flush table camp
sudo ip route add default via 172.17.111.62 dev enp7s0 src 172.17.111.49 table camp
sudo ip rule add from 172.17.111.49 table camp
sudo ip route flush table outer
sudo ip route add default via 61.184.138.1 dev enp6s0 src 61.184.138.35 table outer
sudo ip rule add from 61.184.138.35 table outer
sudo route add -net 172.17.111.0/24 gw 172.17.111.62 dev enp7s0
sudo route add -net 88.17.1.0/24 gw 172.17.111.62 dev enp7s0
四、ubuntu服务器重启之后需要重新执行如下命令,才能ping通目标地址
sudo route add -net 172.17.111.0/24 gw 172.17.111.62 dev enp7s0
sudo route add -net 88.17.1.0/24 gw 172.17.111.62 dev enp7s0
五、查看路由信息
ip route
完成测试