As a Linux user, it is essential to understand how to add a default route to your system. A default route is a route that is used when no other specific route matches the destination IP address. In other words, it is the route that your system takes when it does not know where to send the traffic. In this article, we will cover how to add a default route in Linux and explain related concepts that can help you understand the process.
Before we dive into adding a default route, we need to ensure that we have the necessary tools installed on our Linux system. The most important tool we need is the
ip command-line utility. This utility is used to manage networking in Linux and is available by default on most Linux distributions.
To check if the
ip command is installed on your system, open a terminal and type:
If the command returns the version number, then the
ip command is installed on your system, and you can proceed with the guide. If not, you can install it using your distribution’s package manager.
Adding a Default Route
To add a default route in Linux, we use the
ip command with the
route subcommand. The syntax for adding a default route is as follows:
sudo ip route add default via <gateway_ip_address>
Let’s break down the command:
sudo: This command is used to run the subsequent command as the superuser. This is required as adding a route requires administrative privileges.
ip: This is the command used to manage networking in Linux.
route: This is the subcommand used to manage routing tables.
add: This argument tells the
routesubcommand that we want to add a new route.
default: This is the destination IP address for the default route. It tells the system that this route should be used when no other specific route matches the destination IP address.
via: This is the keyword used to specify the gateway IP address.
<gateway_ip_address>: This is the IP address of the gateway that will be used for the default route.
For example, if the IP address of your gateway is
192.168.1.1, you would run the following command:
sudo ip route add default via 192.168.1.1
This command adds a default route to your system, telling it to send all traffic to the gateway at
192.168.1.1 when no other specific route matches the destination IP address.
Persistent Default Route
The default route we added earlier is not persistent, which means that it will be lost after a system reboot. To make the default route persistent, we need to add it to the system’s routing table configuration file.
In most Linux distributions, the routing table configuration file is located at
/etc/network/interfaces. Open the file in a text editor using the following command:
sudo nano /etc/network/interfaces
Add the following line to the file:
up ip route add default via <gateway_ip_address>
<gateway_ip_address> with the IP address of your gateway.
Save and close the file, then restart the networking service using the following command:
sudo service networking restart
This will ensure that the default route is added to the routing table every time the system boots up.
In this article, we have covered how to add a default route in Linux. We have also explained related concepts such as the
ip command, routing tables, and persistent routes. By following the steps outlined in this guide, you can easily add a default route to your Linux system and ensure that your traffic is routed correctly.