Setting static routes in Linux

As you’ve probably figured out by now, I’m a networking noob and I’m always trying new things to gain a better understanding of how networking works. Personally, I find it fascinating, but the deeper I dig, the the deeper the whole seems to go.

One issue is that most of my test devices (mostly Raspberry Pies) need to have 2 network adapters. 1 to communicate on my lab setup and another for me to access it through SSH, which is usually through the WiFi connection. This scenario actualy works quite well until I need to access something outside of my lab environment, but must also go THROUGH my lab environment. Since the eth0 adapter is statically set and the WiFi adapter is DHCP, the Pi sets the default route through WiFi, completely bypassing my lab environment. The only way to get around this, is to set the default route through the eth0 connection manually.

Update: 12/15/2021

I found it makes more sense to leave the default gateway alone and set static routes to access my lab.  This gives the best of both worlds… access to the Internet and access to my lab devices.  See “Setting a Static Route” below

Setting a Default Gateway

To start, see where your current route is going.

route -n

*-n says to show the IP of the gateway. If you leave it off, you’ll instead see the hostname of the device instead of the ip address.

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 wlan0
192.168.33.0    *               255.255.255.0   U     0      0        0 eth0

In the above example, it shows the default gateway going through 192.168.1.1 (my primary router), but instead, I need it to go through my internal network, 192.168.33.0.

Add the IP for the desired gateway

sudo route add default gw IP.ADD.RE.SS

*Replace ‘IP.ADD.RE.SS’ with the IP address of the router you want to use. In my case, the actual command is:

sudo route add default gw 192.168.33.1

Check the new route

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.33.1    0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

Now you have 2 default gateways! This will likely cause problems (well, it did for me anyway) so next we need to remove the unwanted route.

sudo route del default gw 192.168.1.1

That’s it! Check the route one last time to verify the original route is gone.

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.33.1    0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

These values should only remain in place as long as the device is on. Rebooting the device should set everything back, or you can simply reverse the procedure by re-adding the old route and removing the new!

 

Setting a Static Route

Note: These settings will be lost if the device is rebooted.  More to come later.. maybe

In this scenario:

  • wlan0 is set to DHCP on my home’s 192.168.0/24 network
  • eth0 is statically set to 172.22.22.23
  • My Lab devices are all 10.0.0.0/8
  • The Default Gateway is set to 0.0.0.0/0 via wlan0
  • The connecting Lab Router has the following interface configs
    • int f0/0: 172.22.22.6/24
    • int f0/1: 10.0.0.2/24
  • Other routes exist in the Lab network, could be anything 10.0.0.0/8

To reach my lab router

sudo ip route add 172.22.0.0/16 via 172.22.22.6 dev eth0

To reach my lab network

sudo ip route add 10.0.0.0/8 via 172.22.22.6 dev eth0

That’s it!  Now I can have my Internet and Lab access at the same time!

Comments

So empty here ... leave a comment!

Leave a Reply

Sidebar