Permanently add static routes under Linux

Source: Internet
Author: User

There are two ways to permanently add static routes under Linux:


To add a route command:

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 route del-net 19 2.168.1.0/24 GW 192.168.0.1# Delete a route route-n# view the 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 a route IP ro del 192.168.1.0/24 #删除一条路由ip Route show# View the routing table

Common parameters:

Add Add route

Del Delete route

Via Gateway egress IP Address

Dev Gateway Export Physical device name


The way to make way is still valid after restarting the server:

1, add inside the/etc/rc.local:

The Add route command is: Copy the command directly from the command line to the file and save the exit.

2, write inside the/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 -$args                  done                 fi 

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

There are two ways to add a 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.

Summarize:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.