Wednesday, July 9, 2014

Linux Debian: HOWTO Add a Static Route

To add a route on a Linux Server (in our case, a Debian Server), you can run the following command on the command line:

Sintax:
route add -net $NET netmask $MASK gw $GATEWAY
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

To make this route persistent, we have to add it on the post-up command script on the file /etc/network/interfaces as follows:
post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

Example:
ximena@xdev:~$ cat /etc/network/interfaces 
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
        address 213.252.4.55
        netmask 255.255.255.128
        network 213.252.4.0
        broadcast 213.252.4.127
        gateway 213.252.4.33
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 213.252.1.1
        dns-search ipandmore.de
        post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254

Save and close the file and then restart the networking service.

How to verify that the route was added?

sudo route -n
or
ip route show