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.
What will we cover?
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.
What are the Prerequisites?
Before you start with this tutorial, look at the following requirements:
- Ubuntu 20.04 installed on your system with internet connectivity.
- Root user account or user account with ‘sudo’ privileges.
- Basic knowledge of computer networks and Ubuntu administration.
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.
Experimental Setup
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) |
Finding the Default Gateway IP
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.
Setting Default Gateway Using the ‘ip’ Command
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.
Setting Default Gateway Using the ‘route’ 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.
Conclusion
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.
from Linux Hint https://ift.tt/3BciEvB
0 Comments