我在一台机器上设置了 Ubuntu 14.04 服务器,并在 /etc/network/interfaces 中配置了两个网关。默认网关是 em1,没问题,但我需要通过另一个网关将流量发送到互联网上的特定 IP 地址。以下命令在命令行中有效:
sudo route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1
但将其添加到 /etc/network/interfaces 后重启后不起作用:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto em1
iface em1 inet static
address 10.0.2.251
netmask 255.255.255.0
gateway 10.0.2.1
dns-nameservers 8.8.8.8 8.8.4.4
auto em1:0
iface em1:0 inet static
address 172.16.1.6
netmask 255.255.0.0
gateway 172.16.0.1
dns-nameservers 8.8.8.8 8.8.4.4
post-up route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1
有人可以帮忙吗?
答案1
post-up route add -net XXX.XXX.XXX.XXX netmask 255.255.255.255 gw 172.16.0.1 删除 post-up 并将其更改为“up”
上行路由添加 -net XXX.XXX.XXX.XXX 网络掩码 255.255.255.255 gw 172.16.0.1 之后,ifdown em1:0 和 ifup em1:0 或者您可以重新启动 networking.service 欢呼。