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:
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.
- -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:
It gives lists if the network connection is it Transmission Control Protocol, Printers, LAN, Routing tables, interfaces multicasts, etc.
The route is given below:
ip route show or ip route list
This will show the IP routing and lists. This will give the same result:
route add default gw 188.8.131.52 eth1
It will route all the traffic via 192.168.1.254 gateway connected through eth1 network interface.
ip route add 184.108.40.206/25 dev eth1
It will route all the traffic through 220.127.116.11 gateway connected through the eth1 network interface.
As we know kernel is maintaining route cache information to route the packet speedily by using the -C option.
route add -host 18.104.22.168 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 :
Output: Network is unreachable
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.
route add default gw 22.214.171.124
Now, when 126.96.36.199 ping 188.8.131.52 it will redirect to GATEWAY through 192.168.1.10. In the GATEWAY we need to add the below-mentioned routing entry.
route add -net 184.108.40.206 netmask 255.255.254.0 gw 220.127.116.11
All the packet was addressed to 192.167.3.* network will be directed through the 18.104.22.168 gateway interface which will then deliver the packets to the destination machine.
- 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.
- 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.
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 –