Why Does The Default Gateway Route Entry Disappear After Restarting The Network Service in RHEL
Why Does The Default Gateway Route Entry Disappear After Restarting The Network Service in RHEL
Environment
Issue
After a network service restart or a server reboot, the default gateway entry disappears from
the IP routing table:
Raw
[root@test sysconfig]# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.1.10 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
[root@test sysconfig]#
[root@test sysconfig]# service network restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
[root@test sysconfig]# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.1.10 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
[root@test sysconfig]#
Resolution
Issue will be seen if the parameter NETWORK is assigned an incorrect value. For instance, below is
a sample configuration file:
Raw
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.10
NETMASK=255.255.255.0
NETWORK=192.168.10.0
GATEWAY=192.168.1.1
ONBOOT=yes
When considering the values for the parameters IPADDR and NETMASK, the correct value
for NETWORK is 192.168.1.0, not 192.168.10.0; the latter is totally a different network
The directive DEFROUTE=no is used if the interface should never act as a default gateway
Diagnostic Steps
Note the following additional information about creating and displaying information about persistent routes:
Use the route command with the –name option to add a persistent route by specifying a name rather than
destination and gateway:
For routes that are created by using the route command, use the route –p show command to display all of the
persistent static routes:
# route -p show
To display the currently active routes on a system, use the netstat command with the following options:
# netstat -rn
% netstat -rn
The output would be similar to the following:
2. Become an administrator.
3. (Optional) Flush the existing entries in the routing table.
# route flush
–net network-address
Specifies that the route goes to the network with the address that is specified in network-address.
–gateway gateway-address
Indicates that the gateway system for the specified route has the IP address gateway-address.
The following example shows how to add a static route to a router (Router 2). The static route is needed for the AS's
border router, 203.0.113.150. See Autonomous System With Multiple IPv4 Routers for an illustration of this particular
setup.
The routing table indicates that there are two routes that Router 2 knows about. The default route uses Router
2's 198.51.100.10 interface as its gateway. The second route, 203.0.113.0, was discovered by the in.routed daemon
that is running on Router 2. The gateway for this route is Router 1 and it has the IP address 203.0.113.20.
You would add a second route to network 203.0.113.0, which has its gateway as the border router, as follows:
The routing table now has a route for the border router, which has the IP address 203.0.113.150.
# netstat -rn
Routing Table: IPv4
Destination Gateway Flags Ref Use Interface
-------------------- -------------------- ----- ----- ------ ---------
default 198.51.100.10 UG 1 249 ce0
224.0.0.0 198.51.100.10 U 1 0 ce0
203.0.113.0 203.0.113.20 U 1 78 bge0
203.0.113.0 203.0.113.150 U 1 375 bge0
127.0.0.1 127.0.0.1 UH 1 57 lo0
If you create a persistent route by specifying the –name option, you can display information about the route by
specifying its name rather than the destination. You can also modify and delete the route by specifying its name. Note
that you cannot use the –name option to name an existing route. Nor can you change the name of a route that you
previously named by using this option. To assign a name to an existing route or to modify the name of an existing
route that you previously named, first delete that route and then add the route again by specifying the –name option.
1. Become an administrator.
2. Add a persistent route by specifying a name.
# route -p add destination-address gateway-address -name name
where –name is the name that you refer to the persistent route by instead of its destination and gateway. The –
name option is ignored for non-persistent routes.
# route -p show
Example 11 Adding a Persistent (Static) Route by Specifying a Name
The following example shows how you would add a persistent static route (route1) by specifying a name.
The following example shows how to modify information for a persistent route by specifying its name. In this example,
the gateway information is changed. Note that the persistent route must have been added by specifying a name
initially.
# route -p show
persistent: route add 9.9.9.9 3.3.3.9 -name route1
# route change -name route1 9.9.8.8
change host -name route1 9.9.9.9: gateway 9.9.8.8
Example 13 Deleting a Persistent (Static) Route by Specifying the Route's Name
The following example shows how you would delete a persistent route by specifying its name. Note that the persistent
route must have been added by specifying a name initially.
If you do not speicfy the –p option with the –name option, the route is removed from the routing tables only, as shown
in the following example:
Prerequisites
In order to add routes on your Linux machine, you need to have administrator rights.
In order to verify it, you can run the “sudo” command followed by the “-v” option (in order to update
your cached credentials).
$ sudo -v
If you don’t have sudo rights, you can have a look at our dedicated articles on getting administrator
rights on Ubuntu or CentOS.
By default, if you don’t specify any network device, your first network card, your local loopback
excluded, will be selected.
However, if you want to have a specific device, you can add it to the end of the command.
As an example, let’s say that you want two LAN networks to be able to communicate with each
other.
As the two hosts are not part of the same subnet, the ping command goes to the default gateway.
In order to see the routes already defined on your machine, use the “ip route” command with no
arguments. You can also use the “ip r” command as an abbreviation.
$ ip r
This is the routing table of your Linux computer : every computer has one. A router happens to
manage many more routes than that but it is essentially using the same routing syntax.
By default, network calls will be forwarded to the local default gateway which is 10.0.2.1
UNLESS your call is for the 10.0.2.0/24 network. In this case, it will simply be sent on your
local network via your default physical link (physically a CAT network cable)
UNLESS your call is for the 169.254.0.0/16 network. In this case, it will also be sent on your
local network using your default physical link.
However, is our router able to forward calls addressed to the 10.0.3.0/24 network?
As you can see, the router is only linked to the 10.0.2.0/24 network which is obviously an issue.
In order to add a route on our Linux router, we use the “ip route add” command.
Now, if you were to ping your second computer on the first computer, you would be able to reach it.
Awesome, you have successfully added a route from one Linux computer to another!
To add a permanent route to the Network Manager, you have to navigate to the connection
file located at “/etc/NetworkManager/system-connections”.
Edit your “Wired connection” file and add a “route1” property in the IPv4 part of the network
configuration.
The route has to be defined as : the network IP address followed by the CIDR, next the default
gateway and finally the next-hop.
In order for the changes to be applied, you can restart your network connection, and execute the
“route -n” command in order to see your route.
$ sudo nmcli connection reload
Using Netplan
Netplan is an Ubuntu exclusive but it can be quite useful if you want to configure your network using
a simple YAML file.
To add a permanent route using Netplan, add the following section to your “/etc/netplan”
configuration file.
$ sudo vi /etc/netplan/<configuration_file>.yaml
For the changes to be applied, you will have to execute the “netplan” command with the “apply”
argument.
Using /etc/network/interfaces
To add a permanent route to a distribution using ifup and ifdown, edit the
“/etc/network/interfaces” file and add the following section.
iface eth0 inet static address 10.0.2.2 netmask 255.255.255.0 up route add -net
10.0.3.0 netmask 255.255.0.0 gw 10.0.2.1
However, if it is not the case, you need to add it in order to keep your route when restarting your
server.
On RHEL and CentOS distributions, you need to create a file named “route-<device>” in the
“/etc/sysconfig/network-scripts” folder.
$ sudo vi /etc/sysconfig/network-scripts/route-enp0s3
As changes are not made live, you will need to reload your network connections from disk using the
“nmcli reload” command.
Awesome! Now there is a route between your first and second network.
As a consequence, you will be able to ping your second computer from the first computer.
Adding a route using the network graphical interface
If you are not into executing commands in the terminal, luckily for you, there is a way to add a route
on Linux using a graphical interface.
Whether you are on Ubuntu, Debian or RHEL makes no difference as they all share the same
network panel on GNOME.
At the top right corner of your screen, look for a small network icon and click on it.
Click on “Wired Connected” and look for the “Wired Settings” panel under it.
When the panel opens, look for the “Wired” section and click on the small gear wheel right next to it.
In the “Wired” panel, you will be presented with many different parameters : your current IPv4
address, your current MAC address, an optional IPv6 address and your link speed.
In the “IPv4” tab, you will be presented with your current IP configured (most likely two for your
computer to act as a Linux router).
Right under it, you will see the “Routes” section. In there, you can specify the input of the previous
sections.
When you are done, click on the “Apply” blue button at the top right corner of the window.
In order for the changes to be applied, you will need to restart your network. You can achieve that by
clicking on the “on/off” toggle in the “Wired” window of the network parameters.
Done!
You have successfully added a route on Linux using the graphical interface, your computers
should now be able to talk to each other.
Troubleshooting Internet issues on Linux
In some cases, you may want to add a route on your Linux because you want to be able to reach
websites outside of your local network, say 8.8.8.8 for example.
As an example, let’s say that you have a local router linked to “Internet” that resides at
192.168.178.1/24.
Inspecting your current routes is an easy way for you to guess why you are not able to reach
Internet websites.
Are the computers or routers between me and the target configured to handle my calls?
Reaching a part of the network is great, but is this part of the network able to answer me back?
In our diagram detailed above, our router may receive an answer from Google, but it has to know
what to do with the request. In your local home network, you don’t have to worry about it as most of
the requests are forwarded using the NAT protocol (short for Network Address Translation
Protocol).
Any network we are trying to reach is accessed via default gateway only if it is not
implicitly overwritten by another static route definition. Let’s have a look at a current
routing table on our Redhat 7 Linux box:
From the above we can see that any packets to reach a destination network
ID 10.0.0.0/8 should travel via enp0s3 interface with 10.1.1.110 and any other
destination network not implicitly defined should use a default gateway 10.1.1.1.
To add a new static route means to define yet another destination network as well as
specify via which IP address and interface the packet should travel through in order to
reach its destination. For example, let’s add a static route to destination
network 15.15.0.0/24 via 10.1.1.110 ip address and enp0s3 interface. To do this
create a new file route-enp0s3 in a /etc/sysconfig/network-
scripts/ directory. To define an above mentioned persistent static route add a
following line into your enp0s3 file:
Next, check whether your new static route was correctly defined:
There are numerous commands that show the routing table but today we will
use the ip command as this command will be replacing the route command in
future releases.
# ip route show
As you can see in the example routing table there are numerous routes
however 1 route shows as the default route. This routing table tells the system
that if the IP that is being communicated to does not fall into any of the other
routes than send the packets to the default route defined as 10.1.6.1. The
default route basically acts as a catchall for any packet that isn't being told
what to do in the above routes.
eth0:
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=10.1.6.21
NETMASK=255.255.255.192
ONBOOT=yes
eth1:
# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=static
IPADDR=10.1.7.41
NETMASK=255.255.255.0
ONBOOT=yes
# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=testing.example.com
GATEWAY=10.1.6.1
Adding the static route is a fairly simple task however before we start we must
first know the gateway for the internal network; for our example the gateway
is 10.1.7.1.
Adding a single IP
The above command adds a route that tells the system to send all packets
for 10.1.5.202 and only that IP to 10.1.7.1 from device eth1.
In order to add a whole subnet than you will need to change the CIDR on the
end of the IP. In this case I want to add anything in the 10.1.5.0 - 10.1.5.255 IP
range. To do that I can specify the netmask of 255.255.255.0 in CIDR
format(/24) at the end of the IP itself.
If a CIDR (or netmask) is not specified the route will default to a /32 (single ip)
route.
# ip route add 10.1.5.0/24 via 10.1.7.1 dev eth1
The difference between these two routes is that the second will route anything
between 10.1.5.0 and 10.1.5.255 out** eth1** with 1 route command. This is
useful if you need to communicate with multiple servers in a network and
don't want to manage lengthy routing tables.
While the commands above added the static route they are only in the routing
table until either the server or network service is restarted. In order to add the
route permanently, the route can be added to the route-<interface> file.
# vi /etc/sysconfig/network-scripts/route-eth1
Append:
10.1.5.0/24 via 10.1.7.1 dev eth1
If the above configuration file does not already exist than simply create it and
put only the route itself in the file (# comments are ok). When the interface is
restarted next the system will add any valid route in the route-eth1 file to the
routing table.
I highly suggest that when possible anytime you add a route to the route-
<interface> files that the interface itself is restarted to validate whether the
route is actually in place correctly or not. I have been on many late night calls
where a static route was not added correctly to the configuration files and was
removed on the next reboot, which is also long after everyone has forgotten
that a static route was required.
Before performing any deep down troubleshooting steps the easiest and first
step should be to check if the routing table actually has the route you expect
it to have.
# ip route show
The easiest way that I have found to find out whether a static route is working
correctly or not is to use tcpdump to look at the network communication. In our
example above we were attempting to communicate to 10.1.5.202 through
device eth1.
# tcpdump -qnnvvv -i eth1 host 10.1.5.202
If the static route was incorrect or missing the tcpdump output would look
similar to the following.
# tcpdump -qnnvvv -i eth1 host 10.1.5.202
1.5 Use the following command to show the current routing table
route print
Example output
C:\WINDOWS\system32>route print
===========================================================================
Interface List
===========================================================================
===========================================================================
Active Routes:
===========================================================================
Persistent Routes:
None
===========================================================================
Active Routes:
===========================================================================
Persistent Routes:
None
C:\WINDOWS\system32>
This command means, we are adding a static route, that specifying all traffic bound for
10.0.3.0 subnet will go through gateway 10.0.0.1 with automatic metric cost
Interface List
===========================================================================
===========================================================================
Active Routes:
===========================================================================
Persistent Routes:
None
IPv6 Route Table
===========================================================================
Active Routes:
===========================================================================
Persistent Routes:
None
C:\WINDOWS\system32>
To double confirm, we can use the route print command again, this time, we should
see the new persistent static route under “Persistent Routes:” section