Using the route Command in Ubuntu
The route command in Ubuntu is used to display and adapt the IP routing table. This table determines the paths that network traffic use to reach its destination. The route
command is useful for network administrators to configure, troubleshoot, and optimize network routes.
The route command is part of the net-tools package, which may not be installed by default on newer distributions because it has been deprecated (new command is iproute2)
Installation
To install type in the terminal:
sudo apt install net-tools
Check routing table
To display the current routing table use the following:
route
or
route -n
Output:
root@ubuntu-d-2022q1:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.2.2 0.0.0.0 UG 100 0 0 enp0s3
10.0.2.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-15faf413e796
Add/Delete route
To add a route to a specfic network or host use the following:
sudo route add -net <network> netmask <netmask> gw <gateway> dev <interface>
For specific host:
sudo route add -host <host> gw <gateway> dev <interface>
# Example
sudo route add -net 192.168.10.0 netmask 255.255.255.0 gw 10.0.2.2 dev eth0
To delete a route:
sudo route del -host <host> gw <gateway> dev <interface>