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). For example, each LAN (located at different offices) is connected to HQ IDC (Internet data center) using single T1/LL/Wan links.
For example under Red Hat/Fedora Linux you can add static router for eth0 network interface by editing /etc/sysconfig/network-scripts/route-eth0 file. Under Debian Linux add static route by editing /etc/network/interface file.
Task: Display Current Routing Table Using ip command
By using the ip command, you can setup and view static route. For example, to display current routing table you can type command:
# ip route show
Sample output:
192.168.2.0/24 dev eth1 proto kernel scope link src 192.168.2.1 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.2 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 192.168.55.0/24 available via 192.168.1.254:
# ip route add 192.168.55.0/24 via 192.168.1.254 dev eth1
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 eth1
Linux Persistence Routes
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
Red Hat (RHEL) / CentOS / Fedora Linux Persistence Static Routing
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=192.168.55.0 GATEWAY1=10.164.234.112 NETMASK1= 255.255.255.240 ADDRESS1=10.164.234.132
How do I define static routing for network 10.0.0.0/8 via 10.9.38.65 router?
Open /etc/sysconfig/network-scripts/route-eth0:
# vi /etc/sysconfig/network-scripts/route-eth0
Append following line:
10.0.0.0/8 via 10.9.38.65
Save and close the file. Restart networking:
# service network restart
Verify new routing table:
# route -n
Debian / Ubuntu Linux Persistence Static Routing
Open configuration file /etc/network/interfaces
# cat /etc/network/interfaces
Output:
auto eth0 iface eth0 inet static address 192.168.1.2 netmask 255.255.255.0 gateway 192.168.1.254 up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 down route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1
Debian / Ubuntu Linux static routing for two interfaces:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 10.9.38.76
netmask 255.255.255.240
network 10.9.38.64
broadcast 10.9.38.79
### static routing ###
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
auto eth1
iface eth1 inet static
address 204.186.149.140
netmask 255.255.255.240
network 204.186.149.128
broadcast 204.186.149.143
gateway 204.186.149.129
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 10.0.80.11 10.0.80.12
dns-search nixcraft.in
Updated for accuracy.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email this to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: Jan/22/2010





{ 19 comments… read them below or add one }
The explanation on static route is good.
but i found that there is also a file under /etc/sysconfig/network-scripts/static-route.
If this file is present then what is its use.
By default there is no file named route-eth0 under /etc/sysconfig/network-scripts/ .
If i have to add static route in /etc/sysconfig/network-scripts/static-route file then how to do that.
with regards
rohit
india
9433248393
Hi,
I’ve been trying to figure our how to add a static multicast route.
I know how to do it manually via the route command.
e.g.
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
which give you the following route.
Destination Gateway Genmask Flags Metric Ref Use Iface
224.0.0.0 * 240.0.0.0 0 0 0 eth 0
But i would like to add the route to route-eth0 so i don’t need to apply it manually everytime the server reboots.
I’ve seen an example format for the file as
192.168.0.0/24 via 152.3.182.5
But i don’t know what the equivalent is for the following route
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
What do i put after ‘via’ for the above multicast route.
eg.
224.0.0.0/4 via ???
Open /etc/rc.local:
vi /etc/rc.localAppend command:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0Reboot system:
rebootVerify route
route -nAnd yes you can use:
224.0.0.0/4 via YOUR-ROUTER-IP-ADDRESSFor example my subnet 10.0.0.0/8 routed via 10.5.123.1, so I have following line in my route-eth0
10.0.0.0/8 via 10.5.123.1GATEWAY0=192.168.55.0
NETMASK0=255.255.255.0
ADDRESS0=192.168.1.254
…shouldn’t that be
GATEWAY0=192.168.1.254
NETMASK0=255.255.255.0
ADDRESS0=192.168.55.0
?
thanks for the heads up!
Thanks for the info – why can’t they include info about the ./network-scripts/route-eth0 file in the man file for route? :-S
hi this is venkateshwarlu. iam working in sysadmin in media. heare is using dell linux server. i have give the public ip and private ip.
but not corctly understanding in
” static routing ”
regarding
venkateshwarlu
sysadmin
Static routing is some thing which are not ging to change. If there is one inbound port one outbound port every thing is static one.
It says you are defining a rule for a network to go outside world.
So it will never be broadcasted to search a path.
It will be fast and optimum bandwidth utilisation.
Anywat you if donot know how to write rules, can make a linux box as transparent routing.
Actually its:
224.0.0.0/4 via YOUR-LOCALHOST-IP-ADDRESS
Actually script recognize 2 formats of route- files. The older one is just continued command line pararameters for ‘ip route add’ command and new one use
ADDRESS[0-9]
GATEWAY[0-9]
NETMASK[0-9]
triples
The simplest way is to figure out what ‘ip’ command will do a trick and put it without ‘ip route add’ into /etc/sysconfig/network-scripts/route-eth0
i.e.
224.0.0.0/4 dev eth1
or
to multicast 224.0.0.0/4 dev eth1
192.168.55.0/24 via 192.168.1.254
by the way to see all you routes use
ip route list table all
command
for more information issue
ip route help
for example if you have a 2 internet lines and want to do load balance route then do not set default gateway in configuration, but add in one of the route files (let’s say /etc/sysconfig/network-scripts/route-ppp1) something like:
default dev ppp0 nexthop ppp1/etc/sysconfig/network-scripts/static-routes
eth0 host 1.2.3.4
eth0 net 192.168.2.0 netmask 255.255.255.0 gw 192.168.9.6
The first line provide a host route to an interface for proxy arp.
The second line provides a VPN gateway.
thank you!
Hi everyone,
Can you help me with this, how to configure Red Hat Linux to work with a load balancer router? I’m not familiar with the Red Hat Linux. Thanks.
Thank’s, is very pretty info.
thanks, bydefualt route-eth0 file will not be there, need to be created
I have a pptp connection and have added the following in /etc/sysconfig/network-scripts/route-ppp0
ADDRESS0=172.16.0.0
GATEWAY0=192.168.10.1
NETMASK0=255.255.0.0
ADDRESS1=192.168.0.251
GATEWAY1=192.168.10.50
NETMASK1=255.255.255.0
when the tunnel is established subnet 172.16.0.0/16 is reachable but not 192.168.0.251 as it tries to go out the def gw
192.168.0.251 become reachable if i manually add by (but looses route if pptp drops) :
route add 192.168.0.251 gw 192.168.10.1
I must be messing something up here….anyone can shed light pls ? :)
@Stan: create /etc/ppp/ip-up.d/route-traffic and add your route command. For example, I router 10.0.0.0/8 via ppp0 so my file is as follows:
HTH