Debian 11: how do I add or change the default gateway?

debian 11 default gateway tutorial

This tutorial explains how to manipulate the default gateway on a Debian 11 machine, including how to add, remove or modify a default gateway. An article in the same vein as the one explaining how to set a static IP address on Debian 11.

By the way, what's the default gateway for? It's a node on the local network that allows your machine, in this case running Debian (Linux), to contact other machines, services, etc... located on a network other than yours. Typically, the default gateway takes the form of a router or firewall that handles routing. When your Debian machine wants to communicate with a machine outside your local network, it will use the default gateway to reach its target.

At home, the default gateway takes the form of your ISP-supplied Box, which is a router. If you're using another piece of equipment, such as a Wi-Fi router, this will play the role of "default gateway". The router's IP address is distributed to connected devices via the Box's built-in DHCP server, whether you're using a wired or Wi-Fi connection.

In the enterprise, the default gateway will be a router supplied by your operator, in some cases a Cisco router, or a firewall (UTM box) that acts as a gateway to local network nodes and at the same time provides filtering, flow analysis and other functions. On this subject, if you appreciate the world of open source, I recommend you take a look at the Pfsense and Opnsense solutions.

To follow this tutorial, you'll need root (or sudo) access on the Debian machine, as we'll be modifying the system configuration so we need elevated privileges.

1. What is the default gateway for my Debian server?

Before trying to modify the gateway, it would be a good idea to know the IP address of this gateway. To do this, run the following command in Terminal:

ip route

As far as I'm concerned, the command returns :

default via 192.168.1.1 dev ens192 onlink

I can therefore consider that the default gateway used by my machine has the IP address "192.168.1.1". Another way of obtaining this information is to use the "route" command as follows:

route -n

This command directly returns the routing table of the Debian machine. The gateway is identified in the default route, associated with the destination IP address "0.0.0.0". The value remains the same, which is logical.

Kernel IP routing table
Destination Gateway Genmask Indic Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 ens192

In the output above, the " Indic "is interesting, as it provides information on road conditions. The letter " U "means Up" so the route is active and valid, and the letter " G "means Gateway "To indicate that this road leads to a footbridge.

In some cases, there are different routes displayed, especially if you're using Docker, WireGuard, OpenVPN, etc., as shown in this example, but there's always a default route.

Setting up Debian 11 default gateway

If you can't use the "route" command, you're missing a :

sudo apt-get install net-tools

2. Add a default gateway Debian 11

To declare a default gateway on Debian 11, you can use the "interfaces" file, which is used to configure the machine's network interface. You can also use the "route" command in this way:

sudo route add default gw  
sudo route add default gw 192.168.1.1 ens192

In the example above, I'm adding the gateway "192.168.1.1" to my network card "ens192". Another possible syntax:

sudo ip route add default via 192.168.1.1

All that's left is to use one of the commands described above to check that the configuration has been applied.

3. Delete a default gateway

Conversely, you can also delete a default route. Useful if you make a mistake or if there's a change on your network.

sudo ip route del default via  (default route)
sudo ip route del default via 192.168.1.1

The example above deletes the route to gateway "192.168.1.1". Please note that a machine without a default gateway will not be able to access the Internet, even if the DNS server is correctly declared. Then, as always, check with the following command:

route -n

And now you can manage the default gateway on your Debian 11 server ! It's a great addition to our range, as well as being able to manage the server's IP configuration via the Linux host's "interfaces" file. This new article is the latest addition to the IT tutorial box!

Resources

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *