Linux: Setup static routes

From Define Wiki
Revision as of 11:49, 4 October 2012 by David (talk | contribs)
Jump to navigation Jump to search

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 eth0

Add default gateway

  • Add a default gateway
  route add default gw 192.168.1.254 eth0

Add 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)
david@lt-david:~$ sudo route add -net 172.16.0.0 netmask 255.255.0.0 gw 172.16.1.1
david@lt-david:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.0.3        0.0.0.0         UG    0      0        0 wlan0
10.0.0.0        0.0.0.0         255.0.0.0       U     2      0        0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 wlan0
172.16.0.0      172.16.1.1      255.255.0.0     UG    0      0        0 wlan0
172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 wlan0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0