Linux routing

Adding routes in Linux

Setting a route in Linux or Unix is just a matter of utilizing the route command.
To make routing modifications you have to be running as root or use sudo for super user privileges.
This article will show you how to use "route add" and "route del" to add and remove routing rules.
The Route command modifies the kernels IP routing tables.
Primarily used to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program.
When the add or del options are used, route modifies the routing tables.
Without these options, route displays the current contents of the routing tables.
The basic route functionality is:


1 route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss M]


2 [window W] [irtt I] [reject] [mod] [dyn] [reinstate] [[dev] If]

While this looks complicated I will show some examples here.

Display Routing Table With "route"

If you want to just see the routing table information simply type route:

1# route
2 Kernel IP routing table
3 Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
4 192.168.3.0   *        255.255.255.0  U   0   0    0 eth6
5 default     DD-WRT     0.0.0.0     UG  0   0    0 eth6

So looking at the first line of the output, we see my network is a Class C network (from the netmask) and all traffic goes through interface eth6.
The following line is the default gateway, identified by the flag G. All traffic is flowing through the router.

Add A New Route With "route add -net"


route add -net 10.12.76.0 netmask 255.255.255.0 dev eth0

This adds a route to the network 10.12.76.x via “eth0″.
The Class C netmask modifier is necessary here because 10.*.*.* is not a Class C IP address.
The word “dev” can be omitted here.

Add a New Route With "route add -host"

The -host states that the route is meant for host machine, rather than a specific network.
The syntax is otherwise the same.

route add -host 10.12.76.5 netmask 255.255.255.0 dev eth0

In this case our last octet must be a valid host, in this case the ".5".

Add A Default Gateway With "route add default gw"


route add default gw 192.168.5.0 dev eth4

This adds a default route (which will be used if no other route matches).
All packets using this route will be gatewayed through “eth4″.
For this rule to be inserted you have to have an interface with an IP address on that network.

Add Routing Reject Rule

To add a reject rule for a certain subnet:

route add -net 10.0.0.0 netmask 255.0.0.0 reject

This example shows how to install a rejecting route for the private network “10.x.x.x.”
So if any packets come in from 10.x.x.x they will be masked out prior to hitting the default route.

Remove A Routing Rule

With "route del" Now that we have all those routing rules in our routing table, how do you get rid of them?
Simply replace the route add with route del.
So for instance to remove the earlier addition route add -host 10.12.76.5 netmask 255.255.255.0 dev eth0 we can execute the command:


route del -host 10.12.76.5 netmask 255.255.255.0 dev eth0

Which will remove the entry from the routing table.
The same goes for default gateways that have been added.

admin has written 89 articles