Configure Static Routes In Debian or Red Hat Enterprise Linux

by LinuxTitli on April 13, 2006 · 30 comments

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:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 30 comments… read them below or add one }

1 Rohit Basu February 17, 2007

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

Reply

2 Darrell May 23, 2007

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 ???

Reply

3 Rockdalinux May 23, 2007

Open /etc/rc.local:
vi /etc/rc.local

Append command:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

Reboot system:
reboot
Verify route
route -n

Reply

4 Rockdalinux May 23, 2007

And yes you can use:
224.0.0.0/4 via YOUR-ROUTER-IP-ADDRESS

For 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.1

Reply

5 Irrelevant July 26, 2007

GATEWAY0=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

?

Reply

6 vivek July 26, 2007

thanks for the heads up!

Reply

7 Dude! September 20, 2007

Thanks for the info – why can’t they include info about the ./network-scripts/route-eth0 file in the man file for route? :-S

Reply

8 venkateshwarlu January 30, 2008

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

Reply

9 Rohit Basu February 7, 2008

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.

Reply

10 bselectron February 24, 2008

Actually its:

224.0.0.0/4 via YOUR-LOCALHOST-IP-ADDRESS

Reply

11 Proglot May 28, 2008

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

Reply

12 Proglot May 28, 2008

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

Reply

13 Stuart Gathman September 3, 2008

If i have to add static route in /etc/sysconfig/network-scripts/static-route file then how to do that.

/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.

Reply

14 sql September 17, 2008

thank you!

Reply

15 Alan January 5, 2009

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.

Reply

16 Catalinux September 8, 2009

Thank’s, is very pretty info.

Reply

17 Soundar December 28, 2009

thanks, bydefualt route-eth0 file will not be there, need to be created

Reply

18 Stan January 22, 2010

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 ? :)

Reply

19 Vivek Gite January 22, 2010

@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:

#!/bin/sh
route add -net 10.0.0.0/8 dev ppp0
Save and close the file. And set permissions:
chmod +x /etc/ppp/ip-up.d/route-traffic

HTH

Reply

20 john March 29, 2010

hey guys ,,
I have a problem, I’m trying to configure a Debian board (Olimex) to do the routing but all my trys failed even though it seems my network is connected well

I can ping from my pc the both interfaces which the board are connected to but the problem I can’t ping any IP outside ?!?!?!

anyone can help and let me know what missing ?!!!!!

Reply

21 Joel August 31, 2010

How command can I use to list the multicast coming out of my redhat server?

Reply

22 wiqar.hussain November 22, 2010

i like the redhat and i trying to learn the redhat window so pleas help me in this ok thanks

Reply

23 Zarg December 20, 2010

Hi
I would like to know how I can add permanently a dyndns host in my routing table(I have two network interfaces…). I have some hosts with fixed ip addresses in my /etc/sysconfig/network-scripts/route-eth0 that I have added with
1.2.3.4/32 via 192.168.1.1
but if I can not ad in this file
myhost.dyndns.org/32 via 192.168.1.1

Thank you

Reply

24 Stephen February 15, 2011

Can’t be done. Otherwise your host would have to resolve the dyndns name each time it routed a packet !!

Find out what range your ISP is allocating the dyndns host within and add a route for that.

Reply

25 Zarg March 24, 2011

I done it in a way. I have a crontab every minute do something like
ping -c1 my_adress.dyndns.org | grep from | cut -d’ ‘ -f 4 | cut -d’:’ -f1′ > /tmp/new_ip
diff /tmp/old_ip /tmp/new_IP && route del default gw 192.168.1.254 eth0 route add -net `echo /tmp/new_ip` -mask 255.255.255.0 &&cat /tmp/old_ip > /tmp/old_ip && route add default gw 192.168.1.254 eth0 route add -net `echo /tmp/new_ip` -mask 255.255.255.0

Reply

26 rockysyas March 24, 2011

If you read /etc/init.d/network (rhel),you can find this:
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep “^any” /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi
so, /etc/sysconfig/static-routes is your best chose!
vi /etc/sysconfig/static-routes
any -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.254
any -host 192.168.100.1 gw 192.168.1.254
Thanks!

Reply

27 Anthony March 24, 2011

does 224.0.0.0/4 via IP dev eth0 in the route-eth0 script route all mutlicast addresses? For example I need to multicast on 224 and 235 addresses …if not..how would I enable it in static routing in Red Hat Linux?

Reply

28 Barry July 22, 2011

Hi,
My server ABC has got two nics connected to different subnets. How do I add static route to send traffic to particular interface?

For example, I have 5 another servers with ip-address 10.216.98.XX and all have their own gateway because they all are at different locations. Now I want to ass route which sends/receive all traffic from these servers to eth0 on my ABC server.

Thanks in advance.

Reply

29 Barry July 22, 2011
30 Yoni November 27, 2011

Thx for the tutorial

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 11 + 9 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: