How to Edit Static Routes In A Linux Machine

Introduction

On any linux machine, if you add a route through the command line, after reboot or network restart the route will be deleted. There are couple of ways to keep the route after a reboot/network restart.

One of the ways is to create and edit /etc/sysconfig/static-routes :

 

Static route configuration for HOST

If you type in the following command:

# route add -host 192.168.12.1 gw 192.168.1.1

The equivilant static-route line will be:

any host 192.168.12.1 gw 192.168.1.1

 

Static route configuration for a network

If you type in the following command:

# route add -net 192.168.12.0/24 gw 192.168.1.1

The equivilant static-route line will be:

any net 192.168.12.0/24 gw 192.168.1.1

 

For FreeBSD you should do the following:

If you type in the following command:
# route add -net 192.168.1.1/24 192.168.1.1

The equivilant static-route line will be by editing /etc/rc.conf for example:

static_routes=”lan”

route_lan=”-net 192.168.12.0/24 192.168.1.1″

Remember to restart the network service after you’ve edited the file – to verify the route works.

# service network restart

or

# /etc/init.d/network restart

 

Enjoy!