Updated March 23, 2023
Introduction to Linux Route Command
Routing is the process of sending IP packets from one source to the destination. For example whenever we send electronic mail to our relatives residing in another country or even across the same country/street then actually we are sending a series of IP packets/datagrams from our computer to relatives computer.
How Does Linux Route Command Work?
Considering the above example, when our computer sends the IP packets that contain information of mail to a gateway, then the gateway forwards this packet to other gateways/routers. This process will happen until it arrives at the relative’s computer and after that, she/he reads the message. All the processes involved in the above use different types of protocols like FTP, IRC, HTTP, and many more.
On Linux & UNIX, IP packets that contain information is stored in a type of kernel structure which is termed as a routing table. We can change this table while configuring our computer to communicate with another computer across a network.** Routing is done by the gateways or in common terms routers.
What is Routers?
It is a device that forwards the IP packets between two computers using different protocols. The router maintains a routing table.
Types of Routing
Types of routing are given below:
- Static Routing: It is a process where we have to add routes manually in the routing table. The usage of this type of routing is less.
- Dynamic Routing: In this automatic adding of the route as per the current state of the route present in the routing table. Dynamic routing uses a different protocol to discover the network destination and the path/route to reach the destination. Routing Information Protocol(RIP) & Open Shortest Path First(OSPF) are the best dynamic routing protocols.
- Default Routing: In this router is configured to send all the packet to a single gateway/router and it goes on till the next hop. Packets are forwarded to the router which is configured for default routing irrespective of any network packet. Such a router is also known as a stub router.
Different Commands of Linux Route
Below are the Linux route commands:
Command:
route Options
It is a command utility to view and manipulate IP routing tables i.e. linking between source and destinations. This command will work both in windows and Unix os.
Options:
- -A family: This is used for the address specified family
- -F: It operates on the kernel-based Forwarding Information Base(FIB) routing table, is the default.
- -C: It works on the kernel routing cache.
- -v: Performs verbose operations
- -n: It is used for numeric IP address instead of hostname. It is used to understand when the route to of nameserver disappears.
- -e: It displays the routing table in netstat format.
- del: It will delete the route.
- add: It will add a new route in the table
- -net: To determine when a target is a network.
- -host: When a target is a host.
- netmask NM: When we need to add a network route then netmask is used.
- gw Gw: this Option Will Route Packet via Gateway.
Note: The gateway first must be reachable. This means that we need to set up a static route to the gateway before. - metric M: It is used to set the metric field in the routing table to M.
Now let us understand all the route command through an example:
We can get the information from netstat program:
Command:
netstat -r
It gives lists if the network connection is it Transmission Control Protocol, Printers, LAN, Routing tables, interfaces multicasts, etc.
Output:
Command:
route
The route is given below:
Output:
Command:
ip route show or ip route list
This will show the IP routing and lists. This will give the same result:
Output:
Command:
route add default gw 192.167.1.254 eth1
It will route all the traffic via 192.168.1.254 gateway connected through eth1 network interface.
Command:
ip route add 192.167.1.0/25 dev eth1
It will route all the traffic through 192.167.1.254 gateway connected through the eth1 network interface.
Command:
route -Cn
As we know kernel is maintaining route cache information to route the packet speedily by using the -C option.
Output:
Command:
route add -host 192.167.2.52 reject
In case we want to reject a particular host or network to receive packets then we use to reject the command. After that we need to check whether that host/network is reachable or not :
Command:
ping 192.168.1.51
Output: Network is unreachable
Command:
Make 192.167.3.* Accessible from 192.167.1.*
Here we have to add routing information so that we are fully able to reach 192.168.3. series IP address from 192.167.1. series. The basic understanding is that we have a GATEWAY machine. So on every machine in 192.168.1.* network a default gateway will be added as mentioned below.
Command:
route add default gw 192.167.1.10
Now, when 192.167.1.1 ping 192.167.3.1 it will redirect to GATEWAY through 192.168.1.10. In the GATEWAY we need to add the below-mentioned routing entry.
Command:
route add -net 192.167.3.0 netmask 255.255.254.0 gw 192.167.3.10
All the packet was addressed to 192.167.3.* network will be directed through the 192.167.3.10 gateway interface which will then deliver the packets to the destination machine.
Merits
- It provides a connection between two or more different network buildings like Ethernet and token ring.
- It will choose the best path across the network using a dynamic routing algorithm.
- By creating a collision domain & broadcast domain, network traffic can be controlled.
Demerits
- Complicated Setup: As it requires Network Address Translation(NAT) and private IP to each computer via DHCP server.
- Data Overhead: As data is broken up into different pieces and send to destination in the form of IP packets, which consume some part of network bandwidth.
Recommended Articles
This is a guide to Linux Route Command. Here we discuss the introduction, working, types, different Linux route commands along with merits and demerits. You may also look at the following articles to learn more –