nixCraft Poll

Topics

Configure static routes in Debian or Red Hat Linux systems

Posted by Vivek Gite [Last updated: July 26, 2007]

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 connecting to HQ IDC (internet data center) using single T1/LL/Wan link.

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.

Use of ip command

By using the ip command, you can setup static route. For example, to display current routing table you can type command:# ip route showOutput:

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 eth1Alternatively, use old good route command:# route add -net 192.168.55.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1The drawback of 'ip' or 'route' command is that, when Linux reboots it will forget static routes. So store them in configuration file.


Red Hat/Fedora Linux Static route file

# cat /etc/sysconfig/network-scripts/route-eth0Output:

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


Debian Linux static routes file

# cat /etc/network/interfaceOutput:

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

Updated for accuracy.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Rohit Basu Says:

    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

  2. Darrell Says:

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

  3. Rockdalinux Says:

    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

  4. Rockdalinux Says:

    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

  5. Irrelevant Says:

    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

    ?

  6. vivek Says:

    thanks for the heads up!

  7. Dude! Says:

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

  8. venkateshwarlu Says:

    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

  9. Rohit Basu Says:

    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.

  10. bselectron Says:

    Actually its:

    224.0.0.0/4 via YOUR-LOCALHOST-IP-ADDRESS

  11. Proglot Says:

    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

  12. Proglot Says:

    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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.