Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Linux route command

News Routing Recommended Links Humor Etc

Routing is the process of forwarding a packet from one computer to another. It is based on an address in the message’s layer-3 header. But there’s more involved than just reading a layer-3 address, though.

The  Unix IP stack maintains a special database called a routing table. When  Unix forwards a packet, it first refers to the routing table to decided where to send the packet. If the host tries to connect to a host which is the same network it just sends packet to the destination (direct routing). If the target host is on another network than host's own, then it sends the data to the router (indirect routing). Often only one router is defined as the 'default gateway'.  In this case from the host’s point of view it outsources all the processing of "non-local" packets to this gateway. If there is no default gateway and the router doesn’t find an entry in its routing table that matches the destination network address, the router discards the packet.

The  Unix route command enables manual manipulation of the route table. The routing table contains a list of networks that the router knows it can send packets to, and states over which link to send the packets. These can be static routes, (entered by a system administrator) they can be default routes (a special static route to send unknown destination IP addresses to), or they can be dynamically discovered routes, found using a routing protocol. In case of dynamic routing routers talk to each other and share information about networks they are connected to. That sounds scary and it really is. It also creates the infinite possibilities of misunderstanding. At the same time, dynamic routing takes the building of the routing table out of your hands and puts it into the hands of specific routing protocol. Your job then becomes the monitoring of the routing tables to make sure that the routers are playing nicely with each other. See Routing for more details.

Route command gives the possibility to specify static routes.

The syntax of the command is Unix flavour spific:

These methods of changing the route table don't last accross reboots. Listed below are methods to make any of these changes permanent.

Checking the route table in Linux
netstat -rn
route
Adding and Removing a Network in Linux
route add -net 10.10.10.0/24 gw 192.168.0.1
route del -net 10.10.10.0/24 gw 192.168.0.1
Adding and Removing a specific host is Linux-flavour specific:
route add -host 10.10.10.45 gw 192.168.0.1
route del -host 10.10.10.45 gw 192.168.0.1
Adding a Default GW in Linux
route add default gw 192.168.0.1
route del default gw 192.168.0.1
Note: The old gw will still remain and may need to be taken out for the system to function properly.

Making addition permanent

Routes are made permanent in Red Hat Linux by adding routes to /etc/sysconfig/static-routes

In Suse: Yast -> Network Devices -> Network card -> Edit -> Routing

        Or edit /etc/sysconfig/network/routes if you prefer the CLI.
Notes:
  • This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Some amount of grammar and spelling errors should be expected.
  • The site contain some broken links as it develops like a living tree... Please try to use Google, Open directory, etc. to find a replacement link (see HOWTO search the WEB for details). We would appreciate if you can mail us a correct link.
Google Search
Open directory

Research Index


Old News

[Oct 20, 2008] Add Persistant Route - openSUSE Forums

Hello,

I want SuSE to automatically remember some routes I always have to feed it when it restarts. How do I do that?

example:
route add -net 134.135.2.0 gw 136.131.1.22 netmask 255.255.255.0 dev eth0


In MS DOS I can just "route add -p" for persistant. man route didn't help much (I loathe man pages - the way they are written is overly geeky and 87% of the time I haven't got a clue what they're on about. And they are so DULL to read).

Any help greatly appreciated!
Thanks.

 A: Yast -> Network Devices -> Network card -> Edit -> Routing

You can also edit /etc/sysconfig/network/routes if you prefer the CLI.

HOWTO add a static route - Gentoo Linux Wiki

What is a route

A route is a rule used by your kernel to determine how to get someplace on a network. This HOWTO covers IP routes (routes on an IP network) but there are other types of routable networks. Routes are stored in the Linux kernel are accessible for viewing and editing to users.

Viewing your routing table

The easiest way to view your routes is to use the command:

/sbin/route -n

The table looks something like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
127.0.0.0       127.0.0.1       255.0.0.0       UG    0      0        0 lo
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth1

The Destination is the network address for the routing entry, combined with the genmask (netmask) you can see what network is being routed to the Gateway listed. Please read the man page for route for more information on the meanings of these fields.

Adding a route

To add a route you must first know the network address of the network you wish to add, and the gateway to that network. In our case we are going to use the network 10.0.0.0 with a netmask of 255.255.255.0. We have a firewall on our network with an IP address of 192.168.1.50 that is the gateway to this 10.0.0.0 network.

To add the route manually we use the command:

route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.50

Our route table now looks like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        192.168.1.50    255.255.255.0   UG    0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
127.0.0.0       127.0.0.1       255.0.0.0       UG    0      0        0 lo
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth1

As you can see the network 10.0.0.0 with a Genmask of 255.255.255.0 shows up with a gateway of 192.168.1.50. But how do we make sure this happens on boot?

Adding a route to /etc/conf.d/net

When adding a route to your /etc/conf.d/net file you need one more piece of information, the interface you will be routing out. This will likely be eth0, but can be something else. The easiest way to tell what interface you will route out is to look at your routing table for the line coresponding to the network the gateway for your route is on. In our case the gateway was 192.168.1.50. That coresponds to the 192.168.1.0 255.255.255.0 network, which is on eth1.

We add this to the /etc/conf.d/net file to configure the route:

routes_eth1=(
   "10.0.0.0/24 via 192.168.1.50"
)

If you do not have a "gateway" line in your net config file you probably already have a routes_ethX entry. It will likely have something like "default gw 1.1.1.1" in it. You can have multiple lines in your routes list.

In our case the routes list would look like this (without a gateway line):

routes_eth1=(
   "10.0.0.0/24 via 192.168.1.50"
   "default via 192.168.1.1"
)

Recommended Links

Internal links

External links



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: August 09, 2009