Setting a default gateway 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.

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!

Author: admin on January 20, 2017
Category: Linux, Networking

Leave a Reply

Last articles