Managing network connectivity is a core task for any system administrator. You might find yourself in a scenario where you have moved a production server to a new VLAN or network segment, and it can no longer reach the internet because the old gateway address is invalid.
This guide will walk you through both methods to ensure your Ubuntu network configuration is accurate and resilient.
Key Takeaways: Gateway Management Essentials
- Netplan → The modern, high-level tool used by Ubuntu to manage persistent network settings via YAML files.
- Default Gateway → The router IP address that forwards packets when no other specific route matches the destination.
ip route→ The command-line utility used to view and modify the system routing table in real-time.- Persistence → Configurations in
/etc/netplan/survive reboots, whereasipcommand changes do not. - Validation → Always use
sudo netplan tryorgenerateto catch syntax errors before applying changes.
Method 1: Persistent Configuration Using Netplan (Recommended)
Netplan is the standard for modern Ubuntu releases. It uses a centralized “source of truth” to render configurations for backends like systemd-networkd or NetworkManager.
Command to View Current Netplan Status: sudo netplan status -a
Configuration Steps:
- Locate your configuration file in
/etc/netplan/(e.g.,99_config.yaml). - Add a
routesblock under your specific interface (likeeth0). - Define the default route using the
to: defaultandvia: <gateway_ip>keys.
Example YAML snippet:
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 10.10.10.2/24
routes:
- to: default
via: 10.10.10.1
Note: For Ubuntu 18.04 LTS, use the legacy gateway4: 10.10.10.1 key instead of the routes block.
Expected Output after applying (sudo netplan apply): The system will silently apply the settings. You can verify the new gateway by running ip route show.
Method 2: Temporary Changes Using the ip Command
If you need to test a new gateway immediately without editing files, the ip command is the fastest way. This is highly useful for temporary troubleshooting sessions.
Command to Add a Gateway: sudo ip route add default via 10.102.66.1
See also: Mastering the Linux Command Line — Your Complete Free Training Guide
Details and Output: To verify the change, use: ip route show default
Expected Output:
default via 10.102.66.1 dev eth0 proto static
If you encounter errors like “File exists,” you may need to delete the existing default route first using sudo ip route del default.
Step-by-Step Process for Changing the Gateway Permanently
To ensure your network changes are correct and persistent, follow this verified workflow:
- Identify the interface: Run
ip addrto find the logical name of your NIC (e.g.,enp0s25oreth0). - Verify connectivity: Use
pingto ensure the new gateway address is reachable from your current subnet. - Edit the configuration: Open your Netplan file with
sudo vi /etc/netplan/01-netcfg.yaml(or similar). - Update the route: Replace the old
viaaddress with your new gateway IP. - Test for errors: Run
sudo netplan generateto check for YAML indentation issues. - Apply changes: Execute
sudo netplan applyto make the new gateway permanent. - Final verification: Run how to check your IP address in Ubuntu and gateway status using
resolvectl statusorip route.
Summary Tables
| Goal | Command / Tool | Impact |
|---|---|---|
| Check Current Gateway | ip route show | Viewing only |
| Temporary Change | sudo ip route add default via <IP> | Immediate (Lost on reboot) |
| Permanent Change | Edit /etc/netplan/*.yaml | After netplan apply |
| Syntax Check | sudo netplan generate | Validates YAML format |
| Netplan Keyword | Purpose | Example Value |
|---|---|---|
via | Defines the gateway IP | 10.0.0.1 |
to | Destination network | default (0.0.0.0/0) |
renderer | Backend daemon | networkd or NetworkManager |
addresses | Local IP and CIDR | 192.168.1.10/24 |
FAQs
Do I need to reboot Ubuntu after changing the gateway in Netplan? No. Running sudo netplan apply pushes the configuration to the active network backend immediately.
What happens if I have multiple default gateways? While you can set multiple, the system typically uses the one with the lowest metric value. Having multiple gateways without proper metrics can lead to unpredictable routing.
Why did my internet stop working after changing the gateway? Ensure the gateway address is in the same subnet as your system’s IP address. If the gateway is unreachable, the system cannot forward packets beyond the local network.
