[root@VM-4-10-centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.4.1 0.0.0.0 UG 0 0 0 eth0
10.0.4.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
[root@VM-4-10-centos ~]#
通过route add 添加静态路由,网卡或服务等重启就失效了
[root@VM-4-10-centos ~]# route --help
Usage: route [-nNvee] [-FC] [<AF>] List kernel routing tables
route [-v] [-FC] {add|del|flush} ... Modify routing table for AF.
route {-h|--help} [<AF>] Detailed usage syntax for specified AF.
route {-V|--version} Display version/author and exit.
-v, --verbose be verbose
-n, --numeric don't resolve names
-e, --extend display other/more information
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
<AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
List of possible address families (which support routing):
inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
x25 (CCITT X.25)
[root@VM-4-10-centos ~]#
手动添加主机/网段路由示例:
#添加主机路由(指定下一跳网关IP)
route add -host 172.17.33.25 gw 10.0.4.1
#添加主机路由(指定出接口)
route add -host 172.17.33.25 dev eth0
#添加网段路由(指定下一跳网关IP)
route add -net 172.17.33.0/24 gw 10.0.4.1
#添加主机路由(指定出接口)
route add -net 172.17.33.0/24 dev eth0
##添加网段路由(同时指定出接口和下一跳网关IP)
route add -net 172.17.33.0/24 dev eth0 gw 10.0.4.1
永久添加的两种方式:
A:添加至系统开机启动脚本/etc/rc.local //这是个软链接,实际指向/etc/rc.d/rc.local
[root@VM-4-10-centos ~]# ls -l /etc/rc.local
lrwxrwxrwx 1 root root 13 Nov 9 10:00 /etc/rc.local -> rc.d/rc.local
[root@VM-4-10-centos ~]#
vi /etc/rc.local
将上面route add指令添加到末尾
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/usr/local/qcloud/irq/net_smp_affinity.sh >/tmp/net_affinity.log 2>&1
/usr/local/qcloud/cpuidle/cpuidle_support.sh &> /tmp/cpuidle_support.log
/usr/local/qcloud/rps/set_rps.sh >/tmp/setRps.log 2>&1
/usr/local/qcloud/irq/virtio_blk_smp_affinity.sh > /tmp/virtio_blk_affinity.log 2>&1
/usr/local/qcloud/gpu/nv_gpu_conf.sh >/tmp/nv_gpu_conf.log 2>&1
/usr/local/qcloud/scripts/disable_rt_runtime_share.sh >/tmp/disable_rt_runtime_share.log 2>&1
route add -net 172.17.33.0/24 gw 10.0.4.1
不过这种方式存在一定的缺陷,因为这个脚本是系统启动后最后一个脚本,所以如果主机有挂载nfs的情况下(且添加的路由正好是关联nfs的),由于nfs在rc.local前面执行,所以会缺少路由导致无法挂载。
B:添加至/etc/sysconfig/static-routes (没有static-routes的话就手动建立一个这样的文件)
从系统启动的/etc/rc.d/init.d/network脚本可以看到其会调用static-routes,并执行相关操作
# 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
vi /etc/sysconfig/static-routes
any net 172.17.33.0/24 gw 10.0.4.1
相比而言,通常建议修改/etc/sysconfig/static-routes的方式来实现永久添加静态路由。文章来源:https://www.toymoban.com/news/detail-463357.html
**大家通过route add添加临时路由,再修改/etc/sysconfig/static-routes,这样无需重启网卡或服务,同时也保证了下次重启后路由仍存在**文章来源地址https://www.toymoban.com/news/detail-463357.html
到了这里,关于Linux添加永久静态路由的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!