I'm running a custom built Linux machine, so not all Linux commands are available.
I execute network related commands, so I need to set a default gateway right before I run my command, then remove that gateway immediately afterward.

To do that I run all my commands in one line:

/sbin/route add default gw;my command;/sbin/route del default gw;

The problem is, for some reason I once found 2 default gateways on the same machine which caused all my commands to fail because even if I set my default gateway before running my test, it is still messed up and can't run my test.

So is there a way to remove ALL default gateways in one command ? I have a large amount of machines that are increasing and it won't be practical to plant a script on every machine. I need a command as simple as the following:

/sbin/route del all default;set my default gw;mycommand;/sbin/route del all default;

All I have found so far is a command to remove a default gateway but not all of them /sbin/route del default which won't work for me.

/sbin/route help displays the following

/sbin/route --help

Usage: route [{add|del|delete}]

Edit the kernel's routing tables

        -n      Don't resolve names
        -e      Display other/more information
        -A inet Select address family

If you use ip instead of route, you could simply run:

ip route del 0/0

I believe that catches all default routes.

  • Thank you but it still removes only one default gateway, not all. Dec 20 '12 at 19:48
  • 5
    Then perhaps you should iterate through all default routes, and remove them, instead of assuming that your first remove removes all of them. It sounds like your code is breaking somehow and adding more than one default route. This is not a normal circumstance.
    – jsbillings
    Dec 20 '12 at 19:50
  • I agree that my code breaks at some point but may be I can solve this quickly by fixing the GW thing instead of wasting a lot of time which I don't have. Thanks a lot for your help. Dec 20 '12 at 19:54
  • This is modern linux only.
    – jordanm
    Dec 20 '12 at 20:19

To remove all default gateways using ip

ip route flush 0/0

All the answers are great but I resolved this problem using a different approach, I used the command to add only one default gateway, but fail if there is already one. And thus eventually remove the wrong gateway at the end of the command. This should work the second time inshallah.

ip route add default via my-gateway
ip route del default

If your code breaks with multiple default gateways you should probably also look in multiple ip addresses per device.

As you received already an answer here is another approach as you should NEVER change the configuration of your system just because you want to run/test a script. How about using network namespaces instead. For a short introduction how to add virtual devices to your NS (namespace) have a look at http://code.google.com/p/coreemu/wiki/Namespaces.

To create a namespace named test and run a command under this namespace just run

ip netns add test
ip netns exec test COMMAND

where COMMAND can be for example your shell or just the server script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.