How to remove all default gateways

  • 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
  • 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

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM