Linux Commands

Add a Default Route on Linux

Any Linux user, especially a systems administrator, should have skills to make simple network configurations on a Linux system. For the system to communicate to the other computers on the internet, gateways must be set up and adjusted as needed.

What Will We Cover?

In this article, we will see how to add a default route on the Linux systems. Notably, different systems may have different configurations. However, we will focus on the Ubuntu 20.04 system.

What Is a Default Gateway?

To connect your home network to the wider internet, you need a default gateway. The function of a gateway is implied by its name; it connects a local area network to the wider internet.

All traffic that are not meant for the local area network and for which no alternate route is defined in the routing table are sent via the default gateway. Typically, a network’s default gateway is a specific router.

The wireless router serves as the “default gateway” in most home networks. It’s a way to connect to resources which are located on other networks that are far from your own. Internet navigation would be considerably more difficult without it.

Checking the Default Gateway

We can use the ip command and the route object to view the system’s configured routes. Since the list option is already the default action, we don’t need to include it. Additionally, we can use “r” in place of route to cut down on typing:

$ ip route list
$ ip r

We can see the term “default” included in one of the routes. This is the standard route for the default gateway/route.

Automatic route addition and deletion are possible in certain cases. For example, while establishing a VPN connection, the ip r command on the same system yields a different set of results. In doing so, the network traffic is routed via a private tunnel:

With grep, it’s possible to easily isolate the default route from a large collection of configured ones:

Deleting the Default Gateway

Now, we delete the route that sends the traffic via the system’s default gateway. For this, we use the route object with the ip command and specify the delete option. Also, we require the sudo access to the routing table in order to make any modifications. First, we list the routes before deleting the default one. Then, we verify our action by listing them again:

$ ip r
$ sudo ip route delete default
$ ip r

As we can see, the default gateway entry is deleted.

Creating a New Default Gateway

The route object with an add argument can be used to create a new default gateway. Let’s take example the adding of 192.168.117.161 as the new default gateway on the wlo1 interface:

 $ sudo ip route add default via 192.168.117.161 dev wlo1

The traffic now routes via this new default gateway on enp0s3.

Making the Routing Changes Permanent

The modifications we made have immediate effect, but they are lost once the machine is restarted. You must alter a few configuration files in order to make your modifications permanent.

For example, in the case of Ubuntu, the netplan command comes in handy. Here, we need to open the netplan network configuration file:

$ sudo nano /etc/netplan/01-network-manager-all.yaml

Now, add the following configuration inside it, below the line corresponding to the renderer:

ethernets:
enp0s3:
dhcp4: true
routes:
- to: default
via: 192.168.56.2

You can refer to the following screenshot. Keep in mind that the successive level of indentation is important here as well.

After saving the file, apply the changes:

$ sudo netplan apply

Getting back to the normal command line means that the configuration is successful. Also, you can use the netplan command with the try option to examine the modifications before you apply them:

$ sudo netplan try

This provides you time to test your settings. If everything is set properly, you can save the configuration by hitting the “Enter” key within two minutes before the command times out. In this way, your changes are applied. Otherwise, if you don’t hit the “Enter” key, the changes only remain in the configuration file. However, your network settings are not yetmodified by them.

Note: On RHEL 8 Red Hat System, we can use the nmstatectl utility to define the gateway configuration in a YAML file. Similarly, RHEL System Roles (playbook approach) can also be used to set the default gateway.

Conclusion

We learned how to configure a default route on the Linux systems with the Ubuntu 20.04 target.

If your default gateway settings are correct, you’ll be able to communicate with the rest of the internet. However, familiarity with the hostname-to-IP address resolution is still required.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.