rhel7 add static route

1. Use route command to add

//Routes added to hosts

[root@localhost ~]# route add -host 8.8.8.8 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
8.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
8.8.8.8         8.8.8.8         255.255.255.255 UGH   0      0        0 ens3
8.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

//Routes added to the network (why)

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

//Add 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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
8.8.8.8         8.8.8.8         255.255.255.255 UGH   0      0        0 ens3
8.8.8.8         0.0.0.0         255.255.255.255 UH    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

//Delete route

[root@localhost ~]# route del -host 8.8.8.8 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

//After the machine restarts or the network card restarts, the route fails

[root@localhost ~]# systemctl restart network
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

2. Use ip command to add and delete routes

[root@localhost ~]# ip route add default via 172.25.8.251 dev ens3
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.8.251    0.0.0.0         UG    0      0        0 ens3
0.0.0.0         172.25.8.250    0.0.0.0         UG    100    0        0 ens3
172.16.1.0      172.25.8.251    255.255.255.0   UG    0      0        0 ens3
172.25.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

3. How to set permanent route under linux

1) Added to the end of / etc/sysconfig/network is globally valid. (effective method)

Gateway = GW IP or gateway = GW dev

2) Add routes under / etc / sysconfig / network script / route interface (one file for each interface, if not, only routes for that interface can be added)
For example, add a default gateway to eth0:

vim /etc/sysconfig/network-scripts/ifcfg-eth0

#Add the following (you can omit dev eth0)
GATEWAY=X.X.X.X

#After saving and exiting
service network restart or systemctl restart network. 

Keywords: network Linux vim

Added by rash on Sat, 18 Apr 2020 18:23:51 +0300