banner



How To Add Route On Linux

In the 1st part of the IP Routing series, we learned the fundamentals of Linux IP Routing.

Route command is used to testify/manipulate the IP routing table. Information technology is primarily used to setup static routes to specific host or networks via an interface.

In this article nosotros volition run across how to manipulate the routing tables in Linux using road command.

Nosotros'll first explicate how routing is done with some basic route command examples, and so nosotros'll explain using a sample network architecture about how to setup routes in your network.

I. How Routing is Done?

ane. Display Existing Routes

route command by default volition evidence the details of the kernel routing table entries. In this example, the ip-address of the system where the route command is being executed is 192.168.1.157

$ route Kernel IP routing table Destination     Gateway         Genmask         Flags Metric Ref    Employ Iface 192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

The above command shows that if the destination is within the network range 192.168.1.0 – 192.168.one.255, then the gateway is *, which is 0.0.0.0.

When packets are sent within this IP range, and so the MAC address of the destination is found through ARP Protocol and the packet will be sent to the MAC address.

If you don't know what ARP is, you lot should first understand how ARP protocol works.

In guild to send packets to destination which is non within this ip range, the packets volition be forwarded to a default gateway, which decides farther routing for that packet. We will see this shortly.

Past default route command displays the host proper noun in its output. Nosotros can request it to display the numerical IP address using -n choice equally shown below.

$ road -due north Kernel IP routing tabular array Destination     Gateway         Genmask         Flags Metric Ref    Use Iface 192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0 0.0.0.0         192.168.1.10    0.0.0.0         UG    0      0        0 eth0

2. Calculation a Default Gateway

We can specify that the packets that are not inside the network has to be forwarded to a Gateway address.

The following route add command will set the default gateway every bit 192.168.1.x.

$ road add default gw 192.168.i.10

Now the road command will display the post-obit entries.

$ route Kernel IP routing tabular array Destination     Gateway         Genmask         Flags Metric Ref    Employ Iface 192.168.1.0     *               255.255.255.0   U     0      0        0 eth0 default         gateway.co.in   0.0.0.0         UG    0      0        0 eth0

At present we have just added a default gateway to our machine. To verify whether it is working properly, ping some external host (for example, google.com) to ship ICMP package.

$ ping www.google.com

The following is the sequences of evets that happens when the above ping control is executed.

  1. First information technology will query the DNS server to obtain the ip-address of google.com ( for case: 74.125.236.34 )
  2. The destination address ( 74.125.236.34 ) is non within the network range.
  3. And then, in Layer-3 (IP header) the DESTINATION IP will be set as "74.125.236.34".
  4. In Layer-two, the DESTINATION MAC accost will exist the filled in as the MAC address of the default gateway ( 192.168.ane.ten's MAC ). The MAC will be found past using ARP as described before.
  5. When the packet is sent out, the network switch ( which works on Layer-2 ), send the packet to the default gateway since the destination MAC is that of the gateway.
  6. Once the gateway receives the packet, based on its routing table, it will forrad the packets further.

The above 2 examples would have given a good idea about how routing is done within a network. Now we will see other command line options available with road command.

3. Listing Kernel'due south Routing Cache Information

Kernel maintains the routing cache information to route the packets faster. We can list the kernel'southward routing cache information by using the -C flag.

$ route -Cn Kernel IP routing cache Source          Destination     Gateway         Flags Metric Ref    Use Iface 192.168.i.157   192.168.1.51    192.168.1.51          0      0        1 eth0 192.168.1.157   74.125.236.69   192.168.1.x          0      0        0 eth0 . . .

4. Reject Routing to a Particular Host or Network

Sometimes we may desire to reject routing the packets to a particular host/network. To do that, add the following entry.

$ route add -host 192.168.1.51 reject

As you come across beneath, nosotros cannot access that detail host (i.e .51 host that we just rejected).

$ ping 192.168.1.51 connect: Network is unreachable

Still we tin withal access other hosts in the network (for example, .52 host is nevertheless attainable).

$ ping 192.168.1.53 PING 192.168.1.53 (192.168.1.53) 56(84) bytes of data. 64 bytes from 192.168.i.53: icmp_seq=1 ttl=64 time=7.77 ms

If you lot want to reject an entire network ( 192.168.one.1 – 192.168.1.255 ), then add the following entry.

$ route add -cyberspace 192.168.1.0 netmask 255.255.255.0 turn down

Now, you lot cannot access whatsoever of the host in that network (for case: .51, .52, .53, etc.)

$ ping 192.168.1.51 connect: Network is unreachable  $ ping 192.168.ane.52 connect: Network is unreachable  $ ping 192.168.1.53 connect: Network is unreachable

II. A Sample Network Architecture (to understand routing)

Let us use the following sample network architecture for the rest of the examples.

In the diagram below, we have ii individual networks ( 192.168.ane.0 and 192.168.three.0, with subnet mask of 255.255.255.0 ).

We also have a "GATEWAY" motorcar with iii network cards. 1st card is connected to 192.168.1.0, 2nd card is connected to 192.168.3.0, and the 3rd card is connected to the external world.

5. Brand 192.168.three.* Attainable from 192.168.1.*

Now we need to add together a routing entry such that we are able to ping 192.168.iii. series ip-addresses from 192.168.ane. series. The mutual betoken we take is the GATEWAY car.

So, on each machine in 192.168.one.* network a default gateway will be added as shown beneath.

$ route add default gw 192.168.i.x

Now when 192.168.1.1 pings 192.168.3.1, information technology volition become to the GATEWAY via 192.168.1.ten.

In GATEWAY, add the post-obit routing entry.

$ route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.three.10

Now all the packets addressed to 192.168.3.* network will be forwarded via the 192.168.3.ten interface, which then delivers the packets to the addressed machine.

vi. Make 192.168.1.* Attainable from 192.168.3.*

Information technology is very like to what nosotros did earlier.

So, on each automobile in 192.168.3.* network a default gateway will be added as shown below.

$ route add default gw 192.168.iii.10

In GATEWAY, add the following routing entry.

$ route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.10

Now 192.168.three.* machines can ping 192.168.1.* machines.

7. Allow Internet Access ( External Earth )

In the previous 2 example, we have interconnected the two different networks.

At present nosotros need to access the internet from these 2 different networks. For that, we can add a default routing ( when no routing dominion matches ) to the 125.250.60.59 which is connected to the external globe as follows.

$ route add default gw 125.250.60.59

This is how information technology works:

  1. Now when you try to access the internet (for example: ping google.com) from whatsoever of these machines (for example, from 192.168.3.2), the following is the sequence of events that happens.
  2. Since the destination (google.com) is not within iii.* series, it will be forwarded to GATEWAY via 3.10 interface
  3. In GATEWAY, it checks whether the destination is within 1.* range. In this case, information technology is non.
  4. It so checks whether the destination is within 2.* range. IN this example, information technology is not
  5. Finally, information technology takes the default route to forward the packets (i.e using the 125.250.60.59 interface, which is connected to the external earth).

How To Add Route On Linux,

Source: https://www.thegeekstuff.com/2012/04/route-examples/

Posted by: parkerthavercuris.blogspot.com

0 Response to "How To Add Route On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel