Powered by coANDco UK

Howto configure a Linux box to act as router

From How2s

IP capable nodes come in two flavors, host and routers. While hosts process IP packets that are originate from or are destined to the host itself, a router can also forward IP packets between other sources and destinations. To see if your machine is configured as a router or as a host you yould use the sysctl command:

# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward=0
#

The value 0 means that IP forwarding is off, i.e., the machine is acting as a normal host. To turn on IP forwarding you could do the following:

# sysctl net.ipv4.ip_forward=1
net.ipv4.ip_forward=1
#

If your router is a leaf router as the router R1 in the following figure, there it is not much more to do:

 -----------------------
|      Internet         |
 -----------------------
           |
           R0
           |.1
 ------------------------ 192.168.0.0/24
   |.7       |.11     |.14
   R1        H1       H2
   |.1
 ------------------------ 192.168.1.0/24
             |.57     |.18
             H3       H4

You will have to configure the IP addresses of both your interfaces and then add a route to your default router R0. The next figure shows the situation when your router R1 is one step up in the hierarchy:

 -----------------------
|      Internet         |
 -----------------------
           |
           R0
           |.1
 ------------------------ 192.168.0.0/24
   |.7       |.11     |.14
   R1        H1       H2
   |.1
 ------------------------ 192.168.1.0/24
   |.2       | .57    |.18
   R2        H3       H4
   |.1
 ------------------------ 192.168.2.0/24
             |.2      |.3
             H5       H6

To enable your router to forward packets destined for nodes "below" router R2 you will have to add a network specific route to your routing table:

#route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2

Check the routing table with the command "route -n". In this example we have only used static IP routing. This is a simple approach and appropriate for small IP networks. A problem with static routing is that manual configuration is error prone and cumbersome if networks are large. Furthermore, a change in the network topology will not be reflected in the routing tables before the corresponding entries are updated manually. A better alternative would be to use a dynamic routing protocol such as RIP, OSPF or ISIS. The simplest of these would be to run RIP on your routers, and it can be invoked by the routed command, see "man routed" for more information (you may have to install "routed").

Please check out our sponsor, thanks to whom How2s.org is FREE: