/etc/sysconfig/network-scripts/ directory for legacy systems or using NetworkManager dispatcher scripts for modern setups.
Understanding Default Routes
A default route acts as the gateway for all traffic that does not match a more specific rule in your routing table. In IP networking, this is typically represented as0.0.0.0/0. When your server needs to communicate with an external network, such as the internet, it checks its routing table. If no specific path exists for the destination IP, it sends the packet to the default gateway. Without a permanent default route, your server will lose connectivity to external resources after every reboot or network interface failure, leading to significant downtime and service interruptions.
Configuring via Network Scripts (RHEL 7 and earlier)
For older versions of Red Hat Enterprise Linux, such as RHEL 7, the network configuration is managed through individual script files. You need to identify the active network interface name, which is ofteneth0, ens33, or similar, depending on your hardware and virtualization environment. You can list available interfaces by running ip link show or ls /sys/class/net/.
Once you have identified the interface, navigate to the configuration directory using cd /etc/sysconfig/network-scripts/. Locate the file corresponding to your interface, typically named ifcfg-interface_name. Open this file with a text editor like vi or nano. You must add or modify the following directives within the file:
- GATEWAY: Set this to the IP address of your router or upstream provider. For example,
GATEWAY=192.168.1.1. - DEFROUTE: Ensure this is set to
yesto designate this interface as the primary source for default traffic. - ONBOOT: Verify that this is set to
yesso the interface activates at boot time.
systemctl restart network. To verify that the default route has been added correctly, run ip route show and look for the line starting with default via.
Configuring via NetworkManager (RHEL 8 and later)
In RHEL 8 and newer versions, NetworkManager is the primary tool for managing network connections. While you can still use the old-style scripts, they are often deprecated or handled differently. The recommended approach is to use thenmtui utility or direct nmtui commands.
If you prefer the command line, you can use nmtui to edit connections interactively. Run nmtui, select "Edit a connection," choose your active connection, and navigate to the IPv4 CONFIGURATION section. Change the method from "Automatic" to "Manual" if necessary, and enter your IP address, netmask, and gateway. Ensure the "Default Route" checkbox is selected. Save the changes and exit the utility.
Alternatively, you can modify the connection profile directly using nmcli. First, identify the connection name with nmcli connection show. Then, update the gateway setting using the following command structure:
nmcli connection modify <connection_name> ipv4.gateway <gateway_ip>
nmcli connection modify <connection_name> ipv4.method manual
nmcli connection up <connection_name>
This approach ensures that the configuration persists across reboots because NetworkManager writes these settings to its persistent storage. Always verify the result with ip route show to confirm the default route is present.
Verifying and Troubleshooting Connectivity
After applying the configuration, it is crucial to test the connectivity. A common issue arises when the gateway IP is incorrect or unreachable. If you cannot ping the gateway, check your physical cabling or virtual switch configurations. If you can ping the gateway but not external sites, there may be firewall rules blocking outbound traffic. You can also check the system logs for any errors related to network initialization. Look for messages in/var/log/messages or use journald with journalctl -u network. Additionally, ensure that your DNS settings are correct, as a missing default route is different from a DNS resolution failure. For further assistance with network-related issues, such as troubleshooting curl commands that fail due to network misconfigurations, refer to our guide on Troubleshooting curl not working on an RHEL8 Server.
Another potential pitfall is conflicting routes. If multiple interfaces are configured with default gateways, the kernel might choose the wrong one based on metric values. You can view the metrics associated with each route using ip route show. Lower metrics take precedence. You can adjust the metric in your configuration files or via nmtui to prioritize the correct interface.
Frequently Asked Questions
How do I remove a default route temporarily?
You can remove the default route temporarily by running the command ip route del default. This change is not persistent and will disappear upon reboot or network service restart. Always verify the removal with ip route show to ensure the entry is gone.
What happens if I have two default gateways?
If multiple default gateways exist, the kernel uses the route with the lowest metric value. This can lead to asymmetric routing or connectivity issues. You should ensure only one interface has a default route configured, or explicitly set metrics to control traffic flow between interfaces.
Can I use nmcli to set a default route without restarting the service?
Yes, using nmtui or nmcli connection up applies changes immediately to the active connection. However, if you edit the raw configuration files directly, you may need to restart the NetworkManager service or reload the connection profile to see the changes take effect.
Why is my default route missing after a reboot?
This usually indicates that the configuration file was not saved correctly, the ONBOOT parameter is set to no, or the network service failed to start. Check the system logs for errors during boot and verify that the gateway IP specified in the config file matches your actual network infrastructure.
How do I check the current routing table?
Use the command ip route show or route -n to display the current routing table. Look for the line that begins with default or 0.0.0.0 to identify your active gateway. This output shows the gateway IP, the interface used, and the metric associated with the route.