There are two ways to permanently add static routes under Linux:
One, add the route command 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 # 查看路由表
3. Common parameters:
Add Add route
del Delete route
Via Gateway egress IP Address
Dev Gateway Export Physical device name
Second, make way by restarting the server still effective: 1, in the/etc/rc.local add:
The Add route command is: Copy the command directly from the command line to the file and save the exit.
2. Write in/etc/sysconfig/static-routes file:
If the file does not exist, it is created manually and the Add content format is:
Refer to the shell statement inside 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 -$argsdonefi
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, in the/etc/sysconfig/static-routes file, add the format:
any -net 192.56.76.0 netmask 255.255.255.0 dev eth0
3, two ways to add static route comparison: (1), rc.local:
Restart the server to take effect;
If the network service is restarted, the static route fails;
Rc.local is the last script that runs after the system starts, so this is not appropriate if the service Requirements for NFS require a network to be mounted;
(2), Static-routes:
Restart the server to take effect;
Restart the network service to take effect:
Suitable for services that require network requirements;
The way the script adds static routes and rc.local is pretty much the same:
This method is actually written in the script itself, placed at the beginning of/etc/rc3.d/set to S.
s means start, number is Order, K means stop.
Generally, startup is the boot order of a daemon in a pattern.
The smaller the number, the more forward the order of the boot;
/ETC/RC3.D is a text multi-user environment, the general production environment is this environment.
The downside is that it fails after restarting the network.
Three, Summary:
If you need to add static routes, add static routes to the/etc/sysconfig/static-routes file as much as possible. Avoid failure by restarting the network service, which prevents the routing from failing.
Permanently add static routes under Linux