How to check, add and delete routes in linux

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

[-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]
[-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] [metric N] [[dev] If]
[-V] [--version] [-h] [--help]

To check the routing table

Command: nestat -rn

$ netstat -rn
Kernel IP routing table
Destination  Gateway      Genmask        Flags MSS Window irtt Iface        UG    0   0      0    wlan0  U     0   0      0    wlan0

Adding route

sudo route add -net gw netmask dev eth0

Deleting route

sudo route del -net gw netmask dev eth0

A quick way to add default route

route add default gw

A  quick way to delete defualt route

route del default gw

3 thoughts on “How to check, add and delete routes in linux

  1. Warning: Attempt to read property "entry" on string in /var/app/current/wp-content/plugins/jetpack/modules/gravatar-hovercards.php on line 190 Warning: Trying to access array offset on value of type null in /var/app/current/wp-content/plugins/jetpack/modules/gravatar-hovercards.php on line 190 Warning: Attempt to read property "displayName" on null in /var/app/current/wp-content/plugins/jetpack/modules/gravatar-hovercards.php on line 191
    Rachael says:

    Can you tell how to delete a route based on interface name?

    Destination Gateway Genmask Flags Metric Ref Use Iface * U 0 0 0 eth0
    link-local * U 1002 0 0 eth0 * U 0 0 0 docker0
    default xmas-core1.c UG 0 0 0 eth0

    Like, say I want to delete all with iface “eth0”

    1. Akhil – India – DevOps Engineer with hands-on experience supporting, automating, and optimizing mission-critical deployments, leveraging configuration management, CI/CD, and DevOps processes.
      akhil says:

      route / ip commands don’t support this
      but you can simply use a shell script

      interface_routes=$(sudo ip r | grep “dev $interface”)
      # backup routes
      sudo ip r > route.bak

      for i in $interface_routes; do
      sudo ip r del $i

      and also route command is deprecated, use ip command.

Leave a Reply

Your email address will not be published. Required fields are marked *