ubuntu配置静态路由及重启生效

第一种方法:使用route命令(添加临时路由)

添加到主机的路由

route add -host 192.168.1.123 dev eth0
route add -host 192.168.1.123 gw 192.168.1.1

 

添加到网络的路由

route add -net 192.168.1.123 netmask 255.255.255.0 eth0
route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1
route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1 eth1
route add -net 192.168.1.0/24 eth1

 

添加默认网关

route add default gw 192.168.1.1

 

删除路由

route del -host 192.168.1.11 dev eth0
route del -net 192.168.1.123 netmask 255.255.255.0

 

第二种方法: 修改/etc/rc.local

修改/etc/rc.local,只是要注意的一点是不要卸载 "exit 0"的后面

vi /etc/rc.local
route add default gw 192.168.1.1

这样在系统启动的时候会自动加入相关的路由设置

 

注:

(1)如果某个系统服务,比如说是NFS服务,这个服务是在启动network服务之后,在执行rc.local之前,如果你设置的有自动挂载的nfs,那么,这里链路的不通畅,会造成挂载的失败。

(2)如果你重启了网络服务器,那么路由就失效了,这个时候你不得不重新加载这个文件,但是如果你是远程操作的呢?所以,这个方法不是非常的不推荐

第三种方法:修改interfaces文件

up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

可以看到这个格式和route命令的唯一区别在于,在前面多了一个up

 

下面列出我的interface文件仅供参考,由于安全因素,这里的ip我都用xx替代了:

# 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 eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx
auto eth0:0
iface eth0:0 inet static
address xxx.xxx.xxx.xxx
netmask xxx.xxx.xxx.xxx
#dns-nameservers 202.102.224.68 202.102.227.68
up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0
up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0
up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0

 

第四种:在/etc/sysconfig/network

network里添加到文件末尾,格式如下

vi /etc/sysconfig/network
GATEWAY=gw-ip 或者 GATEWAY=gw-dev

注:这种方式从外观上来看只能用于添加默认网关了,对于多路网络环境貌似不太适合的样子

 

第五种:直接写入ifcfg文件

在配置ip地址的时候直接将GATEWAY的配置写入ifcfg文件,形式:GATEWAY=gw-ip

适合添加默认路由

第六种:写入/etc/sysconfig/static-routes文件

默认在/etc/sysconifg目录中是没有这个文件的,需要我们手工创建,对这个文件的调用在下面:

/etc/init.d/network:

# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done

版权声明:
作者:RuyeNet
链接:https://www.iloveu.top/archives/154
来源:RuyeNet
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录