Ubuntu Linux添加静态路由
时间:2020-01-09 10:45:34 来源:igfitidea点击:
我有两个网络接口。
一个连接到eth0,另一个连接到eth1,如下所示:
eth0 - 具有10.x.x.x/8范围内的专用IP的专用网络。
eth1 - 通过205.153.203.97具有公共IP 205.153.203.98的ISP路由器。
如何设置持久性静态路由,以使仅通过10.70.201.61路由器的10.0.0.0/255.0.0.0流量路由?
您需要编辑/etc/network/interfaces文件。该文件描述了系统上可用的网络接口以及如何激活它们,包括ubuntu Linux服务器静态路由。
语法
语法如下:
route add -net $NET netmask $MASK gw $GATEWAY route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254
您需要将以下语法添加到发布命令中,如下所示:
post-up command post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254
给定命令将在启动界面后运行。
例
编辑`/etc/network/interfaces',执行:
$ sudo vi /etc/network/interfaces
#---------------------------------------#
# Feel free to change IP and gateway #
# as per your local setup and routing #
# policy #
# Last edited by root @ 23/Oct/2012 #
#---------------------------------------#
#--------------------------------------------#
# Setup the loopback network interface (lo0) #
#--------------------------------------------#
auto lo
iface lo inet loopback
#--------------------------------------------#
# Setup eth0 - connected to private LAN/VLAN #
#--------------------------------------------#
auto eth0
allow-hotplug eth0
iface eth0 inet static
address 10.70.201.5
netmask 255.255.255.192
### Ubuntu Linux add persistent route command ###
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.70.201.6
#----------------------------------------#
# Setup eth1 - connected to the Internet #
#----------------------------------------#
auto eth1
allow-hotplug eth1
iface eth1 inet static
address 205.153.203.98
netmask 255.255.255.248
### Ubuntu Linux - This is your default gateway ###
gateway 205.153.203.97
保存并关闭文件。
您需要按照以下方式重新启动网络:
$ sudo service networking restart
或者以root用户身份:
# service networking restart
如何验证我的Ubuntu Linux接口静态路由正常工作?
执行以下命令以查看当前的路由表,运行:
$ /sbin/route -n
或者
$ ip route show
将ICMP ECHO_REQUEST发送到您的lan/vlan上的网络主机:
$ ping lan-ip-address $ ping 10.70.201.6
使用ISP网关将ICMP ECHO_REQUEST发送到网络主机(对Google/Yahoo等公共站点的ping请求):
$ ping google.com $ ping theitroad.local

