Linux: Setup static routes
Using the route command
Couple of examples using route
Route out eth1
Force traffic out on eth1 instead of eth0 (def gw), even though IP of system i'm accessing out on eth1 falls within the eth0 subnet
- Route tables before
[root@mgmt001 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.31.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.31.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ib0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ib0
239.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ib0
0.0.0.0 172.31.4.1 0.0.0.0 UG 0 0 0 eth0
- Add/Remove static route
# add a route
route add -net 172.31.3.0 netmask 255.255.255.0 dev eth1
# remove the route
route del -net 172.31.3.0 netmask 255.255.255.0 dev eth1- Route tables afterwards
[root@mgmt001 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.31.3.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 ###########################
172.31.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.31.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ib0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ib0
239.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ib0
0.0.0.0 172.31.4.1 0.0.0.0 UG 0 0 0 eth0Add default gateway
- Add a default gateway
route add default gw 192.168.1.254 eth0Add route to another network
- Note: You must be on the same network as the gateway give below
- Add a route to another network (e.g. rocks headnode to access ipmi modules from local host)
route add -net 172.16.0.0 netmask 255.255.0.0 gw 172.16.1.1