Summary of CentOS methods for adding (permanent) static routes

Posted by tech0925 on Thu, 23 Dec 2021 23:03:25 +0100

Please visit the original link: https://sysin.org/blog/centos... , see the latest version. Original works, please keep the source for reprint.

Author: GC (at) sysin Org, home page: www.sysin.org

In daily use, the server has two IP addresses, configuration of two network cards, and access to different network segments. This is very common. However, we need to create additional routing entries to ensure that packets are forwarded through the correct gateway so that the interface can communicate normally.

The following tests passed in CentOS 7 and 8

1, Use the route command to join the temporary route. It will become invalid after restart

route command parameters:

add     Add route
del     Delete route
-net    Set the route to a network segment
-host   Set the route to a host
gw      Exit gateway IP address
dev     Exit gateway physical device name
# Route to host
route add -host 192.168.1.123 dev eth0
route add -host 192.168.1.123 gw 192.168.1.1

# Route to join the network
route add -net 192.168.1.123 netmask 255.255.255.0 eth0
route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1
route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1 eth1
route add -net 192.168.1.0/24 eth1

# Join default gateway
route add default gw 192.168.1.1

# Delete route
route del -host 192.168.1.11 dev eth0
route del -net 192.168.1.123 netmask 255.255.255.0
# View routing information
ip route
route -n

2, Method of adding permanent route in Linux

1. Default gateway

(1) Write ifcfg file (recommended)

vi /etc/sysconfig/network-scripts/ifcfg-eth0

When configuring the ip address, directly write the GATEWAY configuration to the ifcfg file. Form: GATEWAY = GW ip

Suitable for joining the default route

(2) Add it to the end of the file in / etc/sysconfig/network. The format is as follows:

Gateway = GW IP or gateway = GW dev

2. Write / etc / RC Loacl (not recommended)

(Note: CentOS 7 must execute chmod +x /etc/rc.d/rc.local to ensure that this script is executed at boot.)

The command mentioned above can be written to / etc / RC In the local file, you will actively add relevant routing settings when the system starts.

There is only one disadvantage of this method: suppose a system service, such as nfs service, is running RC. After starting the network service Before local, suppose you set up your own actively mounted nfs. Well, the link here is blocked. Will cause mount failure. Another is that if you restart the network server, the route will fail. At this time, you have to load the file again, but suppose you operate remotely? Therefore, this method is not recommended.

method:

Edit / etc / RC Local, which is added using the route command syntax

route add -net 192.168.3.0/24 dev eth0
route add -net 192.168.2.0/24 gw 192.168.3.254
route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.100 dev eth0

Modified file / etc / RC d/rc. Local file example

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -net 192.168.3.0/24 dev eth0
route add -net 192.168.2.0/24 gw 192.168.3.254
route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.100 dev eth0

3. Write the / etc / sysconfig / static routes file

By default, this file does not exist in the / etc/sysconifg folder. We need to create it manually. The call to this file is as follows:

cat /etc/init.d/network

    # Add non interface-specific static-routes.
    if [-f /etc/sysconfig/static-routes]; then
        if [-x /sbin/route]; then
            grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
                /sbin/route add -$args
            done
        else
            net_log $"Legacy static-route support not available: /sbin/route not found"
        fi
    fi

Add as follows:

vi /etc/sysconfig/static-routes
any net 192.168.1.0/24 gw 192.168.1.1
any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1
any host 10.19.190.11/32 gw 10.19.177.10
any host 10.19.190.12 gw 10.19.177.10

In this way, and RC Compared with local, it is more useful. For example, for nfs, the effective time of this route is when the network service network is started, while some other network related services are started after the network service is started successfully, so the network link can be ensured to be unobstructed. Also, suppose I restart the network service, and this script is called by the script in the network service startup. Therefore, it also increases the relevant routes set on its own initiative.

This method is invalid when CentOS 8 is installed by default.

In CentOS 8, nmcli is used to manage the network by default. You can install the traditional network through Yum install network scripts Service to restore the configuration of static routing in this way.

4. Create / etc / sysconfig / network scripts / route-eth0 (recommended)

# Create a file named route-eth0 in the ` / etc / sysconfig / network scripts / ` directory
vi /etc/sysconfig/network-scripts/route-eth0
# Add content in the following format to this file
192.168.1.0/24 via 192.168.0.1
# Restart the network and verify that it is valid
systemctl restart network

Topics: Linux CentOS