CentosFedoraLinux

How to set up a default gateway on CentOS/RHEL, Fedora linux

CentOS default gateway, what is it and what is its purpose? A computer needs to know the address of at least one gateway in order to connect to another network, this is called the Default Gateway. Whenever a computer tries to connect to a machine on a different network it will connect to the default network gateway and then the network gateway will route the traffic to the right network. In this tutorial, we’ll learn how to configure a CentOS default gateway. This tutorial can also be applied to other distributions which are still in the Red Hat family, such as RHEL or Fedora.

Print routing table

To print routing table, we can use

route -n

This will show current route(s) in IP format not as DNS name :

[[email protected] ~]# route -n
Kernel IP routing table
Destination            Gateway              Genmask              Flags          Metric       Ref        Use          Iface
192.168.1.0             0.0.0.0               255.255.255.0          U                  0             0             0           bond0
169.254.0.0             0.0.0.0              255.255.0.0               U                  0             0             0           bond0
0.0.0.0                     192.168.1.45     0.0.0.0                       UG               0              0            0           bond0

Delete default gateway

As you can see above the default route is on the last line with Flags UG. if we want to change default route we have to delete current default gateway first. The command pattern for deleting default gateway is route del default gw.
For example

[[email protected] ~]# route del default gw 192.168.1.45

When we see the routing table, there are no default gateway right now.

[[email protected] ~]# route -n
Kernel IP routing table
Destination            Gateway              Genmask              Flags          Metric       Ref        Use          Iface
192.168.1.0             0.0.0.0               255.255.255.0          U                  0             0             0           bond0
169.254.0.0             0.0.0.0              255.255.0.0               U                  0             0             0           bond0

Add default gateway

Now, it’s time to add new gateway. The command pattern to add new default gateway is route add default gw.
For example :

[[email protected] ~]# route add default gw 192.168.1.4
[[email protected] ~]# route -n
Kernel IP routing table
Destination            Gateway              Genmask              Flags          Metric       Ref        Use          Iface
192.168.1.0             0.0.0.0               255.255.255.0          U                  0             0             0           bond0
169.254.0.0             0.0.0.0              255.255.0.0               U                  0             0             0           bond0
0.0.0.0                     192.168.1.4       0.0.0.0                       UG               0              0            0           bond0

Making the changes permanent

When we use route add command above, the change that we made is not permanent, The changes will back to old value when the machine reboot. To make the change permanent, you have to edit file /etc/sysconfig/network, find line

GATEWAY=192.168.1.45

This changes the gateway IP to the new gateway.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close