Linux: Set Which Network Connection You Get Internet From [Default Route]

Linux set default internet route

This quick tutorial will show you how to set which network Linux uses to access internet when multiple networks are connected by setting the default route.

If you have a Linux device (for example a Raspberry Pi) connected to multiple networks (like being connected to a wireless and wired network simultaneously), you might have trouble connecting to the internet.

That’s because Linux will have automatically set up some default routes, and will have somewhat arbitrarily picked which interface it tries to use to access the internet – even if there’s no internet available on that interface.

Forcing Linux/Raspberry Pi to Connect to the Internet via a Specific Network

Follow these steps to update your default route to use the correct network.

List Current Routes

You’ll need to know what the current routes are to issue the commands to modify them. Run:

netstat -rn

You’ll see something like the following:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 eth0
10.10.10.0     0.0.0.0         255.255.255.0    U         0 0          0 wlan0

In the example above, the network address 192.168.0.1 is the default route – the route Linux uses for any route which isn’t matched elsewhere in the routing table. It’s associated with the network on eth0 – the wired ethernet connection.

However, in our scenario the network on the second listed connection, wlan0, is where the internet router actually is. The default route must be altered to reflect this.

Issue the following commands to change the default route, replacing 192.168.0.1 and 10.10.10.1 with the IP addresses you are using on your network.

ip route del default via 192.168.0.1 dev eth0

ip route add default via 10.10.10.1 dev wlan0

In the first command, the default route is identified by the IP address and interface and then deleted.

In the second command, a new default route is defined, specifying the correct router IP address and network interface.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *