ubuntu default gateway

The default gateway is the node in your network that acts as the main access point to other networks. For the average user, this means the default gateway is your router.

Knowing the default gateway (i.e., the router’s address) is necessary for accessing router settings. And if your default gateway is defined incorrectly, it’s also useful to know how to change it to restore network access.

How to Get Default Gateway

The ip route command lists the contents of the routing tables by default. It’s a quick and easy way to find the default gateway.

ip r  

Alternatively, you can ping the default gateway to check the address.

ping _gateway

You can also check the default gateway graphically if you aren’t comfortable with the terminal. To do this, open the Settings app.

In the Network (Ethernet) or Wi-Fi tab, click on the Connection details cog. You’ll find the default gateway, along with the other network details here like your IP address here.

Ways to Change Default Gateway

NetworkManager Applet

The Network Manager GUI that we checked the default gateway from just now can also be used to change it.

  1. Go to Settings > Network as before and click on the Connection details cog.
    wired connection details ubuntu
  2. In the IPv4 tab, switch the IPv4 method to Manual.
    ubuntu manual ipv4 configuration
  3. Enter the new gateway in the Gateway section. Fill in the address and netmask fields as well and press Apply to save the changes.
  4. Now, restart the connection (toggle off-on) to set the new gateway.
    restart network connection ubuntu

Netplan

The recommended way to configure networking on modern Ubuntu systems is with the Netplan config files. To use this method, you’ll need to know the interface’s device name first.

nmcli device status

Now, open the default netplan config file from the /etc/netplan/ directory with a text editor. We’ll use nano.

sudo nano /etc/netplan/*.yaml

The default config for manually setting a static IP will look something like this.

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp1s3:
      dhcp4: no
      addresses:
        - 192.168.18.55/24
      routes:
        - to: default
          via: 192.168.18.1
      nameservers:
          addresses: [8.8.8.8, 1.0.0.1]

Simply adjust the default route here, save the changes, and exit the editor. If you copy this exact config, remember to adjust the other values like the interface name, IP address, etc, before saving. 

Use the try command to test the new configuration. If the gateway is valid, you’ll see a configuration accepted message.

sudo netplan try 

Iproute

If you only want to change the default gateway for the current session, a better option would be to use iproute. It’s quicker, and the gateway changes will automatically revert once you reboot. 

sudo ip route add default via 192.168.18.10

If you want to change the default gateway for a specific interface, note the interface’s logical name first.

nmcli device status

Then, specify the new gateway and the device name like so

sudo ip route add default via 192.168.18.10 dev enp1s0

Nmcli

You can also use the command-line version of NetworkManager to modify network configurations. Check the connection name first.

nmcli c

Now, specify that you want to edit the ipv4.gateway of this connection. Remember to adjust the connection name and gateway as required.

sudo nmcli con modify 'Wired connection 1' ipv4.gateway 192.168.18.25 

Finally, use the following command to start the connection with the new gateway.

sudo nmcli con up id 'Wired connection 1'

Networkd

On systems that use the networkd backend instead of NetworkManager, you can edit the networkd config file to modify values such as your default gateway.

sudo nano /etc/systemd/networkd.conf

The config file should look something like this.

[Match]
Name=enp1s3

[Network]
Address=192.168.18.50/24
Gateway=192.168.18.1
DNS=1.1.1.1

Adjust the gateway here, save the changes, and exit the editor. Then, restart the networkd daemon to apply the changes.

sudo systemctl restart systemd-networkd
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.