CentOS routing settings

1, Common settings of routing table:

1. The route added with route command will fail after the machine restarts or the network card restarts. Methods:

//Routes added to hosts
# route add –host dev eth0
# route add –host gw

//Routes added to the network
# route add –net netmask dev eth0
# route add –net netmask gw
# route add –net dev eth1

//Add default gateway
# route add default gw

//Delete route
# route del –host dev eth0

2. You can also use ip commands to add and remove routes

//Add to
ip route add default via dev eth0
ip route add via dev eth0

3, query

# netstat -nr
# route -n
# ip route list
# ip route show

2, Set route table permanently:

Method 1: configure in the / etc/sysconfig/network configuration file (I tested it as if it was not feasible):

default via dev eth0 # is the gateway address of eth0 network card via dev eth0 via dev eth1 # is the gateway address of eth1 network card

Note: this configuration writing method also supports writing to the / etc / sysconfig / network scripts / route interface configuration file.

For details, please refer to redhat official documents.

Method 2: configure in / etc / sysconfig / network scripts / route - {interface} configuration file ({interface} is network interface, such as eth0)

Two configuration formats are supported here

A: Method mentioned in method 1

# cat /etc/sysconfig/network-scripts/route-eth0 via dev eth0 via dev eth0
# cat /etc/sysconfig/network-scripts/route-eth1 via dev eth1

B: Netmask (opportunism, loopholes)

# cat /etc/sysconfig/network-scripts/route-eth0

Where the network segment address and mask are all 0, which means all network segments, that is, the default route.  

# cat /etc/sysconfig/network-scripts/route-eth1

Netmask can also be found in redhat official documents.

Method 3: configure / etc / sysconfig / static routes (recommended)

# cat /etc/sysconfig/static-route
any net any gw
any net gw
any net netmask gw

Note: by default, this file does not exist in the host and needs to be created manually. net is the scope. Host can specify a machine separately.

The reason why this method can also be used is that the / etc/init.d/network startup script will call this file. The specific principle of calling part of the code is as follows:

# 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

Method 4: write in the startup script / etc/rc.local

route add -net gw


Change from: https://www.cnblogs.com/EasonJim/p/8428102.html

Keywords: network

Added by bellaso on Sun, 09 Feb 2020 20:46:27 +0200