Home » Linux » Tips » Linux Set Default Internet Route

Linux: Set Which Network Connection You Get Internet From [Default 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         UG        0 0          0 eth0    U         0 0          0 wlan0

In the example above, the network address 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 and with the IP addresses you are using on your network.

ip route del default via dev eth0

ip route add default via 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.

Photo of author
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 Comment