Flipkart

Friday 10 March 2017

Network Routing in Linux (Centos&Red Hat) - It’s all Practical

Agenda:
To know what is Network Routing and to know HOW and WHERE routing in Linux/UNIX can be used.
What is a Network Routing:
Network Routing is a process on Linux and UNIX servers in which a system administrator configures or modifies a kernel structure called a routing table to define the network accessibility among the computers in two or more different networks.
In simple words,  suppose if I want to connect to a system which is in 172.168.1.1 network from 192.168.1.1 network. It is directly not possible to talk to a computer which is in a different network. Here comes the gateway concept. To connect to 172.168.1.1 network, we need to go and connect to its default gateway first which will route us further to the computers which are in its own network.
So, routing helps us to go and connect to the computers which are not in same network through gateways.
A routing table contains the information on how packets are to be forwarded among the computers in different networks.
How to see the present routing settings OR Routing Table on your Linux Server?
#route -n is the command which provides present routing details
Note: You must be ROOT user to be able to configure routs on your Server
#netstat -r was used earlier but now it’s completely obsolete program and the alternate for the same is # ip route
Example output:
[root@LinuxServer ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric      Ref     Use Iface
192.168.1.3     0.0.0.0         255.255.255.255 UH   0      0        0         usb0
x.x.x.x              0.0.0.0         255.255.255.0     U      0      0        0         eth2
172.1.1.1         0.0.0.0         255.255.252.0     U      0      0        0         eth10
172.0.0.0         0.0.0.0         255.255.230.0     U      0      0        0         eth3
0.0.0.0             x.x.x.x          0.0.0.0                 UG    0      0        0         eth2
[root@LinuxServer ~]#
It is necessary to know what each column is meant for in the above output to be able to configure further changes to your routing table as needed for you.
1.Destination⇒ This column represents the route from your Linux server to the target server. In this case, it is 192.168.1.13 in the first row.
2.Gateway⇒ This column has the information whether a gateway is used for the route specified. In this example, only the last row has used default gateway and rest of the routes do not a default gateway.
3.Genmask⇒ This column represents netmask of the interface for which route has been configured
Flags⇒ This column shows whether the route is up and whether Gateway was used for the connection. U->Up and G->Gateway
4.Iface⇒ This column has the interface name of our own Linux server through which a route has been established and supposed to work while connecting to other system in a different network.
Types of Network Routing:
There are two types of network routing. They are:
  1. Dynamic Routing
  2. Static Routing
Dynamic Routing:
A dynamic routing is generally used by an ISP who usually manages large number of network connections which may be difficult to discuss here.
Static Routing:
Static routing  is the one which we use in our enterprise networks.
 We have the commands #route and #ip route to add or delete the routs on your Linux server.
SCENARIO:
Let's take a scenario in which we need a routing change to be able to connect to a system from your Linux server.
Suppose the IP address of your server (server1) is 172.168.1.1 with netmask 255.255.255.0 on interface eth1.
And the server(server2) your aiming to connect has IP:192.168.1.1 with netmask 255.255.255.0 on interface eth1
Where can you find the above details? :)
1. #ifconfig -a is the command to see all the existing network interfaces on your Linux Server.
2. #ifconfig eth1 ==> is used to fetch the details only about eth1 interface.
Example:
#ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 01:89:A8:G8:4R:54
         inet addr:192.168.9.5  Bcast:192.168.99.255  Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:190458 errors:0 dropped:0 overruns:0 frame:0
         TX packets:86768 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:150
         RX bytes:30701269 (29.3 Mb)  TX bytes:7878926 (1.9 Mb)
          Interrupt:9 Base address:0x5000   
 So, I now have a goal to establish a route in such a way that I should be able to connect to my server server1 to server2.
==>Command is the below to add a static route for the same on server1:
#route add -net 192.168.1.0 netmask 255.255.255.0 dev eth1
In the above line why did I use 192.168.1.0 instead of 192.168.1.1? Because I would like to be able to connect not only the system with th IP:192.168.1.1 but all the systems in this network.
So, If you have only one system in the target network, you can specify single IP address as well.
==>If the gateway for the network 192.168.1.0 is not added already, we use the below command to add the same:
#route add default gw 192.168.1.20  
If I want to route the connection from server1 to server2, we use the below command:
#route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.20
==>We can also set the flags to allow or reject the connections using #route command as shown below:  
#route add -net 10.1.1.0 netmask 255.0.0.0 reject
With the above command, I restricted access to 10.1.* network from my server.
IMPORTANT POINTS:
1).So far I have added all the routes from server1 to server2 which means, we get access from server1 to server2 but vice versa does not work until you add appropriate routes in server2.  So, game should be both the ways always :)
2).The routs which we usually add using the route commands like above will be vanished after the host reboot. How to make them persistent?
To add a persistent static route in Red Hat and Centos Linux, create a file called route- in the /etc/sysconfig/network-scripts/ directory like below:
 The file /etc/sysconfig/network-scripts/route-eth0 looks like below  
# cat /etc/sysconfig/network-scripts/route-eth0
ADDRESS0=192.168.1.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.1.20
ADDRESS1=172.16.1.1
NETMASK1=255.255.255.0
GATEWAY1=172.16.1.10  
There are different formats to put text in this file, the one which I chose seems to be a meaningful one.
->Once the above file is created, a network restart is required using the below command to make the changes working.
#service network restart
I hope you take system downtime from business while running the above command on your production server :)
How to Delete the existing route entries ?
You can use the same route command with del argument.
Example:
#route del -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.20
**Do not forget to comment out the lines in the route-ethx file for permanent changes
 
HAPPY LINUX LEARNING :)
 
Related Posts from Blog:
Easy search from Google:  
Use the text in quotes shown below to get into my blog quickly:
"linuxunixdatabases blogger"

No comments:

Post a Comment