Analysis of Two Implementation Methods of Adding Static Routing to Linux
- 2021-08-28 21:50:23
- OfStack
To add a route:
1.route add
route add-net 192.56. 76.0 netmask 255.255. 255.0 dev eth0 # Add a static route
route add default gw 192.168. 0.1 # Add default route
route del-net 192.168. 1.0/24 gw 192.168. 0.1 # Delete 1 route
route-n # View Routing Table
2.ip ro add
ip ro add 192.56. 76.0/24 dev 192.168. 0.1 # Add a static route
ip ro add default via 192.168. 0.1 dev eth0 # Add default route
ip ro del 192.168. 1.0/24 # Delete 1 Route ip route show # View Routing Table
Common parameters:
add Added Routing del Delete Route via Gateway Exit IP Address dev Gateway Exit Physical Device Name
Let the route still take effect after restarting the server:
1. In/etc/rc. local, add:
Add the routing command as follows: directly copy the command operated on the command line to the file, save and exit.
2. In the/etc/sysconfig/static-routes file, write:
If the file does not exist, it is created manually, and the added content format 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 1 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:
any net 192.56.76.0 netmask 255.255.255.0 dev eth0
Or
any net 192.56.76.0 netmask 255.255.255.0 gw 192.168.1.1
Comparison of two ways to add static routes:
1. rc. local:
Restart the server to take effect;
If the network service is restarted, the static route will fail;
rc. local is the last script to run after the system starts, so if there is a service requirement that needs network to mount like NFS, this method is not suitable;
2. static-routes:
Restart the server to take effect;
Restart network service takes effect:
Services suitable for network needs;
The script adds static routes in a way similar to rc. local:
In fact, this method also writes its own script, which is set to S at the beginning of/etc/rc3.d/.
S means start, the number is sequence, and K means stop.
1 is the startup sequence of daemons in a certain mode.
The smaller the number, the higher the starting sequence;
/etc/rc3. d is a text multi-user environment, which is used in all production environments.
** If you need to add static routes, try to add static routes to the/etc/sysconfig/static-routes file. Avoid routing failure caused by restarting network services, thus avoiding failure. **