changing the priority of network interface

16,974

You have several possibility to change the default route.

Nice place for this kind of action is in /etc/network/if-down.d/ or /etc/network/if-up.d/

You can in this folder put some script and make action. For example put this script in /etc/ppp/if-up.d/ to make default route to ppp0 interface every time when go up, make script called script with execute permissions 755

sudo nano /etc/ppp/if-up.d/script

# Check for specific interface if desired
[ "$IFACE" != "ppp0" ] || exit 0
# Do something
sudo route add default dev ppp0

chmod 755 /etc/ppp/if-up.d/script

In second case wifi reconnect, make one more script aka script2

sudo nano /etc/network/if-up.d/script2

# Check for specific interface if desired
[ "$IFACE" != "wlan0" ] || exit 0
# Do something
sudo route add default dev ppp0

This will if wlan0 go up set default route to ppp0, if is ppp0 down route will not be changed from wlan0.

Try.

Edit 1

Based on internet research, for ppp0 interface you move script in /etc/ppp/if-up.d

Share:
16,974

Related videos on Youtube

Kumar
Author by

Kumar

Updated on September 18, 2022

Comments

  • Kumar
    Kumar almost 2 years

    I want my ppp0 connection to have higher priority over wlan0. I added following lines to /etc/network/interfaces file.

    iface wlan0 inet dhcp
        up ifmetric wlan0 20
    ----------------------------------------------------------------------------
    #route table after modifying
    $ route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.43.1    0.0.0.0         UG    20     0        0 wlan0
    169.254.0.0     0.0.0.0         255.255.0.0     U     20     0        0 wlan0
    192.168.43.0    0.0.0.0         255.255.255.0   U     20     0        0 wlan0
    220.224.141.145 0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
    

    As you can see, metric of wlan0 changed to 20, but still it is preferred over ppp0 as default gateway. When i checked man page of route, it was mentioned "metric was not used by recent kernels".

    I can change the route manually by deleting the unwanted gw and adding ppp0 as gw. But when wlan0 disconnects and reconnects, the routing table changed to default. How could i set my ppp0 as default gw permanently?

    • 2707974
      2707974 about 9 years
      adding ppp0as gw with command route add default dev ppp0 or on some different way?
    • Kumar
      Kumar about 9 years
      but it will change if wlan0 reconnects, or when you boot the system.
  • Kumar
    Kumar about 9 years
    what does [ "$IFACE" != "ppp0" ] || exit 0 this line represent?
  • 2707974
    2707974 about 9 years
    compare ifup and ifdown action against interface. If is script in if-up.d, check ifup action that belongs ppp0 interface and go to command.
  • Kumar
    Kumar about 9 years
    Scripts in if-up.d are not executing when ppp0 is up. I tried with sample shell with only this command "sudo touch /home/user/test.txt". No file is created for ppp0 (the script is not executing). But for wlan it is working
  • Kumar
    Kumar about 9 years
    I am using wvdial to start the pppd.
  • 2707974
    2707974 about 9 years
    Pelase, give me output from command ifconfig when wvdial is connected.
  • Kumar
    Kumar about 9 years
    ppp0 Link encap:Point-to-Point Protocol inet addr:115.241.58.163 P-t-P:220.224.141.145 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 RX packets:106 errors:0 dropped:0 overruns:0 frame:0 TX packets:108 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:26594 (26.5 KB) TX bytes:19725 (19.7 KB)
  • 2707974
    2707974 about 9 years
    Name on the interface is ppp0 script should be work. Script will be executed only when interface change state, in this case, from down to up.
  • Kumar
    Kumar about 9 years
    Couldn't get you. Interface changes stated from down to up, when we disconnect and connect again. I am doing that, but still the script is not executing.
  • 2707974
    2707974 about 9 years
    Are you set permision? chmod 755 /etc/network/if-up.d/script
  • Kumar
    Kumar about 9 years
    yes. The output of ls -l. -rwxr-xr-x 1 root root 45 May 22 16:21 test. By the way, script name is test
  • Kumar
    Kumar about 9 years
    I asked a question on this. askubuntu.com/questions/626953/…
  • 2707974
    2707974 about 9 years
    If you have usb modem try to change ppp0 with usb0