Changing the Default Gateway on Ubuntu 20: A Step-by-Step Guide

Changing the Default Gateway on Ubuntu 20: A Step-by-Step Guide
how to change default gateway on ubuntu 20

When working with networking in Ubuntu, one crucial aspect is the configuration of gateways. The gateway acts as a bridge between your local network and other networks, including the internet. Changing the default gateway in Ubuntu 20 can be necessary when you need to redirect traffic through a different IP address, especially in a dynamic environment or when troubleshooting network issues.

In this comprehensive guide, we will walk you through the necessary steps to change the default gateway on Ubuntu 20. To ensure we're thorough, we'll cover the following topics:

  • Understanding the Role of a Gateway
  • Checking the Current Gateway
  • Changing the Default Gateway via GUI
  • Changing the Default Gateway via Command Line
  • Configuring Persistent Gateway Settings
  • Troubleshooting and Validation
  • Conclusion

Throughout this guide, we will touch on relevant aspects such as API management related to networking tools. For instance, solutions like APIPark can play a role in effectively managing your network traffic by providing comprehensive API governance and management tools.

Understanding the Role of a Gateway

A gateway in a network functions as a node that serves as an access point to another network. The default gateway is primarily used to route traffic from the local network to external networks, such as the internet. In most cases, your router is the default gateway for your network.

Key Functions of a Gateway

  • Routing: Routes data packets from one network to another.
  • Interfacing with External Networks: Acts as a translator between different networking protocols.
  • Network Security: May provide a level of security by monitoring traffic entering and leaving.

Understanding these functions is essential before making any changes. A misconfigured gateway can lead to loss of network access, making it crucial to follow the correct procedures.

Checking the Current Gateway

Before changing your gateway settings, it's helpful to understand your current network configuration. You can verify your existing gateway using the terminal.

Steps to Check the Default Gateway

  1. Open a Terminal:
  2. You can do this by pressing Ctrl + Alt + T.
  3. Run the Command: bash ip route show
  4. This command displays your routing table.

Here’s an example output:

default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.10

From the output, you can see 192.168.1.1 is currently set as the default gateway. Make a note of this IP address in case you need to revert back.

Changing the Default Gateway via GUI

For users who prefer a graphical interface, Ubuntu 20 provides a simple way to change network settings through the Settings GUI.

Steps to Change the Default Gateway Using Setting GUI

  1. Open Settings:
  2. Click on the System Menu located at the top right corner, then select Settings.
  3. Navigate to Network:
  4. In the left sidebar, click on Network.
  5. Select Your Connection:
  6. Click on the settings icon (gear icon) next to the network connection you want to edit (e.g., Wired or Wi-Fi).
  7. Edit IPv4 Settings:
  8. In the IPv4 tab, navigate to the Gateway field and enter the new gateway IP address you wish to use.
  9. Save Changes:
  10. Click Apply or Save to confirm your modifications.
  11. Restart the Network:
  12. You may need to disconnect and reconnect your network or restart your device for the changes to take effect.

The above steps should effectively change your default gateway via the GUI.

APIPark is a high-performance AI gateway that allows you to securely access the most comprehensive LLM APIs globally on the APIPark platform, including OpenAI, Anthropic, Mistral, Llama2, Google Gemini, and more.Try APIPark now! 👇👇👇

Changing the Default Gateway via Command Line

Using the command line allows for more flexibility and is preferred in many server environments. Below, we detail how to change the default gateway using the terminal.

Steps to Change the Default Gateway Using the Command Line

  1. Open Terminal:
  2. Use Ctrl + Alt + T to open the terminal.
  3. Remove Existing Gateway:
  4. To remove the current default gateway: bash sudo ip route del default via [old-gateway-ip] Replace [old-gateway-ip] with the actual IP address noted earlier.
  5. Add New Default Gateway:
  6. To set a new default gateway: bash sudo ip route add default via [new-gateway-ip] Insert your new gateway IP in place of [new-gateway-ip].
  7. Verify Changes:
  8. Again, run ip route show to ensure the changes have been applied successfully.

Here’s a practical example:

sudo ip route del default via 192.168.1.1
sudo ip route add default via 192.168.1.254
ip route show

Configuring Persistent Gateway Settings

Changes made through the command line might not persist after a reboot. To ensure the default gateway remains configured on restart, follow the steps below.

Persisting the Default Gateway

  1. Edit the Netplan Configuration File:
  2. Use a text editor, such as nano, to open the netplan configuration file: bash sudo nano /etc/netplan/01-netcfg.yaml
  3. Update the Gateway:
  4. Locate the section under your network interface, and add or edit the gateway4 parameter: yaml network: version: 2 renderer: networkd ethernets: eth0: dhcp4: yes gateway4: [new-gateway-ip]
  5. Apply the Configuration:
  6. Save and close the file (in nano, press Ctrl + X, then Y, and then Enter). Then apply the new configuration: bash sudo netplan apply

The new default gateway should now persist through reboots.

Troubleshooting and Validation

After making changes, it’s essential to validate that the new gateway is functioning correctly.

Steps to Troubleshoot Gateway Issues

  1. Check Network Connectivity:
  2. Use the ping command to verify connectivity to the gateway: bash ping [new-gateway-ip]
  3. Test Internet Access:
  4. Try to ping a known external server: bash ping google.com
  5. Review Network Configuration:
  6. If you lose connectivity, check your configuration by reviewing the netplan file: bash cat /etc/netplan/01-netcfg.yaml
  7. Logs and Diagnostics:
  8. Check system logs for error messages: bash journalctl -xe

Diagnosing networking issues may require careful examination of both your settings and external factors such as firewalls or ISP configurations.

Conclusion

Changing the default gateway on Ubuntu 20 is a straightforward process that can be accomplished through both graphical user interface and command line methods. Understanding the implications of gateway changes and ensuring that those changes are persistent can enhance your networking capabilities and prevent future issues.

As network operations become increasingly complex, effective API governance tools like APIPark can provide beneficial support in managing API configurations and traffic, ensuring all aspects of your network are operating seamlessly.

FAQ

  1. What is a default gateway?
  2. A default gateway is a node that allows devices in a local network to communicate with other networks, typically routing traffic to the internet.
  3. How can I check my current gateway on Ubuntu?
  4. You can check your current gateway by running the command ip route show in the terminal.
  5. Will changing the gateway affect my internet connection?
  6. Yes, changing the gateway will reroute your internet traffic. If configured incorrectly, it can result in loss of internet access.
  7. How can I restore my default gateway settings?
  8. You can restore your default gateway settings by either re-entering the previous gateway IP or reverting the netplan configuration to its original state.
  9. Can I use both GUI and command line to change the gateway?
  10. Yes, you can use either method to change the gateway on Ubuntu. The choice comes down to your preference and comfort level.

🚀You can securely and efficiently call the OpenAI API on APIPark in just two steps:

Step 1: Deploy the APIPark AI gateway in 5 minutes.

APIPark is developed based on Golang, offering strong product performance and low development and maintenance costs. You can deploy APIPark with a single command line.

curl -sSO https://download.apipark.com/install/quick-start.sh; bash quick-start.sh
APIPark Command Installation Process

In my experience, you can see the successful deployment interface within 5 to 10 minutes. Then, you can log in to APIPark using your account.

APIPark System Interface 01

Step 2: Call the OpenAI API.

APIPark System Interface 02

Learn more