What is Routing and a Routing table in Linux?
Routing is the transfer of an IP packet from one point to another across the network. When you send someone an email, you’re actually transmitting a series of IP packets or datagrams from your system to the other person’s computer. The packets sent from your computer pass through several gateways or routers to get to the destination computer system. The same is true for all Internet protocols such as HTTP, IRC, FTP, etc.
In all Linux and UNIX systems, the information about how the IP packets should be routed is stored in a kernel structure. These structures are called routing tables. If you want your system to communicate with other computers, you may want to configure these routing tables. First, it is important to know how to view these routing tables on your Linux system.
In this article, we explain how to view the routing tables in Ubuntu using the following three common commands:
- The netstat command
- The route command
- The ip route command
We have run the commands and procedures mentioned in this article on a Ubuntu 22.04 LTS system.
We are using the Ubuntu command line, the Terminal, in order to run the above-mentioned commands. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
How to View Routing Table?
Method 1: Through the netstat command
The netstat command has always been a widely used method of printing routing table information in Linux. However, it is officially replaced by the ip route command. We are including it anyway as it is still an approach to retrieve the required information.
Here is how you can use this command:
$ netstat -rn
-r This flag is used to display the Kernel routing tables
-n This flag is used to display the numerical addresses
This is what the output indicates:
|Destination||This column indicates the destination network.|
|Gateway||This column indicates the defined gateway for the network. If you see an * in this column, it means that no forwarding gateway is needed for the specified network.|
|Genmask||This column indicates the netmask of the network.|
|Flags||The U output in this columns means that the route is up. The G output indicates that specified gateway should be used for this route. D stands for dynamically installed, M stands for modified, and R means reinstated.|
|MSS||This column indicates the default Maximum Segment Size(MSS) for TCP connections for this route.|
|Window||This column indicates the default window size for TCP connections over this route.|
|Irtt||This column indicates the Initial Round Trip Time for this route.|
|Iface||The Iface column shows the network interface. If you had more than one interface, you would see lo (for loopback), eth0 (first Ethernet device), and eth1 (for the second Ethernet device), and so on for the number of interfaces, you have installed.|
Method 2: Through the route command
The route command also falls under the category of once widely used but now obsolete command to view routing tables. The manual page of this command also mentions that the command is now replaced by the ip route command.
Through this command, you can view exactly the same information that you could, through the netstat command. Here is how you can use it:
$ route -n
-n This flag is used to display the numerical addresses only
Method 3: Through the ip route command
Last but not least, here is the most recommended way of printing routing table information in Linux. Here is how to use this command:
$ ip route
Though this information is not much reader-friendly as that of the previously mentioned commands, it is still enough for you to configure the router.
These were a few commands to view the routing table information in Ubuntu. Though the ip route command in not neat looking, it is still the most recommended way of looking up for relevant routing table information. Though the other commands are considered obsolete, they are sometimes really helpful in deriving what you need to extract.