Setting a default gateway in Ubuntu allows your system to route traffic to networks outside its local subnet, enabling internet access or communication with remote networks. This is essential when the gateway is not automatically assigned via DHCP or needs to be changed for static network configurations.
What You'll Need
- Administrative (sudo) access to the Ubuntu system
- Terminal access (local or via SSH)
- The IP address of the gateway you want to set (e.g., 192.168.1.1)
- Knowledge of your network interface name (e.g., eth0, ens5, enp3s0)
Step-by-Step
- Open a terminal window. You can do this by pressing Ctrl+Alt+T or searching for 'Terminal' in the application menu.
- Check your current network configuration and identify the active network interface by running:
ip route show. Look for the line starting with 'default' to see the current gateway and interface (e.g., 'default via 192.168.1.1 dev eth0'). - If you need to change or set the gateway, first determine the correct interface name using:
ip link show. Look for interfaces with state 'UP' and note the name (e.g., eth0, ens5). - To set the default gateway temporarily (until next reboot), use the command:
sudo ip route add default via GATEWAY_IP dev INTERFACE. Replace GATEWAY_IP with the gateway address (e.g., 192.168.1.1) and INTERFACE with your interface name (e.g., eth0). Example:sudo ip route add default via 192.168.1.1 dev eth0. - To make the change permanent across reboots, edit the Netplan configuration file. Netplan is the default network configuration tool in Ubuntu 17.10 and later. Locate the Netplan YAML file in
/etc/netplan/. Common filenames include 00-installer-config.yaml or 50-cloud-init.yaml. - Open the Netplan file with a text editor using sudo, for example:
sudo nano /etc/netplan/00-installer-config.yaml. - In the YAML file, find the section for your network interface under 'ethernets'. It will look something like: ethernets: eth0: dhcp4: true
- To set a static gateway, change dhcp4 to false and add gateway4 and optional nameservers. For example: ethernets: eth0: dhcp4: false addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8,8.8.4.4]
- Save the file and exit the editor (in nano, press Ctrl+O, then Enter, then Ctrl+X).
- Apply the new Netplan configuration by running:
sudo netplan apply. If there are syntax errors, Netplan will output an error message and not apply the changes. - Verify the new gateway is active by running:
ip route showagain. The default line should now show your specified gateway. - Test connectivity to an external host (e.g., Google's DNS) using:
ping -c 3 8.8.8.8. If successful, your default gateway is working correctly.
Common Mistakes
- Using the wrong interface name: Always verify the interface with
ip link show; using a non-existent or down interface will cause the command to fail silently or produce an error. - Incorrect Netplan YAML syntax: YAML is space-sensitive; using tabs instead of spaces or incorrect indentation will cause
netplan applyto fail. Always use spaces (typically 2 spaces per indent level). - Forgetting to disable DHCP when setting a static gateway: If dhcp4 remains true, the system may ignore your gateway4 setting or cause conflicts.
- Setting the gateway on the wrong interface: On systems with multiple network adapters (e.g., Wi-Fi and Ethernet), ensure you are configuring the correct active interface.
- Not applying changes after editing Netplan: The configuration is not applied until you run
sudo netplan apply; editing the file alone does nothing. - Using an incorrect gateway IP: The gateway must be a valid IP address on the same subnet as your system's IP address.
Tips
- Use
ip -brief link showfor a cleaner view of interface names and status. - If you are unsure of the Netplan filename, list the directory:
ls /etc/netplan/. - Always back up the Netplan file before editing:
sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak. - If DHCP is preferred but you only need to influence routing, consider adjusting metrics instead of removing DHCP entirely.
- After applying Netplan, check for errors with
sudo netplan tryfirst, which allows a temporary apply with rollback on failure. - For troubleshooting, check systemd-networkd or NetworkManager logs if applicable, though Netplan typically uses the former on servers.
- Remember that cloud instances (AWS, Azure, etc.) may have gateway settings managed by cloud-init; editing Netplan directly may be overridden.
Troubleshooting
If the default gateway is not set or traffic is not routing correctly:
- Recheck the interface status:
ip link showensures the interface is 'UP'. - Verify the gateway is reachable on the local network:
ping -c 3 GATEWAY_IP(e.g., 192.168.1.1). If this fails, the issue is local connectivity, not routing. - Look for Netplan application errors: Run
sudo netplan applyagain and note any output. Fix YAML syntax if needed. - Check for conflicting configurations: Ensure no other network management tools (like NetworkManager) are interfering. On servers, systemd-networkd is typical; on desktops, NetworkManager may override Netplan unless configured to render via Netplan.
- Inspect the routing table:
ip route showshould list your gateway as the default. If multiple defaults exist, the one with the lowest metric is used. - Restart networking: After applying Netplan, restart the backend:
sudo systemctl restart systemd-networkd(or NetworkManager if used). - Test DNS resolution separately: If pinging IPs works but domain names fail, the issue may be DNS, not gateway. Check
resolv.confor Netplan nameservers. - As a last resort, revert to a known good configuration or use DHCP temporarily to regain connectivity.
Frequently Asked Questions
Can I set multiple default gateways?
While you can configure multiple gateways, only one will be used as the default route at a time, based on metric. Multiple gateways are useful for failover but require advanced routing policies or scripts to switch dynamically.
What if I don't have a Netplan file?
Minimal installations or certain cloud images may not include Netplan by default. In such cases, networking might be managed by /etc/network/interfaces or NetworkManager. Check those locations or install netplan.io if needed.
Will setting a static gateway break DHCP?
If you set a static gateway but leave dhcp4: true, the DHCP client may still request and potentially override settings. For a fully static setup, set dhcp4: false.
How do I find the correct gateway IP?
On most home networks, the gateway is the router's IP address, commonly 192.168.0.1, 192.168.1.1, or 10.0.0.1. Check your router's label or admin page, or from another device on the network, use ip route to see its gateway.
Is this process different on Ubuntu Desktop vs Server?
The Netplan method is the same on both. However, Ubuntu Desktop may use NetworkManager, which can render Netplan configurations. Ensure your Netplan file has the correct renderer (usually networkd or NetworkManager) if needed.
What does 'RTNETLINK answers: Network is unreachable' mean?
This error when adding a route means the gateway IP is not reachable on the interface's subnet. Verify that the gateway is on the same network as your IP address (e.g., both in 192.168.1.0/24).