how to change default network interface in linux

how to change default network interface in linux,
how to change network interface priority in linux,

to install ifmetric in linux use this command

sudo apt-get install ifmetric

it happened when i want to change my wlan as default network interface in raspberry pi. i have two connection

  1. LAN connection = eth0
  2. Wifi connection = wlan0
pi@raspberrypi:~ $ ifconfig
eth0      Link encap:Ethernet 
          inet addr:10.2.1.166  Bcast:10.2.1.255  Mask:255.255.255.0
          inet6 addr: fe80::5df2:5b0c:8062:b551/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1194121 errors:0 dropped:77 overruns:0 frame:0
          TX packets:116003 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1428192778 (1.3 GiB)  TX bytes:10600602 (10.1 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:16656 (16.2 KiB)  TX bytes:16656 (16.2 KiB)

wlan0     Link encap:Ethernet 
          inet addr:192.168.43.120  Bcast:192.168.43.255  Mask:255.255.255.0
          inet6 addr: fe80::8751:b941:7c96:a41/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:35 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4143 (4.0 KiB)  TX bytes:5843 (5.7 KiB)

pi@raspberrypi:~ $
pi@raspberrypi:~ $
pi@raspberrypi:~ $
pi@raspberrypi:~ $ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
^Z
[2]+  Stopped                 ping 8.8.8.8

while it uses LAN connection as default connection i can’t reach the internet because it only connects internal network. In order to be able to connect internet i set tethering via my smartphone and let raspberry pi download what i need via this tethering connection.

using ifmetric, changing default network interface in linux is as simply as running 3 words command on your shell command.

ifmetric interface metric

NAME
ifmetric - An IPv4 route metrics manipulation tool

SYNOPSIS
ifmetric INTERFACE [METRIC]

DESCRIPTION
ifmetric  is  a  Linux  tool for setting the metrics of all IPv4 routes
attached to a given network interface at once.  This  may  be  used  to
change  the  priority of routing IPv4 traffic over the interface. Lower
metrics correlate with higher priorities.

ifmetric uses the Linux NETLINK interface  to  manipulate  the  routes.
Because  of  that  it  is  compatible  with routes created with the new
iproute2 utility.

as stated by above “man” page, Lower metrics correlate with higher priorities.

So if i want to set wlan0 as default connection (connection with higher priority). i can enter this command

pi@raspberrypi:~ $ sudo ifmetric eth0 10
pi@raspberrypi:~ $ sudo ifmetric wlan0

Now i can reach the internet via wlan0 connection

pi@raspberrypi:~ $ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=50 time=396 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=50 time=524 ms

Leave a Reply

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