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.
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 the originating 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.
The preferred method of working with routes in Linux is via the
ip command. Other commands such as
ifconfig are considered deprecated.
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
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.
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
ip r | grep default
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
The default gateway entry has been removed.
Adding a Default Gateway
To add a default gateway we use the
add option with the
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
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.
In Ubuntu, you can use the
netplan command and configuration file.
sudo gedit /etc/netplan/01-network-manager-all.yaml
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.
Save the file and close your editor.
To apply the changes, use the
netplan command with the
sudo netplan apply
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
sudo netplan try
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.
In Fedora, we need to edit the “/etc/sysconfig/network” file, and either add or edit the “GATEWAY=” line.
sudo gedit /etc/sysconf/networks
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.
Save the file and close the editor.
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
We can find the name of the interface using the
ip addr command:
Our interface name is “enp0s3”.
We’ll need to use this in the next command.
sudo gedit /etc/systemd/network/enp0s3.network
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.
Save the file and close the editor, and then restart the network manager daemon.
sudo systemctl start NetworkManager.service
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