Analysis of two implementation methods for adding static routing in Linux

Analysis of two implementation methods for adding static routing in Linux

Command 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 a default route
route del -net 192.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 default route
ip ro del 192.168.1.0/24 #Delete a route 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

Let the route still take effect after restarting the server:

1. Add in /etc/rc.local:

The command to add a route is: directly copy the command in the command line operation to the file, save and exit.

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

If the file does not exist, create it manually and add the following content:

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 following format to 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 you restart the network service, the static route will become invalid;

rc.local is the last script to be run after the system is started, so if a service such as NFS requires a network connection to be mounted, 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 script method of adding static routing is similar to rc.local:

This method is actually to write your own script, put it at the beginning of /etc/rc3.d/ and set it to S.

S means start, the numbers are the sequence, and K means stop.

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

The smaller the number, the earlier the startup order;

/etc/rc3.d is a text multi-user environment, which is generally used in production environments.

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of routing configuration in Linux system with multiple network cards
  • How to use Crontab to access a certain routing address regularly in Linux
  • How to modify IP, DNS and routing command line configuration in Linux
  • A detailed introduction to Linux routing table
  • How to add permanent static routing in Linux
  • Building a Router with Linux
  • A LINUX soft router that supports hybrid multi-line access
  • Using Linux as a router in an enterprise network

<<:  Mysql database scheduled backup script sharing

>>:  React's component collaborative use implementation

Recommend

Web Design Tutorial (2): On Imitation and Plagiarism

<br />In the previous article, I introduced ...

Linux system to view CPU, machine model, memory and other information

During system maintenance, you may need to check ...

Will Update in a Mysql transaction lock the table?

Two cases: 1. With index 2. Without index Prerequ...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

Tutorial on installing MySQL 5.7.18 decompressed version on Windows

1. Installation process MySQL version: 5.7.18 1. ...

A brief discussion on Yahoo's 35 rules for front-end optimization

Abstract: Whether at work or in an interview, opt...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

A solution to the abnormal exit of Tomcat caused by semaphore

I'm playing with big data recently. A friend ...

Things to note when writing self-closing XHTML tags

The img tag in XHTML is so-called self-closing, w...

Detailed explanation of JavaScript onblur and onfocus events

In HTML pages, visual elements such as buttons an...