Configure Static Routes In Debian or Red Hat Enterprise Linux

by on April 13, 2006 · 36 comments· LAST UPDATED January 22, 2010

in , ,

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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 36 comments… read them below or add one }

1 Rohit Basu February 17, 2007 at 7:52 am

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 at 4:31 am

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 at 8:01 am

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 Aco August 26, 2013 at 7:53 pm

I find this method preferable because the syntax doesn’t chance. Same route command.

Reply

5 Rockdalinux May 23, 2007 at 8:19 am

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

6 Irrelevant July 26, 2007 at 12:13 pm

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

7 nixCraft July 26, 2007 at 1:24 pm

thanks for the heads up!

Reply

8 Dude! September 20, 2007 at 2:47 pm

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

9 venkateshwarlu January 30, 2008 at 11:14 am

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

10 Rohit Basu February 7, 2008 at 10:51 am

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

11 bselectron February 24, 2008 at 7:41 am

Actually its:

224.0.0.0/4 via YOUR-LOCALHOST-IP-ADDRESS

Reply

12 Proglot May 28, 2008 at 8:44 am

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

13 Proglot May 28, 2008 at 9:08 am

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

14 Stuart Gathman September 3, 2008 at 6:21 pm

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

15 sql September 17, 2008 at 10:17 am

thank you!

Reply

16 Alan January 5, 2009 at 5:51 am

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

17 Catalinux September 8, 2009 at 9:19 pm

Thank’s, is very pretty info.

Reply

18 Soundar December 28, 2009 at 7:01 am

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

Reply

19 Stan January 22, 2010 at 6:00 pm

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

20 nixCraft January 22, 2010 at 6:20 pm

@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

21 john March 29, 2010 at 2:04 pm

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

22 Joel August 31, 2010 at 2:56 pm

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

Reply

23 wiqar.hussain November 22, 2010 at 12:53 am

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

Reply

24 Zarg December 20, 2010 at 5:44 pm

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

25 Stephen February 15, 2011 at 8:25 am

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

26 Zarg March 24, 2011 at 9:14 pm

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

27 rockysyas March 24, 2011 at 3:40 am

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

28 Anthony March 24, 2011 at 11:15 pm

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

29 Barry July 22, 2011 at 3:12 am

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

30 Barry July 22, 2011 at 7:14 am
31 Yoni November 27, 2011 at 9:38 am

Thx for the tutorial

Reply

32 James July 23, 2012 at 1:14 pm

I have a problem with my static routes.

I have a route to a 10.0.0.0/8 network through both of my NICs which are configured for seperate /24 networks.

My eth0 has a default gateway. So I need to add the static route for Eth1.

10.0.0.0/8 via 172.21.32.3

I restart network services and see the route in netstat -nr.

However, when I unplug the Eth1 cable, the route is STILL IN THE TABLE. Which means the packets will not go out Eth1 even when there is a route. When I issue the ifdown eth1 command, finally the route is removed from the routing table and pings can go out eth0.

How can I get this to happen automagically?

James

Reply

33 R. Sriram September 5, 2012 at 8:12 am

Hi,

I have 3 servers in cluster and want to update the ntp server. I am running Red Hat on all the servers.
Do I need to stop the cluster, update the ntp server and start the ntpd service in each server and then bring the cluster up or I can update the ntp server with out disturbing the cluster? Would really appreciate some inputs.

Rgds
Ram

Reply

34 bahram October 10, 2012 at 8:52 am

hi I went help to config centOS 0.4 to Linux routing for network WAN

Reply

35 Ap.Muthu June 28, 2013 at 10:40 am

It would be useful to stick with the listed subnets for the example.
All possibilities are in vain:

# ip route show
192.168.70.0/24 dev eth0 proto kernel scope link src 192.168.70.1
192.168.14.0/24 dev eth1 proto kernel scope link src 192.168.14.162
169.254.0.0/16 dev eth0 scope link
default via 192.168.14.1 dev eth1

# ip route add 192.168.70.0/24 via 192.168.14.1 dev eth1
RTNETLINK answers: File exists

# ip route add 192.168.70.0/24 via 192.168.14.1 dev eth0
RTNETLINK answers: Network is unreachable

# ip route add 192.168.14.0/24 via 192.168.70.1 dev eth0
RTNETLINK answers: File exists

# ip route add 192.168.14.0/24 via 192.168.70.1 dev eth1
RTNETLINK answers: Network is unreachable

# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.70.0 * 255.255.255.0 U 0 0 0 eth0
192.168.14.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.14.1 0.0.0.0 UG 0 0 0 eth1

Reply

36 sinergiasincontrol September 30, 2013 at 4:03 pm

on debian, the config can be also on

/etc/network/if-up.d/
/etc/network/if-down.d/

… and so on.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: