A default gateway is used for traffic not destined for the local network and for which no particular route is preferred inside the routing table. Similarly, traffic of static routes must not or need not pass via a default gateway. Traditionally, a default gateway serves as the dedicated network router.
This guide will learn how to set up a default gateway on Ubuntu 20.04 (Focal Fossa) OS using the ‘ip route’ and ‘route’ command. Let us get started with this HowTo.
Before you start with this tutorial, look at the following requirements:
Precaution: Please note that this is a very advanced technical guide. Only attempt to follow this guide if you know what you are doing. If you are a novice and end up messing with network configuration, you may lose your connection to the internet.
For this guide, we will use IP 192.168.56.4 (enp0s8) as the default gateway address, the IP address of one of the interfaces on our Ubuntu machine (VM1). The other interface has a different IP address which is 192.168.57.5 (enp0s3). Now comes the VM2, which has only one interface with IP address 192.168.56.101 (enp0s3). It is clear that VM2 enp0s3 cannot access VM1 enp0s3 as both are on different network addresses.
Our task is to make both VMs communicate on interfaces with different network addresses. We will set the IP of VM1(192.168.56.4) as a gateway for another machine (VM2), and we will then check by pinging VM1 from VM2 to see if it can reach it the gateway.
VM1 | 192.168.56.4 (enp0s8) | 192.168.56.101 (enp0s3) |
VM2 | — | 192.168.57.5 (enp0s3) |
Let us first check the default gateway that is already set on our machine, for this run the command:
This will display the default gateway on the terminal.
Alternatively, we can use the ‘route’ command for the same purpose:
If you get an error like ‘Command ‘route’ not found’, then you can install the ‘route’ command with:
Check the UG flag to look for the Gateway. Routing table flags and their meanings are given below:
U : UP— indicates that the route is up and valid
H : Host— target is to a host
G : Gateway—means that the route is to a gateway
R : Reject— It is set by ARP when an entry is expired
D : Dynamic— Route added by a routing redirect
M : Modified— Route modified by a routing redirect
The output of the above two commands is almost identical. Another way is to use the ‘netstat -rn’ command to check the gateway IP.
First, we will use the ‘ip’ command to set the default gateway on VM2. For this, follow the steps mentioned below:
Step 1. Open a terminal (Ctrl+Alt+F2) and run the command:
Step 2. Now ping the VM1 from VM2 to check if our gateway is accessible:
To delete the above default route, use the command below:
To confirm the above changes, use the “route -n” command.
Now we will use the ‘route’ command to set the default gateway on VM2. For this, follow the steps mentioned below:
Step 1. Open a terminal (Ctrl+Alt+F2) and run the command:
Step 2. Now ping the VM1 from VM2 to check if our gateway is accessible:
To delete the above default route, use the command below:
To confirm the above changes, use the “route -n” command.
In this guide, we have learned about setting the default gateway on Ubuntu 20.04. Try to make different scenarios of devices and connect them using a common gateway.
One of the most critical gaps in traditional Large Language Models (LLMs) is that they…
Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…
What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…
Wouldn’t it be wonderful to wake up one day with a desire to explore AI…
Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…
Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…