How to add a static route in rhel7 and rhel7

Source: Internet
Author: User

How to add a static route in rhel7 and rhel7
1. Use the route command to add

// Route entry to the host

[root@localhost ~]# route add -host 8.8.8.8 dev ens3[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens38.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0[root@localhost ~]# route add -host 8.8.8.8 gw 8.8.8.8[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens38.8.8.8         8.8.8.8         255.255.255.255 UGH   0      0        0 ens38.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

// Route (why) added to the network)

route add –net 11.11.1.11 netmask 255.255.255.0 dev eth0route add –net 11.11.1.11 netmask 255.255.255.0 gw 11.11.1.1route add –net 11.11.1.0/24 dev eth1

// Add a default gateway

[root@localhost ~]# route add default gw 172.25.8.250[root@localhost ~]# route add default gw 172.25.8.251[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens30.0.0.0         172.25.8.250    0.0.0.0         UG    0      0        0 ens30.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens38.8.8.8         8.8.8.8         255.255.255.255 UGH   0      0        0 ens38.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

// Delete a route

[root@localhost ~]# route del -host 8.8.8.8 dev ens3[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens30.0.0.0         172.25.8.250    0.0.0.0         UG    0      0        0 ens30.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

// After the machine is restarted or the NIC is restarted, the route becomes invalid.

[root@localhost ~]# systemctl restart network[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
2. Use ip commands to add or delete routes
[root@localhost ~]# ip route add default via 172.25.8.251 dev ens3[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens30.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0[root@localhost ~]# ip route add 172.16.1.0/24 via 172.25.8.251 dev ens3[root@localhost ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens30.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3172.16.1.0      172.25.8.251    255.255.255.0   UG    0      0        0 ens3172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
3. How to set a permanent route in linux

(1) adding it to the end of/etc/sysconfig/network takes effect globally. (Effective method)

GATEWAY = gw-ip or GATEWAY = gw-dev

2) Add a route under/etc/sysconfig/network-script/route-interface (each interface has one file. If no file exists, you can only add a route for this interface)

For example, add a default gateway to eth0:

Vim/etc/sysconfig/network-scripts/ifcfg-eth0 # Add the following content (dev eth0 can be omitted) GATEWAY = X. x. x. X # Save and exit the service network restart or systemctl restart network.

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.