UnixServerAdmin

Server Administration & Management

How to add static route in linux

Static routes improves overall performance of your network (especially bandwidth saving). They are also useful in stub networks (i.e. there is only one link to the network).

Display Current Routing Table Using following command

# ip route show

Sample output:
10.10.10.0/24 dev eth1 proto kernel  scope link  src 10.10.10.3
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.3
default via 192.168.1.254 dev eth0

You can add static route using following command:

ip route add {NETWORK} via {IP} dev {DEVICE}

For example network 172.17.0.0/24 available via 192.168.1.254:

# ip route add 172.17.0.0/24 via 192.168.1.254 dev eth0

Alternatively, you can use old good route command:

# route add -net 192.168.55.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth0

The drawback of ‘ip’ or ‘route’ command is that, when Linux reboots it will forget static routes. So store them in configuration file. Static routing describes a system that does not implement adaptive routing. In these systems routes through a data network are described by fixed paths (statically). These routes are usually entered into the router by the system administrator. You need to open /etc/sysconfig/network-scripts/route-eth0 file to define static routes for eth0 interface:

# cat /etc/sysconfig/network-scripts/route-eth0

Sample Output:
GATEWAY0=192.168.1.254
NETMASK0=255.255.255.0
ADDRESS0=172.17.0.0

# service network restart

Verify new routing table:

# route -n

To make the routing information persistent, add the “route add” line as seen above into the /etc/rc.local file.

Advertisements

February 8, 2012 - Posted by | Tips & Tricks, Unix/Linux | , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: