19

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 10.10.10.10;my command;/sbin/route del default gw 10.10.10.10;

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

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

5 Answers 5

22

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

ip route del 0/0

I believe that catches all default routes.

4
  • Thank you but it still removes only one default gateway, not all. Dec 20, 2012 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, 2012 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, 2012 at 19:54
  • This is modern linux only.
    – jordanm
    Dec 20, 2012 at 20:19
7

To remove all default gateways using ip

ip route flush 0/0
1
  • Note that for clarity you can also write this as ip route flush default.
    – larsks
    Aug 15, 2022 at 15:27
5

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
2

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.

0

It solves
https://github.com/hub4ops/vagrant/blob/main/Vagrantfile

Vagrant.configure("2") do |config|    
  config.vm.box = "ubuntu/trusty64"    
  config.vm.network "public_network", bridge: "Intel(R) Dual Band Wireless-AC 7265"    
  config.vm.provision "shell", inline: <<-SHELL    
    ip route del 0/0
    ip route add 0.0.0.0/0 via 192.168.1.1
    echo "nameserver 8.8.8.8" > /etc/resolv.conf
  SHELL
end
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .