If you have a missing default gateway on your Redhat system you can add new route as per network interface or globally. 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.

First, list our routing table with routel command:
[root@rhel7 ~]# routel
         target            gateway          source    proto    scope    dev tbl
      10.0.0.0/ 8                        10.1.1.56   kernel     link enp0s3 
       10.0.0.0          broadcast       10.1.1.56   kernel     link enp0s3 local
      10.1.1.56              local       10.1.1.56   kernel     host enp0s3 local
 10.255.255.255          broadcast       10.1.1.56   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

From the above routel command output we can see that we are missing default gateway. For a basic configuration we can add new default gateway using GATEWAY="GW IP ADDRESS" directive inside main network config /etc/sysconfig/network file. For example:
[root@rhel7 ~]# cat /etc/sysconfig/network
GATEWAY="10.1.1.1"
Now, restart networking:
[root@rhel7 ~]# systemctl restart network
After, network restart we can check whether the new default gateway exists within our routing table:
[root@rhel7 ~]# routel | grep default
        default          10.1.1.1                   static          enp0s3 
        default        unreachable                   kernel              lo unspec
        default        unreachable                   kernel              lo unspec
As it was already mentioned above, adding new default gateway via /etc/sysconfig/network file is system wide and any default configuration using specific ifcfg config file will take precedence. To add default gateway in Redhat Linux as per network interface basis, first check your available network interfaces:
 
[root@rhel7 ~]# ip addr show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:32:cc:c0 brd ff:ff:ff:ff:ff:ff
    inet 10.1.1.56/8 brd 10.255.255.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe32:ccc0/64 scope link 
       valid_lft forever preferred_lft forever
Now we can open individual ifcfg config file for any given network interface name, so for example config file for a network interface named enp0s3 will be /etc/sysconfig/network-scripts/ifcfg-enp0s3 and so on. Open this file and add default gateway using GATEWAY="GW IP ADDRESS" directive:
[root@rhel7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
# Generated by dracut initrd
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/network config file, after network restart we will see that the new default gateway GATEWAY="10.1.1.2" takes precedence:
[root@rhel7 ~]# systemctl restart network
[root@rhel7 ~]# routel | grep default
        default           10.1.1.2                   static          enp0s3 
        default        unreachable                   kernel              lo unspec
        default        unreachable                   kernel              lo unspec


Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top