Linux Commands

How to Change the Default Interface in Linux?

Almost everything productive we can do in Linux requires us to have a network connection. Whether we are developing apps, installing software, scripting, sharing files, or even watching movies, we need a working network connection. Hence, the statement “I require a network connection” is simply an understatement. The only way to enable network connection on a machine is through a network interface.

A network interface is a device or a point of connection between a device and a private or public network. In most cases, a network interface is a physical card such as a wireless adapter, a network card, and such. However, this does not necessarily mean that a network interface should be a physical device. For example, a loopback adapter that is not physically visible is implemented by software and available on all devices.

This quick tutorial will show you how to set the default interface in Linux.

Method 1 – Turn Off Adapters

The simplest way to set your default network interface is by disabling all other interfaces. For example, in Linux, you can use the GUI network manager or use the terminal.

Suppose you have a wireless adapter and you wish to use the Ethernet adapter; in that case, you can bring down the wifi adapter using the command as:

$ sudo ifconfig wlan0 down

$ sudo ifconfig eth0 up

The above commands will shut down the wireless adapter and bring up the ethernet adapter.

That will force the system to switch to the available network.

NOTE: The above command requires sudo or root privileges with the net-tools package installed.

Method 2 – Use IP ROUTES

An unconventional method is to edit your routes and specify which devices to use as the default.

Start by using the command:

$ sudo ip route list

This command will show you the default gateway and the default interface. For example, below:

default via 192.168.0.1 dev wlan0 proto dhcp metric 100

169.254.0.0/16 dev wlan0 scope link metric 1000

192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.10 metric 100

In the above example, the default value is wlan0. To change this, we start by removing all the routes as

$ sudo ip route list

This removes the default interface. Here’s an output:

169.254.0.0/16 dev wlan0 scope link metric 1000

192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.10 metric 100

To set the default interface, add the route using the command:

$ sudo ip route add default via 192.168.0.1 dev eth0

Once executed successfully, you can list the default interface as:

$ sudo ip route list

default via 192.168.0.1 dev eth0

169.254.0.0/16 dev eth0 scope link metric 1000

192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.10 metric 100

Conclusion

That’s it for this one. A quick tutorial that shows you how to modify the IP routes to specify your default interfaces.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list