Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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      Dont resolve names
        -e      Display other/more information
        -A inet Select address family

Thank you.

share|improve this question

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

ip route del 0/0

I believe that catches all default routes.

share|improve this answer
Thank you but it still removes only one default gateway, not all. – Muhammad Gelbana Dec 20 '12 at 19:48
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. – Muhammad Gelbana Dec 20 '12 at 19:54
This is modern linux only. – jordanm Dec 20 '12 at 20:19

To build up on @jsbillings's solution, repeat the command until it fails:

while ip route del default; do :; done
share|improve this answer

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.

share|improve this answer
up vote 1 down vote accepted

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 on the second time isa.

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

share|improve this answer

To remove all default gateways using ip

ip route flush 0/0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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