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

CentOS 6 uses Docker to deploy redis master-slave database operation example

This article describes how to use docker to deplo...

JavaScript to achieve custom scroll bar effect

In actual projects, the up and down scroll bars a...

How to prohibit vsftpd users from logging in through ssh

Preface vsftp is an easy-to-use and secure ftp se...

How to configure the Runner container in Docker

1. Create a runner container mk@mk-pc:~/Desktop$ ...

Some tips on website design

In fact, we have been hearing a lot about web des...

In-depth analysis of nginx+php-fpm service HTTP status code 502

One of our web projects has seen an increase in t...

A case study on MySQL optimization

1. Background A sql-killer process is set up on e...

How to configure environment variables in Linux environment

JDK download address: http://www.oracle.com/techn...

Notes on MySQL case sensitivity

Table of contents MySQL case sensitivity is contr...

Detailed explanation of Nginx status monitoring and log analysis

1. Nginx status monitoring Nginx provides a built...

Vue3.0 project construction and usage process

Table of contents 1. Project construction 2: Dire...

Web page CSS priority is explained in detail for you

Before talking about CSS priority, we need to und...

Navicat cannot create function solution sharing

The first time I wrote a MySQL FUNCTION, I kept g...