Centos7 adds methods for static routing

  • 2020-05-17 07:31:21
  • OfStack

1. ip route displays and sets routes

1. Display the routing table


[root@centos7 ~]# ip route show 
default via 192.168.150.254 dev enp0s3 proto static metric 1024 192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110 

Too ugly, format 1 (shows the default gateway and LAN routing, the two lines of content are not common) :


[root@centos7 tmp]# ip route show|column -t 
default      via 192.168.150.254 dev  enp0s3 proto static metric 1024 192.168.150.0/24 dev enp0s3      proto kernel scope link  src   192.168.150.110

2. Add static routing


[root@centos7 ~]# ip route add 10.15.150.0/24 via 192.168.150.253 dev enp0s3 
[root@centos7 ~]# 
[root@centos7 ~]# ip route show|column -t 
default      via 192.168.150.254 dev  enp0s3 proto static metric 1024 10.15.150.0/24  via 192.168.150.253 dev  enp0s3 proto static metric 1 192.168.150.0/24 dev enp0s3      proto kernel scope link  src   192.168.150.110 
[root@centos7 ~]# 
[root@centos7 ~]# ping 10.15.150.1 
PING 10.15.150.1 (10.15.150.1) 56(84) bytes of data. 64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.77 ms 64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08 ms 64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57 ms 
^C

3. Delete static routing

Simply replace add with del, or simply write only the target network


[root@centos7 ~]# ip route del 10.15.150.0/24

2. Set up permanent static routing

1. Add permanent static routing

Changes to the ip route directive to the routing cannot be saved and are gone by restart. It is also futile to write the ip route directive to /etc/ rc.local.

The official website document of RHEL7 does not mention /etc/sysconfig/ static-routes, and this file has been invalid after testing;

The /etc/sysconfig/network configuration file only provides the global default gateway, with the same syntax as Centos6 1: GATEWAY= < ip address > ;

The permanent static route needs to be written to /etc/sysconfig/ network-scripts/route-interface file, for example, adding two static routes:


[root@centos7 ~]# vi /etc/sysconfig/network-scripts/route-enp0s310.15.150.0/24 via 192.168.150.253 dev enp0s3 10.25.250.0/24 via 192.168.150.253 dev enp0s3 

Restart the computer or restart the device enp0s3 to take effect.


[root@centos7 ~]# nmcli dev connect enp0s3 

1 directly connect the device once, if not successful, disconnect the device and then connect the device, pay attention to the two instructions must start running, otherwise,,,, you know.


[root@centos7 ~]# nmcli dev disconnect enp0s3 && nmcli dev connect enp0s3

2, clear the permanent static route

You can delete the ifcfg-enp0s3 file or comment out the corresponding static routing entries in the file and restart the computer.

For the modified static route to take effect immediately, you can only manually delete the static route entry using ip route del.

Two strange things happened during the experiment:

1) sometimes the route takes effect but is not displayed on ip route show. It must be displayed after restarting the computer, for reasons that are not immediately clear.

2) when there are multiple network CARDS, the default route seems to be random through a network card device. After checking all the connection profiles, we found that the default connection profile for nic 1, ifcfg-eth0, was set to GATEWAY0 (this setting overwrites the global default gateway defined by /etc/sysconfig/network), and the connection profile for nic 2, ifcfg-eth1, used dhcp and assigned a default gateway at startup. The two default gateways confused the computer. This is a common occurrence in test systems, where production system 1 generally does not let network CARDS use dhcp, or even when used, carefully assign default gateways to prevent collisions.

Other points to note:

1) multiple GATEWAY can be set in the connection configuration file ifcfg-*, the first one is GATEWAY0, then GATEWAY1, GATEWAY2... , the maximum effective tail;

2) if the global gateway must be defined in the /etc/sysconfig/network file, the connection profile ifcfg-* should not be set to GATEWAY, and the dhcp connection should be noted that the dhcp server should not define the default gateway.

3) after the file ifcfg-enp0s3 is renamed ifcfg-eth0, the file route-enp0s3 will also be renamed route-eth0.


Related articles: