Saturday, 4 February 2012

How to add Route in linux


To check the routing table in linux we can use

# netstat -nr 

# route -n 

# ip route show

How to Change Your system's Default Gateway

Your server needs to have a single default gateway. DHCP servers will automatically assign a default gateway to DHCP configured NICs, but NICs with configured static IP addresses will need to have a manually configured default gateway.

 # route add default gw 192.168.1.1 eth0 
                                                                     wlan0

 Adding Temporary Static Routes

Reference to the 192.168.0.0 network has to be preceded with a -net switch and the subnet mask and gateway values also have to be preceded by the netmask and gw switches respectively. 

# route add -net 192.168.0.0 netmask 255.0.0.0 gw 192.168.1.254  eth0

If you wanted to add a route to an individual server, then the "-host" switch would be used with no netmask value.

# route add -host 10.0.0.1 gw 192.168.1.254 eth0


A universal way of making this change persistent after a reboot would be to place this route add command in the file /etc/rc.d/rc.local, which is always run at the end of the booting process.


 Adding Permanent Static Routes

permanent static routes are added on a per interface basis in files located in the /etc/sysconfig/network-scripts directory. The filename format is route-interface-name so the filename for interface wlan0 would be route-wlan0.

 we would have to configure file /etc/sysconfig/network-scripts/route-wlan0 to look like this:
#
# File /etc/sysconfig/network-scripts/route-wlan0
10.0.0.0/8 via 192.168.1.254


How to Delete a Route 
# route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254 eth0

 the file /etc/sysconfig/network-scripts/route-wlan0 will also have to be updated so that when you reboot the server will not reinsert the route. Delete the line that reads:

10.0.0.0/8 via 192.168.1.254



Displaying the routing cache with ip route show cache

The routing cache is used by the kernel as a lookup table analogous to a quick reference card.Routes existing in the route cache are periodically expired.

# ip route show cache 192.168.100.17


Displaying statistics from the routing cache with ip -s route show cache

# ip -s route show cache 192.168.100.17



No comments:

Post a Comment