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.
Previously I have written about how useful public cloud storage can be when starting a…
This is Part 2 of the "Karafka to Async Journey" series. Part 1 covered WaterDrop's…
For many software teams, documentation is written after features are built and design decisions have…
With the release of the FIPS 140-3 certified cryptographic modules for Ubuntu 22.04 LTS, Canonical…
Open source libraries are repositories of code that developers can use and, depending on the…
Last year, we had the opportunity to speak at Regent’s UX Conference (Regent’s University London’s…