Linux添加永久静态路由

[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] []           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} []              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

  =Use -4, -6, '-A ' or '--'; 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的方式来实现永久添加静态路由。

**大家通过route add添加临时路由,再修改/etc/sysconfig/static-routes,这样无需重启网卡或服务,同时也保证了下次重启后路由仍存在**

你可能感兴趣的:(Linux,linux,运维,centos)