sábado, 2 de febrero de 2013

Configure Static Routes In Debian or Red Hat Enterprise Linux


by on April 13, 2006 · 33 comments· Last updated January 22, 2010
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

Example
route add -net 10.10.10.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
route add -host 10.10.1.1 netmask 255.255.255.0 gw 192.168.1.1 dev eth0
This adds the route immediatly to the Kernel IP routing table. To confirm the route has been successfully, simply type the "route" command with no arguements:


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
 
 
http://www.cyberciti.biz/tips/configuring-static-routes-in-debian-or-red-hat-linux-systems.html 
 
 

How to add a Permanent Static Route on Linux?

In a Linux system specifically on CentOS machine, most of the configurations relevant to networking, IP address and routes addition or manipulation is done and saved in /etc/sysconfig/network-scripts directory. Please go into the /etc/sysconfig/network-scripts/ directory and list the files just for your own understanding.

[root@pbx2 ~]# cd /etc/sysconfig/network-scripts/
[root@pbx2 network-scripts]# ls
ifcfg-eth0   ifdown-eth   ifdown-isdn   network-functions
ifcfg-lo     ifdown-ib    ifdown-post route-eth0
 
 
 so here you’d be able to see the files like above where the network information of our Linux server is stored.
As we discussed how to add a route on a Linux server but it was not static and the route’s information gets flushed away after rebooting the server. So we need to add a permanent route so that it remains static on every reboot or even we execute the restart command of the networking service. In order to add a permanent static route, we need to create a new file in /etc/sysconfig/network-scripts/ and have to store the route information in that file which can be done as:
 
[root@pbx2 ~]# echo ‘192.168.2.55/24 via 192.168.2.1’ >>  /etc/sysconfig
/network-scripts/route-eth0
 
Now in order to check, restart the networking service:

[root@pbx2 ~]# service network restart
  
 
In order to get more satisfaction, reboot the server once and check again the routes on the server with following command:

[root@pbx2 ~]# route –n

OR

[root@pbx2 ~]# netstat -r
 
The file should be looking like this:

[root@pbx2 network-scripts]# cat route-eth0 192.168.2.55/24  via 192.168.2.1

http://linuxzilla.com/add-static-route-in-linux/
 

______________________________________________________________________
 

CentOS Linux: Add Static Routing

by on November 11, 2013 · 0 comments· LAST UPDATED November 11, 2013
I am a new CentOS Linux sysadmin. How can I add static route On CentOS Enterprise Linux server running on HP amd64 server?

You can use any one of the following command line utility to add, delete, display, or manipulate the Linux kernel routing table on CentOS and friends:
Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsCentOS/RHEL
Estimated completion time10m
  1. ip command - A CentOS Linux command line tool to print / manipulate routing, devices, policy routing and tunnels.
  2. route command - Older command line utility to show or manipulate the Linux kernel routing table. I suggest that you use ip command instead of route command. This command exists for historical and compatibility reasons only.
You need to edit the following configuration files for static route configuration :
  1. /etc/sysconfig/network - Edit this file to set default gateway IP address.
  2. /etc/sysconfig/network-scripts/route-ethX - Edit this file to set additional static gateway IP address.

CentOS: Displaying current routing table

Type any one of the following command:
# netstat -nr
# route -n
# ip route list

Sample outputs:
Fig.01: ip command displaying current CentOS Linux routing table
Fig.01: ip command displaying current CentOS Linux routing table
Warning: It is important that you configure routing correctly over ssh based session; otherwise, you will be locked out due to wrong network configuration.

CentOS Linux add a default gateway

In this example, route all traffic via 192.168.1.254 gateway connected via eth0 network interface. The following command will set a default gateway for both internal and external network (if any):
# route add default gw 192.168.1.254 eth0
OR
# ip route add 192.168.1.0/24 dev eth0

How do I make routing changes persistent across CentOS Linux server reboots?

To set default gateway edit /etc/sysconfig/network as follows:
# cat /etc/sysconfig/network
Sample configuration file:
  NETWORKING=yes ## server name ## HOSTNAME=server1.cyberciti.biz ## Default route ## GATEWAY=192.168.1.254 NETWORKING_IPV6=yes IPV6_AUTOCONF=no  Save and close the file. Restart the networking service on CentOS Linux, type:
# service network restart
# ip route list

To verify new settings ping to the default gateway and external network:
# ping 192.168.1.254
# ping www.cyberciti.biz
# host google.com

CentOS Linux static routing config for eth1 interface

The following is a sample route-eth1 file. The default gateway set to 192.168.2.254, interface eth1. The static route is 10.10.29.65 for 10.0.0.0/8 network:
# cat /etc/sysconfig/network-scripts/route-eth1
Sample configurations:
default 192.168.2.254 dev eth1 10.0.0.0/8 via 10.10.29.65 dev eth1
Alert: For eth0 interface use /etc/sysconfig/network-scripts/route-eth0 file. Avoid setting duplicate default gateways; either use /etc/sysconfig/network or /etc/sysconfig/network-scripts/route-ethX file. Do not use both files to configure default gateways.

A note about GUI/TUI tool

If you don't have X windows GUI (gnome/kde desktop) installed on CentOS server / system, than type the following command at shell prompt. This method can only set the default gateway. It can not be used to set additional static routing for interface. Type the following command:
# system-config-network-tui &
Fig.01: CentOS command line network config tool (click to large)
Fig.01: CentOS command line network config tool (click to large)
Select your Ethernet card such as eth0 or eth1 and hit [Enter] or [F12] special key to configure IP properties for selected NIC:
Fig.02: Setting up IPv4 properties on CentOS including default gateway (click to large)
Fig.02: Setting up IPv4 properties on CentOS including default gateway (click to large)
You can obtain an IP address using DHCP or setup IP address manually. Once an IP address assigned, click on the Ok button to save the changes. See how to configure networking on CentOS / RHEL / Fedora Linux tutorial for more information.
 



 

1 comentario:

  1. rutas persistentes al reiniciar el servidor
    nano /etc/rc.local

    /sbin/route add -net 190.6.132.0 netmask 255.255.255.0 gw 192.168.176.

    dev eth0

    ResponderEliminar