Changing of the default GW depending of the port status

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have an Ubuntu based server with 4 Ethernet interfaces (eth0, eth1, eth2, eth3). Eth0 from network 192.168.0.0/24 is having a fixed IP address 192.168.0.19 and GW: 192.168.0.254 and eth1 to eth3 are in a bridge mode with br0 IP: 10.0.0.3, net: 10.0.0.0/24, gw: 10.0.0.254. The server is connected to the outside network either over eth0 or eth3 port. I want in case that eth0 is UP the server to have default GW from 192.168.0.0/24 network and if eth3 is UP to have default GW from 10.0.0.0/24 network. I have tried to add two routing tables /etc/iproute2/rt_tables:



# reserved values
#
255 local
254 main
253 default
0 unspec
#
# local
#
#1 inr.ruhep
2 rt1
3 rt2


and changed the /etc/network/interfaces file adding post-up and post-down rules as follows:



post-up ip route add 192.168.0.0/24 dev eth0 src 192.168.0.19 table rt2
post-up ip route add default via 192.168.0.254 dev eth0 table rt2
post-up ip rule add from 192.168.0.19/32 table rt2
post-up ip rule add to 192.168.0.19/32 table rt2
post-up ip r add default via 192.168.0.254 metric 101 dev eth0
post-down ip rule del from 0/0 to 0/0 table rt2
post-down ip rule del from 0/0 to 0/0 table rt2


and for eth3:



post-up ip route add 10.0.0.0/24 dev eth3 src 10.0.0.3 table rt1
post-up ip route add default via 10.0.0.254 dev eth3 table rt1
post-up ip rule add from 10.0.0.3/32 table rt1
post-up ip rule add to 10.0.0.3/32 table rt1
post-up ip r add default via 10.0.0.254 metric 100 dev eth3
post-down ip rule del from 0/0 to 0/0 table rt1
post-down ip rule del from 0/0 to 0/0 table rt1


The problem is that the Linux server isn't reading these rules from the interfaces config file and when I execute ip rule show it is showing me the rt1 and rt2 routing tables in its output. If I add these rules manually it was listing them correctly, but for some reason the automatic routing was not working correctly, and the device was not deleting the default gw and only adding the new one. I was mostly following another thread here: Is it possible to have multiple default gateways for outbound connections?. I know that I can write a bash script which will check the status of the two ports at regular intervals and then delete and add the appropriate routes but I think this is not so elegant solution since this script has to work always in the background and it will add some latency to the execution, which will be maximum the configured polling time. I really like the solution based on the post-up/post-down but I can't make it work correctly on my machine.







share|improve this question


























    up vote
    0
    down vote

    favorite












    I have an Ubuntu based server with 4 Ethernet interfaces (eth0, eth1, eth2, eth3). Eth0 from network 192.168.0.0/24 is having a fixed IP address 192.168.0.19 and GW: 192.168.0.254 and eth1 to eth3 are in a bridge mode with br0 IP: 10.0.0.3, net: 10.0.0.0/24, gw: 10.0.0.254. The server is connected to the outside network either over eth0 or eth3 port. I want in case that eth0 is UP the server to have default GW from 192.168.0.0/24 network and if eth3 is UP to have default GW from 10.0.0.0/24 network. I have tried to add two routing tables /etc/iproute2/rt_tables:



    # reserved values
    #
    255 local
    254 main
    253 default
    0 unspec
    #
    # local
    #
    #1 inr.ruhep
    2 rt1
    3 rt2


    and changed the /etc/network/interfaces file adding post-up and post-down rules as follows:



    post-up ip route add 192.168.0.0/24 dev eth0 src 192.168.0.19 table rt2
    post-up ip route add default via 192.168.0.254 dev eth0 table rt2
    post-up ip rule add from 192.168.0.19/32 table rt2
    post-up ip rule add to 192.168.0.19/32 table rt2
    post-up ip r add default via 192.168.0.254 metric 101 dev eth0
    post-down ip rule del from 0/0 to 0/0 table rt2
    post-down ip rule del from 0/0 to 0/0 table rt2


    and for eth3:



    post-up ip route add 10.0.0.0/24 dev eth3 src 10.0.0.3 table rt1
    post-up ip route add default via 10.0.0.254 dev eth3 table rt1
    post-up ip rule add from 10.0.0.3/32 table rt1
    post-up ip rule add to 10.0.0.3/32 table rt1
    post-up ip r add default via 10.0.0.254 metric 100 dev eth3
    post-down ip rule del from 0/0 to 0/0 table rt1
    post-down ip rule del from 0/0 to 0/0 table rt1


    The problem is that the Linux server isn't reading these rules from the interfaces config file and when I execute ip rule show it is showing me the rt1 and rt2 routing tables in its output. If I add these rules manually it was listing them correctly, but for some reason the automatic routing was not working correctly, and the device was not deleting the default gw and only adding the new one. I was mostly following another thread here: Is it possible to have multiple default gateways for outbound connections?. I know that I can write a bash script which will check the status of the two ports at regular intervals and then delete and add the appropriate routes but I think this is not so elegant solution since this script has to work always in the background and it will add some latency to the execution, which will be maximum the configured polling time. I really like the solution based on the post-up/post-down but I can't make it work correctly on my machine.







    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an Ubuntu based server with 4 Ethernet interfaces (eth0, eth1, eth2, eth3). Eth0 from network 192.168.0.0/24 is having a fixed IP address 192.168.0.19 and GW: 192.168.0.254 and eth1 to eth3 are in a bridge mode with br0 IP: 10.0.0.3, net: 10.0.0.0/24, gw: 10.0.0.254. The server is connected to the outside network either over eth0 or eth3 port. I want in case that eth0 is UP the server to have default GW from 192.168.0.0/24 network and if eth3 is UP to have default GW from 10.0.0.0/24 network. I have tried to add two routing tables /etc/iproute2/rt_tables:



      # reserved values
      #
      255 local
      254 main
      253 default
      0 unspec
      #
      # local
      #
      #1 inr.ruhep
      2 rt1
      3 rt2


      and changed the /etc/network/interfaces file adding post-up and post-down rules as follows:



      post-up ip route add 192.168.0.0/24 dev eth0 src 192.168.0.19 table rt2
      post-up ip route add default via 192.168.0.254 dev eth0 table rt2
      post-up ip rule add from 192.168.0.19/32 table rt2
      post-up ip rule add to 192.168.0.19/32 table rt2
      post-up ip r add default via 192.168.0.254 metric 101 dev eth0
      post-down ip rule del from 0/0 to 0/0 table rt2
      post-down ip rule del from 0/0 to 0/0 table rt2


      and for eth3:



      post-up ip route add 10.0.0.0/24 dev eth3 src 10.0.0.3 table rt1
      post-up ip route add default via 10.0.0.254 dev eth3 table rt1
      post-up ip rule add from 10.0.0.3/32 table rt1
      post-up ip rule add to 10.0.0.3/32 table rt1
      post-up ip r add default via 10.0.0.254 metric 100 dev eth3
      post-down ip rule del from 0/0 to 0/0 table rt1
      post-down ip rule del from 0/0 to 0/0 table rt1


      The problem is that the Linux server isn't reading these rules from the interfaces config file and when I execute ip rule show it is showing me the rt1 and rt2 routing tables in its output. If I add these rules manually it was listing them correctly, but for some reason the automatic routing was not working correctly, and the device was not deleting the default gw and only adding the new one. I was mostly following another thread here: Is it possible to have multiple default gateways for outbound connections?. I know that I can write a bash script which will check the status of the two ports at regular intervals and then delete and add the appropriate routes but I think this is not so elegant solution since this script has to work always in the background and it will add some latency to the execution, which will be maximum the configured polling time. I really like the solution based on the post-up/post-down but I can't make it work correctly on my machine.







      share|improve this question














      I have an Ubuntu based server with 4 Ethernet interfaces (eth0, eth1, eth2, eth3). Eth0 from network 192.168.0.0/24 is having a fixed IP address 192.168.0.19 and GW: 192.168.0.254 and eth1 to eth3 are in a bridge mode with br0 IP: 10.0.0.3, net: 10.0.0.0/24, gw: 10.0.0.254. The server is connected to the outside network either over eth0 or eth3 port. I want in case that eth0 is UP the server to have default GW from 192.168.0.0/24 network and if eth3 is UP to have default GW from 10.0.0.0/24 network. I have tried to add two routing tables /etc/iproute2/rt_tables:



      # reserved values
      #
      255 local
      254 main
      253 default
      0 unspec
      #
      # local
      #
      #1 inr.ruhep
      2 rt1
      3 rt2


      and changed the /etc/network/interfaces file adding post-up and post-down rules as follows:



      post-up ip route add 192.168.0.0/24 dev eth0 src 192.168.0.19 table rt2
      post-up ip route add default via 192.168.0.254 dev eth0 table rt2
      post-up ip rule add from 192.168.0.19/32 table rt2
      post-up ip rule add to 192.168.0.19/32 table rt2
      post-up ip r add default via 192.168.0.254 metric 101 dev eth0
      post-down ip rule del from 0/0 to 0/0 table rt2
      post-down ip rule del from 0/0 to 0/0 table rt2


      and for eth3:



      post-up ip route add 10.0.0.0/24 dev eth3 src 10.0.0.3 table rt1
      post-up ip route add default via 10.0.0.254 dev eth3 table rt1
      post-up ip rule add from 10.0.0.3/32 table rt1
      post-up ip rule add to 10.0.0.3/32 table rt1
      post-up ip r add default via 10.0.0.254 metric 100 dev eth3
      post-down ip rule del from 0/0 to 0/0 table rt1
      post-down ip rule del from 0/0 to 0/0 table rt1


      The problem is that the Linux server isn't reading these rules from the interfaces config file and when I execute ip rule show it is showing me the rt1 and rt2 routing tables in its output. If I add these rules manually it was listing them correctly, but for some reason the automatic routing was not working correctly, and the device was not deleting the default gw and only adding the new one. I was mostly following another thread here: Is it possible to have multiple default gateways for outbound connections?. I know that I can write a bash script which will check the status of the two ports at regular intervals and then delete and add the appropriate routes but I think this is not so elegant solution since this script has to work always in the background and it will add some latency to the execution, which will be maximum the configured polling time. I really like the solution based on the post-up/post-down but I can't make it work correctly on my machine.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 14 at 11:12









      Jeff Schaller

      31.2k846105




      31.2k846105










      asked Mar 14 at 11:07









      Georgе Stoyanov

      16515




      16515

























          active

          oldest

          votes











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430145%2fchanging-of-the-default-gw-depending-of-the-port-status%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430145%2fchanging-of-the-default-gw-depending-of-the-port-status%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay