10+ ip route command examples in Linux [Cheat Sheet]

Introduction to ip route command

In Linux, ip command is used to show or manipulate routing, devices, policy routing and tunnels. ip route is a  part of ip command. It helps to manage the route entries in the kernel routing tables that keep information about paths to other networked nodes. The routes can be set for different IP addresses (destination). You can use ip route command to add, delete, change, and list the routes.

 

Types of route

Following are the different types of routes.

Advertisement
  • unicast:  It describes real paths to the destinations covered by the route prefix.
  • unreachable: Unreachable destinations. The packets are discarded and the ICMP message host unreachable is generated.
  • blackhole: Unreachable destinations. The packets are discarded silently.
  • prohibi: Unreachable destinations. The packets are discarded and the ICMP message communication administratively prohibited is generated.
  • local: The destinations are assigned to this host.
  • broadcast: The destinations are broadcast addresses. The packets are sent as link broadcasts.
  • throw: The packets are dropped and the ICMP message net unreachable is generated.
  • nat: A special nat route. The destinations covered by the prefix are considered dummy (or external) addresses and require translation to real (or internal) ones before forwarding.
  • anycast: The destinations are anycast addresses assigned to this host. They are not implemented.
  • multicast: A special type for multicast routing. It is not present in normal routing tables.

 

Syntax for ip route command

The syntax for ip route command is:

$ ip route <command>

Some important commands in ip route are:

  • add: to add a new route
  • delete: to delete a route
  • change: to change a route
  • list: to list the routes

 

Different examples to use ip route command

1. List current routing table using ip route command

You can use ip route list or ip route show command to list current routes or routing table.

$ ip route list 

OR

$ ip route show

Sample Output:

ip route command to list current routes

 

2. Add a new route using ip route command

The ip route add command is used to add a new route for the specified network range. You will need root privileges to execute the command.

Advertisement
$ sudo ip route add <network/mask> via <gateway_IP>

Sample Output:

ip route command to add a new route

You can also use ip route only to list the routing table.

 

3. Add a new default gateway route using ip route command

A default gateway is the IP address of the router that connects your host to remote networks. You can use the following command to add a new default gateway route.

$ sudo ip route add default via <gateway_IP>

Sample Output:

ip route command to add a new default gateway route

 

4. Add a new route to specific device with ip route command

The below command adds a new route specifying the network interface or network device as the gateway.

$ sudo ip route add <NETWORK/MASK> dev <DEVICE>

Sample Output:

ip route command to add a new route specifying device gateway

 

5. Delete a route using ip route command

You can use the ip route delete command to delete a route from the routing table.

$ sudo ip route delete <ROUTE>

Sample Output:

Advertisement

ip route command to delete a route

 

6. Modify an existing route using ip route command

You can change a route using the ip route change command.

$ sudo ip route change <ROUTE>

Sample Output:

For example, to change the default gateway route, you can use the below command.

ip route command to change a route

 

7. Clear routes with flush using ip route command

The ip route flush command clears a route from the routing table for a particular destination.

$ sudo ip route flush <NETWORK/MASK>

Sample Output:

ip route command to flush

 

8. Clear all the routes using ip route command

You can also use ip route flush command to remove all the routes from the routing table.

$ sudo ip route flush table main

Sample Output:

ip route command to remove all routes

 

9. Get a single route to a destination

The below command is used to get a single route to a destination address and prints its contents.

Advertisement
$ ip route get to destination

Sample Output:

deepak@ubuntu:~$ ip route get to 192.168.0.133
192.168.0.133 dev enp0s3 src 192.168.0.103 uid 1000
cache

 

10. Get a single route from the source

You can run the following command to get a single route from the source address.

$ ip route get destination from source

Sample Output:

deepak@ubuntu:~$ ip route get to 192.168.0.133 from 192.168.0.103
192.168.0.133 from 192.168.0.103 dev enp0s3 uid 1000
cache

 

11. List routes with given scope only

You can run the below command to list routes with the given scope only.

$ ip route list scope scope_value

Sample Output:

ip route command to list routes with given scope only

 

12. List routes for specified device only

The following command lists the routes with the specified device only.

$ ip route list dev name

Sample Output:

To view the routes with device name enp0s3, you can use:

ip route command to list route with specified device only

 

Conclusion

In this tutorial, we have learned how we can manage the routing table using the ip route command in the Linux system. You can add, delete, change, and list the routes in the routing table. If you still have any confusion, feel free to ask us in the comment section.

 

What’s Next

16 Linux ip command examples to configure network interfaces (cheatsheet)

 

Further Reading

man page for ip route command

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X