The default gateway is an essential part of computer networking. When a computer attempts to communicate with another device, it will send packets to the default gateway. The default gateway, which is usually a router, will then direct the packets where they need to go.
Therefore, if a Linux system is not configured with the address of a proper default gateway, it will not know where to send packets to.
In this tutorial, you will learn how to view the currently configured default gateways on Red Hat Enterprise Linux. You will also see how to change the default gateway, in case the IP address or path to the device has changed.
In this tutorial you will learn:
- How to view and change the default gateway on an RHEL system
|Category||Requirements, Conventions or Software Version Used|
|System||Red Hat Enterprise Linux|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Viewing default gateway
Let’s start by viewing the currently configured default gateway on the RHEL system. From there, we can see if the correct IP address is being used (or if any is used at all).
The following two commands will do the job.
$ ip r default via 10.0.2.2 dev enp0s3 10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
Or, for a more thorough output, the
$ routel target gateway source proto scope dev tbl default 10.0.2.2 enp0s3 10.0.2.0/ 24 10.0.2.15 kernel link enp0s3 192.168.122.0/ 24 192.168.122.1 kernel link virbr0 10.0.2.0 broadcast 10.0.2.15 kernel link enp0s3 local 10.0.2.15 local 10.0.2.15 kernel host enp0s3 local 10.0.2.255 broadcast 10.0.2.15 kernel link enp0s3 local 127.0.0.0 broadcast 127.0.0.1 kernel link lo local 127.0.0.0/ 8 local 127.0.0.1 kernel host lo local 127.0.0.1 local 127.0.0.1 kernel host lo local 127.255.255.255 broadcast 127.0.0.1 kernel link lo local 192.168.122.0 broadcast 192.168.122.1 kernel link virbr0 local 192.168.122.1 local 192.168.122.1 kernel host virbr0 local 192.168.122.255 broadcast 192.168.122.1 kernel link virbr0 local ::1 kernel lo ::1 local kernel lo local ff00::/ 8 enp0s3 local
As we can observe from both outputs,
10.0.2.2 is the current default gateway.
Change default gateway
Now that we know what our default gateway currently is, let’s see how we would change it.
Defining default gateway using
ifcfg files, that is, defining it as per network interface basis takes a precedence over global system wide default gateway configuration defined by
/etc/sysconfig/network configuration file.
- For a basic configuration we can add new default gateway using
GATEWAY="GW IP ADDRESS"directive inside main network config
/etc/sysconfig/networkfile. For example:
# cat /etc/sysconfig/network GATEWAY="10.1.1.1"
Make sure you restart Network Manager for the changes to take effect.
# systemctl restart NetworkManager.service
- As it was already mentioned above, adding new default gateway via
/etc/sysconfig/networkfile is system wide and any default configuration using specific
ifcfgconfig file will take precedence. To add a default gateway in Redhat Linux as per network interface basis, edit the appropriate file in
For example, editing the
/etc/sysconfig/network-scripts/ifcfg-enp0s3file will edit the settings for network interface
# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3 DEVICE="enp0s3" ONBOOT=yes NETBOOT=yes UUID="452901c2-06e1-4ed9-afa4-f227c7632eed" BOOTPROTO=none IPADDR="10.1.1.56" NETMASK="255.0.0.0" HWADDR="08:00:27:32:cc:c0" TYPE=Ethernet NAME="enp0s3" GATEWAY="10.1.1.2"
Even though we still kept the global default gateway configuration within
/etc/sysconfig/networkconfig file, after a network restart we will see that the new default gateway
# systemctl restart NetworkManager.service # routel | grep default default 10.1.1.2 static enp0s3 default unreachable kernel lo unspec default unreachable kernel lo unspec
In this tutorial, you learned how to view the currently configured default gateway in Red Hat Enterprise Linux. We also saw two different methods to change the default gateway, either on a global basis or per individual interface. Having a correctly configured default gateway address is essential for communication across local networks and with devices on the internet.