Add static routes to Centos7

Source: Internet
Author: User

Add static routes to Centos7

1. ip route display and set Routing

1. display the route table

[root@centos7 ~]# ip route showdefault via 192.168.150.254 dev enp0s3  proto static  metric 1024192.168.150.0/24 dev enp0s3  proto kernel  scope link  src 192.168.150.110 

The format is too ugly. (the default gateway and LAN route are displayed. The two lines have no common content ):

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


2. Add a static route

[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 -tdefault           via  192.168.150.254  dev    enp0s3  proto  static  metric  102410.15.150.0/24    via  192.168.150.253  dev    enp0s3  proto  static  metric  1192.168.150.0/24  dev  enp0s3           proto  kernel  scope  link    src     192.168.150.110[root@centos7 ~]#[root@centos7 ~]# ping 10.15.150.1PING 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 ms64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08 ms64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57 ms^C


3. delete a static route

You only need to replace add with del, or simply write only to the target network.

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

2. Set permanent static routes

1. Add permanent static routes

The changes made by the ip route command to the route cannot be saved, so the restart will be gone. Writing the ip route command to/etc/rc. local is futile.

The/etc/sysconfig/static-routes file is not mentioned in the RHEL7 official website. It has been tested that this file is invalid;

The/etc/sysconfig/network configuration file only provides the global default GATEWAY. The syntax is the same as that of Centos6: GATEWAY = <ip address>;

Permanent static routes must be written to the/etc/sysconfig/network-scripts/route-interface file. For example, add two static routes:

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

Restart your computer or enp0s3 to activate the device.

[root@centos7 ~]# nmcli dev connect enp0s3

Generally, you can directly connect to the device once. If the device fails, disconnect the device before connecting to the device. Note that you must run the two commands together. Otherwise, you know.

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

2. Clear permanent static routes

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

To make the modified static route take effect immediately, you can only use ip route del to manually delete static route entries.

Two strange phenomena occurred during the experiment:

1) Sometimes the route takes effect, but it is not displayed in ip route show. It is certainly displayed after the computer is restarted. The reason is unknown for the moment.

2) When multiple NICs exist, the default route appears to be randomly routed through a NIC device. After checking all the connection profiles, we found that the default connection profile ifcfg-eth0 for the first Nic has set GATEWAY0 (which overwrites the global default gateway defined by/etc/sysconfig/network ), the ifcfg-eth1 for the second Nic's connection profile uses dhcp, which will also be assigned a default gateway at startup, and the two default gateways confuse the computer. This is a common phenomenon in the test system. The production system generally does not allow the NIC to use dhcp, or even if it is used, it will carefully allocate the default gateway to prevent conflicts.

Other notes:

1) You can set multiple gateways in the connection configuration file ifcfg-*. Generally, the first one is GATEWAY0, and then GATEWAY1, GATEWAY2..... The maximum tail number is valid;

2) If the global GATEWAY must be defined in the/etc/sysconfig/network file, do not set the GATEWAY when connecting to the configuration file ifcfg-*. do not define the default GATEWAY for dhcp connection.

3) ifcfg-enp0s3 files renamed to ifcfg-eth0, route-enp0s3 files also to be renamed to route-eth0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.