Linux static routes & fixing 'Error either to is duplicate, or gw is a garbage'

While working with Linux routing, it's not uncommon to encounter errors when adding static routes. A typical issue is the error message: "Error: either 'to' is duplicate, or 'gateway IP' is a garbage." This post details my experience with this error and how I resolved it.

Encounter with the Error

In my scenario, I was attempting to add a new route using the ip route command:

1[root@adlwest-nms1 ~]# ip route add 172.16.103.0/24 172.16.1.254 dev eth1
2Error: either "to" is duplicate, or "172.16.1.254" is a garbage.

After researching and experimenting, I realized the syntax I used was incorrect.

Understanding the Error

The error message can be broken down into two parts:

  1. "to is duplicate": This suggests that the route you are trying to add already exists.
  2. "gw is a garbage": This typically indicates a syntax error in the command, specifically related to the gateway address.

Correcting the Syntax

To address this issue, I changed the syntax of the command to explicitly define the gateway using the via keyword:

1[root@adlwest-nms1 ~]# ip route add 172.16.103.0/24 via 172.16.1.254

This command was accepted without errors. To verify that the route was added successfully, I used the ping command:

1[root@adlwest-nms1 ~]# ping 172.16.103.41
2PING 172.16.103.41 (172.16.103.41) 56(84) bytes of data.
364 bytes from 172.16.103.41: icmp_seq=1 ttl=62 time=1.04 ms
464 bytes from 172.16.103.41: icmp_seq=2 ttl=62 time=0.992 ms

Key Takeaways

  • Ensure the syntax of the ip route command is correct, particularly when specifying the gateway.
  • Use the via keyword to clearly indicate the gateway address.
  • Check for duplicate routes before adding a new one.

I hope this post helps others who encounter similar issues with Linux routing. Remember, often the solution lies in the details of the command syntax.