How to Set the Default Gateway in Linux (2024)

How to Set the Default Gateway in Linux (1)

Access to the internet—or any other network—is governed by the default gateway. We explain what a default gateway is, and how to set and change it on your Linux computer.

The Default Gateway

All of the devices in your home that are connected to the internet such as laptops, desktop computers, cell phones, tablets, and smart devices are actually connected to your local area network (LAN).

Because they’re on the same network, these devices can talk to one another if they need to. Most are happy to do their own thing and operate in isolation but if you have a printer, for example, or a network-attached storage device, then some of your other devices will want to be able to connect to them.

All of your network-enabled appliances connect to your router over Wi-Fi or via a network cable. Your router is the traffic controller for your network. It directs network traffic from device to device. The traffic from each device goes to the router, the router determines which device the data is going to, and sends it on to the target device.

RELATEDHow Does the Internet Work?

Your router is the only part of your network that is actually connected to the internet. Everything else talks to your router and the router brokers connections out to the internet. Responses from internet-based services such as mail servers or websites are received by the router. It then forwards them to the appropriate device inside your network.

So, as well as directing traffic around your network, your router also controls the data flow to and from the internet. It’s a traffic controller and it’s a gateway to other networks. For most people, the only other network they’re concerned with is the internet.

The device that is sending traffic is called theoriginating device. On larger networks, the originating device decides which router to use. If it doesn’t specify a preference—or if there is only one router—the default gateway is used. On most home networks, there is usually a single router containing one gateway.

Configuring the Default Gateway

Normally, the gateway is configured when your operating system is installed. Sometimes you might take ownership of a computer that has been used on a different network that you need to reconfigure the gateway on, or you might have a need to point a particular machine to a different gateway. Perhaps you have a network with different sub-networks and you have a gateway acting as an intermediary.

RELATEDHow to Use the ip Command on Linux

The preferred method of working with routes in Linux is via the ip command. Other commands such as ifconfig are considered deprecated.

With the ip command you can find out what the default gateway setting is, and you can add or delete default gateways.

Discovering the Default Gateway

To see the routes configured on a Linux computer use the ip command with the route object. You can add the list option, but as list is the default action it can be omitted. And to further save keystrokes, “r” can be used instead of the word “route”.

ip route list
ip r

How to Set the Default Gateway in Linux (4)

One of the routes will have the word “default” in it. That’s the default route to the default gateway.

Sometimes routes can be added and removed automatically. Using the ip r command on the same computer produces a different result when we’ve opened a VPN connection. That creates a private tunnel for that network traffic.

How to Set the Default Gateway in Linux (5)

We can see the new entry has a “dev” device name of “tun0”, meaning tunnel zero.

If you have many routes set up, it can be easier to extract the default route using grep.

ip r | grep default

How to Set the Default Gateway in Linux (6)

RELATED: What Is a VPN Tunnel, and How Does It Work?

Removing the Default Gateway

Actually, what we’re doing is removing the route that guides traffic to the default gateway. We can use the ip command with the route object and the delete option. To make changes to the routing table we need to use sudo . We’ll delete the default route and then list the routes.

sudo ip route delete default
ip r

How to Set the Default Gateway in Linux (7)

The default gateway entry has been removed.

Adding a Default Gateway

To add a default gateway we use the add option with the route object.

We’re going to add a route called “default” that directs traffic to the router at 192.168.1.1, and we’re going to send that traffic through network interface “enp0s3.”

sudo ip route add default via 192.168.1.1 dev enp0s3
ip r

How to Set the Default Gateway in Linux (8)

Making Routing Changes Persistent

The changes we’ve made so far take instant effect, but they don’t survive a reboot of the computer. To make your changes permanent requires modifying some configuration files. The techniques differ from Linux distro to Linux distro.

Ubuntu

In Ubuntu, you can use the netplan command and configuration file.

sudo gedit /etc/netplan/01-network-manager-all.yaml

How to Set the Default Gateway in Linux (9)

Add the text starting at “ethernets” to the configuration file. Note that whitespace is important. Make sure each successive level of indentation is two spaces, and take care to include the hyphen ” -” in the “- to:” line. This will set a default route to the router at 192.168.1.1. Replace this with the IP address for your network.

How to Set the Default Gateway in Linux (10)

Save the file and close your editor.

To apply the changes, use the netplan command with the apply option:

sudo netplan apply

How to Set the Default Gateway in Linux (11)

Being silently returned to the command line means the changes were accepted. If you want to test the changes before they are applied, use the netplan command with the try option.

sudo netplan try

How to Set the Default Gateway in Linux (12)

This gives you time to test your changes. Press the “Enter” key to commit the changes. If you don’t press the “Enter” key within two minutes the process will time out and your edits will not have been applied. They’re still in the config file, but they haven’t been applied to your network settings.

Fedora

In Fedora, we need to edit the “/etc/sysconfig/network” file, and either add or edit the “GATEWAY=” line.

sudo gedit /etc/sysconf/networks

How to Set the Default Gateway in Linux (13)

The file might be empty or it might contain other settings. Either find and edit the “GATEWAY=” line or add it. Substitute the IP address for the one that is correct for your network.

How to Set the Default Gateway in Linux (14)

Save the file and close the editor.

Manjaro

With Manjaro we need to edit or create a file named after the network interface you’re setting the default gateway for. The file has an extension of “.network”, and the filename is the same as the network interface.

First, we need to stop the network manager daemon:

sudo systemctl stop NetworkManager.service

How to Set the Default Gateway in Linux (15)

We can find the name of the interface using the ip addr command:

ip addrr

How to Set the Default Gateway in Linux (16)

Our interface name is “enp0s3”.

How to Set the Default Gateway in Linux (17)

We’ll need to use this in the next command.

sudo gedit /etc/systemd/network/enp0s3.network

How to Set the Default Gateway in Linux (18)

There may already be entries in the file, or it might be completely empty. Make sure these two lines appear in the file. Substitute the IP address of the gateway and the name of the network interface to suit your computer and network.

How to Set the Default Gateway in Linux (19)

Save the file and close the editor, and then restart the network manager daemon.

sudo systemctl start NetworkManager.service

How to Set the Default Gateway in Linux (20)

You Might Not Change It Often

But when you need to you’ll find it is easy. Making the changes persistent across reboots is slightly more involved, but still not too difficult.

RELATED: How to Find Your Router's IP Address on Any Computer, Smartphone, or Tablet

READ NEXT

  • How to Set a Static IP Address in Ubuntu
  • Intel Arc GPUs Now Work Better With Older Games
  • How to Fix Bluetooth Audio Delay
  • You Can Get a Year of Paramount+ for $25 (Again)
  • How to Watch UFC 282 Blachowicz vs Ankalaev Live Online
  • Here’s the PC Hardware You Should Buy for Stable Diffusion
  • What Is Packet Loss? (And How to Test for It)
How to Set the Default Gateway in Linux (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5785

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.