试图了解在CentOS中创build静态路由的正确方法,请协助

我一直在试图了解在CentOS 6.x机器上添加静态路由的正确方法。 在一些论坛上,他们说用相关路由创build一个名为route-dev_name的文件(例如route-eth0),并将其放置在/etc/sysconfig/network-scripts ,在某些论坛中他们说这个文件应该被命名为static-routes ,在这两种情况下,我无法设置一个静态路由。 看起来像在某些CentOS发行版中,它只在命名文件route-dev_name ,在某些情况下它只在命名文件static-routes时才起作用。任何人都可以帮助我吗? 这是我的路线文件的内容:

 192.168.20.0/24 via 192.168.20.253 dev eth0 

提前致谢

使用/etc/sysconfig/network-scripts/route-device文件定义的RH样式设备依赖路由引起了很多问题。

所以真正的系统pipe理员只使用/etc/sysconfig/static-routes文件而没有设备依赖性:

 any net 10.0.0.0 netmask 255.255.255.0 gw 192.168.0.1 

问题:

  • 当物理设备绑定时,您还需要记住设备路由 – 设备文件
  • 在虚拟机中重新组织适配器时。

自然,人们应该总是使用桥接设备,这样可以避免路由设备文件问题。

另请注意/etc/init.d/network/etc/sysconfig/static-routes文件中的语法:sniplet:

  # Add non interface-specific static-routes. if [ -f /etc/sysconfig/static-routes ]; then if [ -x /sbin/route ]; then grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do /sbin/route add -$args done else net_log $"Legacy static-route support not available: /sbin/route not found" fi fi 

在/ etc / syconfig / network-scripts / route-eth0中创build一个文件

添加以下内容

 192.168.20.0/24 via 192.168.20.253 dev eth0 

我一直使用这种方法。 我发现这是最好的方法。

供参考:检查 – https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-static-routes.html

有一个更简单的方法来添加路线…

这个文件 – /etc/init.d/network – 在PC引导时启动,它使用文件/etc/sysconfig/static-routes添加静态路由

你必须创build它,因为它不存在。

如果仔细阅读文件/etc/init.d/network ,它将在每个行中读入这个文件以添加路由,那些行必须以“any”开头,而“route add – ”是已知的。

所以在文件中你要创build>> / etc / sysconfig / static-routes,你必须写:

 any net 10.0.0.0 netmask 255.255.255.0 gw 192.168.0.1 eth0 
  • 其中10.0.0.0/24是你想要达到的networking(255.255.255.0是掩码)
  • 其中gw 192.168.0.1是网关到达(当然路由器)
  • eth0是哪里使用的接口。

这是添加静态路由的最佳方式,1个文件(不是X接口的X文件)

我总是在我的CentOS 6.4和6.5上使用route-ethX文件方法:

创build/编辑文件/etc/sysconfig/network-scripts/route-ethX (其中X是您的接口号)并在此文件中设置路由:

 192.168.20.0/24 via 192.168.20.253 dev eth0 

您也可以使用以下格式编辑此文件(我个人更喜欢第一种语法):

 GATEWAY0=192.168.20.253 NETMASK0=255.255.255.0 ADDRESS0=192.168.20.0 

然后重新启动networking以应用更改:

 service network restart 

详细阅读: https : //www.centos.org/docs/5/html/5.2/Deployment_Guide/s1-networkscripts-static-routes.html

/ etc / sysconfig / network-scripts中有一个脚本ifup-routes 。 它会查找名称为route-dev_name的文件,例如route-eth0。