How to add permanent static routes in Ubuntu
Static routing is the term used to refer to the manual method used to set up routing. An administrator enters routes into the router using configuration commands. This method has the advantage of being predictable, and simple to set up. It is easy to manage in small networks but does not scale well.
Advantages of Static Routes
- Easy to configure
- No routing protocol overhead
Disadvantages of Static Routes
- Network changes require manual reconfiguration
- Network outages cannot be automatically routed around
- Does not scale well in large networks.
Add a Static route using "route" command
route add [-net|-host] <IP/Net> netmask <Mask> gw <Gateway IP> dev <Int>
Example
route add -net 10.10.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
route add -host 10.10.1.1 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
This adds the route immediatly to the Kernel IP routing table. To confirm the route has been
successfully, simply type the "route" command with no arguements:
route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
localnet * 255.255.255.0 U 0 0 0 eth0
10.10.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
default 192.168.1.2 0.0.0.0 UG 100 0 0 eth0
Use "netstat -rn" to print the Kernel IP Routing table.
netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.10.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.1.2 0.0.0.0 UG 0 0 0 eth0
Setting A Persistent Static Route
To keep the Static Route persistent or you want to add the route entries to the network script files (not using the route command) then all you need to do is to edit the file
and the static routes in the following format:
up route add [-net|-host] <host/net>/<mask> gw <host/IP> dev <Interface>
Example
up route add -net 10.10.1.0/24 gw 192.168.1.1 dev eth1
Run "sudo cat /etc/network/interfaces" and the output should show something like this:
# 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
auto eth0
iface eth0 inet static
address 192.168.1.5
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
# dns-* options are implemented by the resolvconf package
dns-nameservers 192.168.1.8
dns-search mydomain.com
# Static route - switching VPN hardware w/ Remote Office
up route add -net 10.10.1.0/24 gw 192.168.1.1 dev eth1
For the change to /etc/network/interfaces to
take effect, restart the "networking" service as follows:
sudo /etc/init.d/networking restart
NOTE: If you added the route already using "route" then there is no need to restart the networking service because, the next time server is restarted this takes effect.
This command is also available for Windows with similar parameters. See here.