Pages

Sunday, November 6, 2011

Configure static routing on linux


Static routing can be used to manually set the network gateway. This often happens when we want to access our ip public while actually it can also be accessed locally. So, instead of going through the internet clouds, it will be faster if we access it from LAN.


On RedHat/Fedora, we can add a static routing to eth0 by editing the file etc/sysconfig/network-scripts/route-eth0. While on Debian Linux, we can add a static routing by editing the file /etc/network/interfaces.
By using the command "ip", we can configure and view the static routing. For example, to display the current routing table we can type the command:

# ip route show

It will show as follow :
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.1
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.2
default via 192.168.1.254 dev eth0

We can add a static routing using the following command format :
ip route add {NETWORK} via {IP} dev {DEVICE}
eg for network 192.168.55.0/24 available via 192.168.1.254, use the following command:
# ip route add 192.168.55.0/24 via 192.168.1.254 dev eth1

Alternatively we can also use the following command :
# route add -net 192.168.55.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1

Persistence Routing
The weakness of the command 'ip' or 'route' above is when rebooted Linux does not store static routing earlier. So, we need to store it in the configuration file.

Red Hat Persistence Static Routing
For example to define static routing on network 10.0.0.0/8 via 10.9.38.65, do the following :
  1. Open the file /etc/sysconfig/network-scripts/route-eth0
  2. Add the following code
    10.0.0.0/8 via 10.9.38.65
    
  3. Save that file
  4. Restart network service using the following command :
    # service network restart
    
  5. check the new routing table using the following command :
    # route -n
    

Debian Persistence Static Routing
For example to define static routing on network 10.0.0.0 via 10.9.38.65, do the following :
  1. Open the file /etc/network/interfaces.
  2. Add the following line:
    auto eth0
    iface eth0 inet static
    address 10.9.38.76
    netmask 255.255.255.240
    network 10.9.38.64
    broadcast 10.9.38.79
    ### static routing ###
    post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
    pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
    

1 comment:

Don't Forget To Join Our Community
×
Widget