Categories: Ubuntu

How to set the default gateway on Ubuntu

To access the internet, a gateway plays a vital role. For accessing other devices on a network or devices like computers, they need to be configured either automatically or manually by a network or system administrator. Communication between different devices is necessary to route data packets when they travel across a network. A system’s routing table decides which network interface should send these packets to remote systems or devices. Network devices are used for this purpose, although routing can be performed by any device configured as a router.

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:

  1. Ubuntu 20.04 installed on your system with internet connectivity.
  2. Root user account or user account with ‘sudo’ privileges.
  3. 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:

ip route

This will display the default gateway on the terminal.

Alternatively, we can use the ‘route’ command for the same purpose:

route -n

If you get an error like ‘Command ‘route’ not found’, then you can install the ‘route’ command with:

sudo apt install net-tools

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:

sudo ip route add default via 192.168.56.4

Step 2. Now ping the VM1 from VM2 to check if our gateway is accessible:

ping -c3 192.168.57.5

To delete the above default route, use the command below:

sudo ip route del default via 192.168.56.4

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:

sudo route add default gw 192.168.56.4

Step 2. Now ping the VM1 from VM2 to check if our gateway is accessible:

ping -c3 192.168.57.5

To delete the above default route, use the command below:

sudo route del default gw 192.168.56.4

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.

Ubuntu Server Admin

Recent Posts

Predict, compare, and reduce costs with our S3 cost calculator

Previously I have written about how useful public cloud storage can be when starting a…

5 days ago

One Thread to Poll Them All: How a Single Pipe Made WaterDrop 50% Faster

This is Part 2 of the "Karafka to Async Journey" series. Part 1 covered WaterDrop's…

5 days ago

A year of documentation-driven development

For many software teams, documentation is written after features are built and design decisions have…

6 days ago

Announcing FIPS 140-3 for Ubuntu Core22

With the release of the FIPS 140-3 certified cryptographic modules for Ubuntu 22.04 LTS, Canonical…

7 days ago

The foundations of software: open source libraries and their maintainers

Open source libraries are repositories of code that developers can use and, depending on the…

1 week ago

From inspiration to impact: design students from Regent’s University London explore open design for their dissertation projects

Last year, we had the opportunity to speak at Regent’s UX Conference (Regent’s University London’s…

2 weeks ago