Networking HowTos
Networking HowTos

Adding a Static Route in Linux

July 23, 2012 Linux

There may be times when you need to set up a static route in Linux to allow your PC to access a network that is not accessible via the default gateway route. An example of this would be if you had a inter office VPN. You would want your main internet traffic to go via a default gateway (your main cable/adsl router), and traffic destined for the subnet of the other office would go via the device managing the VPN connection, assuming its not the same device that is managing the main internet connection.
Setting up a static route in Linux:
Route command syntax:

route add -net <destination>/<prefix> gw <gateway>

Destination = The destination network address.
Prefix = The cidr notation/network prefix for the destination network. (eg. 24 for, 8 for, etc).
Gateway = The gateway/router ip address to route the traffic through.

# route add -net gw

Make sure you run the command as root, or using “sudo”.
This adds a route for the network, so that traffic destined for this network gets routed through The gateway address must be on on the same network subnet that you are on.
This route wont get saved automatically, so after rebooting it will be gone. To set it up to automatically add the route at start up, you will need to edit one of the interface config files, or start up scripts, depending on the Linux distribution used.
For example, you could add the route command into the “/etc/rc.local” script on Ubuntu, above the “exit 0” line, and the route will automatically get added on system boot up.
Test connectivity using the ping and traceroute commands to make sure the destination network is accessible and going via the correct route.

You Might Also Like