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 192.168.1.11 dev eth0
# route add –host 192.168.1.12 gw 192.168.1.1

//Routes added to the network
# route add –net 192.168.1.11 netmask 255.255.255.0 dev eth0
# route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 dev eth1

//Add default gateway
# route add default gw 192.168.2.1

//Delete route
# route del –host 192.168.1.11 dev eth0

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

//Add to
ip route add default via 172.16.10.2 dev eth0
ip route add 172.16.1.0/24 via 172.16.10.2 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 192.168.3.1 dev eth0 #192.168.3.1 is the gateway address of eth0 network card
10.211.6.0/24 via 192.168.3.1 dev eth0
10.0.0.0/8 via 10.212.52.1 dev eth1 #10.212.52.1 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
0.0.0.0/0 via 192.168.3.1 dev eth0
10.211.6.0/24 via 192.168.3.1 dev eth0
# cat /etc/sysconfig/network-scripts/route-eth1
10.0.0.0/8 via 10.212.52.1 dev eth1

B: Netmask (opportunism, loopholes)

# cat /etc/sysconfig/network-scripts/route-eth0
ADDRESS0=0.0.0.0
NETMASK0=0.0.0.0
GATEWAY0=192.168.3.1
ADDRESS1=10.211.6.0
NETMASK1=255.255.255.0
GATEWAY1=192.168.3.1

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
ADDRESS0=10.0.0.0
NETMASK0=255.0.0.0
GATEWAY0=10.212.52.1

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 192.168.3.1
any net 10.211.6.0/24 gw 192.168.3.1
any net 10.0.0.0 netmask 255.0.0.0 gw 10.212.52.1

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
  done
fi

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

route add -net 10.8.0.0 255.255.255.0 gw 192.168.199.2

 

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

Keywords: network

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