There are two ways to permanently add static routes in Linux

Command to add a route:

1.route add

route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0  #添加一条静态路由  
route add default gw 192.168.0.1 #添加默认路由 route del -net 192.168.1.0 /24 gw 192.168.0.1 #删除一条路由 route -n #查看路由表 

2.ip ro add

ip ro add 192.56.76.0 /24 dev 192.168.0.1  #添加一条静态路由
ip ro add default via 192.168.0.1 dev eth0 #添加默认路由 ip ro del 192.168.1.0/24 #删除一条路由 ip route show #查看路由表 

Common parameters:

Add add route

Del delete route

Via gateway IP address

dev gateway exports physical device name

After routing restarts the server, it still takes effect:

1. Add in /etc/rc.local:

The routing command is added: directly copy the command operated on the command line to the file, save and exit.

2. Write in the / etc / sysconfig / static-routes file:

If the file does not exist, it is manually created and the format of the added content is:

Refer to the shell statement in the /etc/init.d/network file:

# 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 fi 

Then, if you want to add a static route, the command is:

route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0 

Then, add the format in the / etc / sysconfig / static-routes file as:

any net 192.56.76.0 netmask 255.255.255.0 dev eth0
或者
any net 192.56.76.0 netmask 255.255.255.0 gw 192.168.1.1

Comparison of adding static routes in two ways:

1.rc.local:

Restart the server to take effect;

Restart the network service, the static route will be invalid;

rc.local is the last script that runs after the system is started, so if there is a service requirement such as NFS that requires a network to mount, this method is not suitable;

2.static-routes:

Restart the server to take effect;

Restart the network service to take effect:

Suitable for services that require network requirements;

The method of adding static routes in the script is almost the same as rc.local:

In fact, this method is to write the script yourself, and set it to S at the beginning of /etc/rc3.d/.

S means start, number is sequence, K means stop.

Generally, startup is the startup sequence of the daemon in a certain mode.

The lower the number, the higher the startup sequence;

/etc/rc3.d is a text multi-user environment, which is the general production environment.

** If you need to add a static route, try to add the static route to the / etc / sysconfig / static-routes file. Avoid routing failures due to restarting network services, thus avoiding failures. **

Guess you like

Origin www.cnblogs.com/xzlive/p/12737300.html