Linux provides the “route add” command in order to add new network routes. The route add command can be used to add a new gateway to access other networks and the internet. Also, explicit routes can be added to access other networks by specifying the first hop. During connection to a network or internet default routes are generally added automatically but route add can be used to add new routes.
route add Command Syntax
The route add command has the following syntax.
route add HOP NETWORK DEVICE
- HOP is an IP address in the directly connected network. This HOP IP address is used to access specified NETWORK.
- NETWORK is the network which is accesssed and route used for.
- DEVICE is the current system network interface name to access specified NETWORK.
Display Current Rotues
Before starting to use route add command to add new routes or default gateway listing current routes or routing table is good practice. The current routes or routing table can be listed with different commands. The “route -n” list current routing table.
route -n
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.10.2 0.0.0.0 UG 100 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
- Destination is the NETWORK which is accessed by using route.
- Gateway is the first HOP IP address used to access NETWORK.
- Genmask is the network mask used to specify NETWORK mask.
- Iface is the network interface used to access specified NETWORK.
Add Default Gateway
One of the most popular use cases for the route add command is adding a default gateway. The default gateway is used when the destination IP does not match any network in the routing table. The default gateway generally set by network service can be also set by using the route add command manually. In the following example, we set 192.168.1.1 as the default network or default gateway. The 192.168.1.0/24 network is connected to the device or network interface named eth0. Adding a route to the system is an administrative task and requires root privileges. The root privileges can be provided with the sudo command.
sudo route add default gw 192.168.1.254 eth0
Add New Route For A Specific Network
We can also add new route for a specific network with the route add command. We should specify the reuqires options and parameters like network, gateway, network interface etc.
sudo route add -net 10.0.0.0/8 gw 192.168.10.2 ens33
- -net 10.0.0.0/8 is used to set destination as network which network address is 10.0.0.0/8.
- gw 192.168.10.2 is used to set directly connected network first hop IP address where each packet for the destination network redirected to this gateway IP address. Gateway IP address should be directly connected to the local linux system if not it throws error.
- ens33 is the network interface or device name used to send packets which is connected to the first hop address or gateway network.
Add New Route For A Specific Host
The route add command can be also used to add route to the specific remote host. The -host option should be provided instead of the -net option and the parameter should be a single IP address not network address or network address range.
sudo route add -host 10.20.30.40 gw 192.168.10.2 ens33
Remove Existing Route
In some cases we may need to remove or delete existing route. Deleting a route is very same with the adding route. Just change the add command with the del command. So “route del” is used to remove existing route.
sudo route del -net 10.0.0.0/8 gw 192.168.10.2 ens33
An alternatively shorter command can be used to delete the existing route. Just the host or network address can be specified to remove from the routing table.
sudo route del -net 10.0.0.0/8