There are two commands which are useful either to add or delete route, those are route and ip. We will see how to change route using command route.
Route Synopsis
1 2 3 4 5 6 |
route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss M] [window W] [irtt I] [reject] [mod] [dyn] [reinstate] [[dev] If] route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] [metric N] [[dev] If] route [-V] [--version] [-h] [--help] |
Adding route
1 |
sudo route add -net 192.168.3.0 gw 192.168.1.1 netmask 255.255.255.0 dev eth0 |
Deleting route
1 |
sudo route del -net 192.168.3.0 gw 192.168.1.1 netmask 255.255.255.0 dev eth0 |
A quick way to add default route
1 |
route add default gw 192.168.1.1 |
A quick way to delete defualt route
1 |
route del default gw 192.168.1.1 |
Can you tell how to delete a route based on interface name?
Destination Gateway Genmask Flags Metric Ref Use Iface
10.1.32.1 * 255.255.254.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
171.16.1.1 * 255.255.0.0 U 0 0 0 docker0
default xmas-core1.c 0.0.0.0 UG 0 0 0 eth0
Like, say I want to delete all with iface “eth0”
route / ip commands don’t support this
but you can simply use a shell script
#!/bin/bash
interface=eth0
interface_routes=$(sudo ip r | grep "dev $interface")
# backup routes
sudo ip r > route.bak
IFS="
"
for i in $interface_routes; do
sudo ip r del $i
done
and also route command is deprecated, use ip command.
https://dougvitale.wordpress.com/2011/12/21/deprecated-linux-networking-commands-and-their-replacements/#route