Table of Contents
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.
- 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:
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.
$ sudo ip route add <network/mask> via <gateway_IP>
Sample Output:
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:
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:
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:
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.
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:
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:
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.
$ 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:
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:
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