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

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

1 day ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

1 day ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

2 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

3 days ago

Bringing automation to open source 5G software at Ubuntu Summit 2024

In today’s massive private mobile network (PMN) market, one of the most common approaches to…

4 days ago

Ubuntu Weekly Newsletter Issue 865

Welcome to the Ubuntu Weekly Newsletter, Issue 865 for the week of November 3 –…

6 days ago