centos 7 default gateway

linux
network
centos
redhat

Routing

On CentOS you can check the routing table with:

The previous command prints a table like this:

The last line of the table indicates the default gateway of the machine. In this case:

Change default gateway

You can control default gateway using route command:

Keep in mind that this changes are only temporary. To change default gateway permanently you need to edit the file /etc/sysconfig/network and change to:

With multiple interfaces on the same subnet it’s also possible designate the prefered route to the default gateway with:

I am following a tutorial found here on how to setup a gateway for each adapter and also a default gateway:

But when I run the commands

the gateway does not appear as shown in the tutorial.

Here are my relevant files:

I think I have followed everything correctly. So I don’t understand why gateways does not show up as in the tutorial.

A default gateway is a remote host or router that your Linux host forwards traffic to when the destination IP address of outgoing traffic does not match any route in your local routing table. Configuring a default gateway on CentOS is quite straightforward.

If you wish to change a default gateway temporarily at run time, you can use ip command.

First things first. To check what default gateway you are using currently:

According to the local routing table shown above, a default gateway is 192.168.91.2, and traffic is forwarded to the gateway via eth0.

In order to change a default gateway to another IP address:

», ‘clear’ => », ‘margin_top’ => ’10’, ‘margin_bottom’ => ’10’, ‘padding_top’ => », ‘padding_bottom’ => »), array(), array()) —>

Obviously, a default gateway’s IP address should come from the subnet associated with the interface connected to the default gateway, in this example, 192.168.91.0/24. Otherwise, the command will fail with the following error.

Also, keep in mind that the default route change made by ip command will be lost after rebooting.

In order to set a default gateway permanently on CentOS, you will need to update /etc/sysconfig/network accordingly.

Again, be aware that the IP addressed specified here should match with the subnet (192.168.91.0/24) associated with a default route interface.

Another option to set a default gateway persistently on CentOS is to edit /etc/sysconfig/network-scripts/ifcfg- , and add «GATEWAY= » there. If the default interface is «eth0», you will need to edit /etc/sysconfig/network-scripts/ifcfg-eth0. If you choose to use this method, you need to refer to this post to get familiar with this option.

Whether you edit /etc/sysconfig/network or /etc/sysconfig/network-scripts/ifcfg-ethX, don’t forget to restart network service as follows, or reboot your CentOS for the change to take effect.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

Dan Nanni

Latest posts by Dan Nanni (see all)

7 thoughts on “ How to set a default gateway on CentOS ”

Hi,
Can you help me answer this question:
You have replaced the default gateway on your network. The gateway had used the IP address of 201.12.3.4, and you must remove it from the routing table. Which command would you use to accomplish this?
Thanks

$ sudo ip route delete default via 201.12.3.4

Hello there, thanks for taking your time to help others.

I have a machine with two network cards.

eth0 has the following settings:
address = 10.242.28.27
netmask = 255.255.255.0

eth1 has the following settings:
address = 192.168.100.11
netmask = 255.255.255.0

the current default gateway for the system is 10.242.28.1

my problem is this. The 192 subnet is connected to the internet. The 10 subnet is connected to my VOIP Vendor. in the current configuration my phones work but my machine can not see the internet. this is problematic because I can not update the software on the machine which i need to do regularly. What I would like to accomplish is the following:

all traffic on 10 network route through 10.242.28.1
all trafic on 192 network route through 192.168.100.1

this would enable the phone traffic to route over the 10 network and all other traffic to route over the 192 network which is where the internet is located.

if i change the default route to the 10 network the phones work but no internet. if I change the default route to the 192 network i have internet and no phones. I have searched hi and low to find an answer and I have been out of luck so far. I am going to update the /etc/sysconfig/network and /etc/sysconfig/network-scripts/route-eth0 and eth1 files to make this permanant.

any help you can offer with the commands and or advice would be greatly appreciated.

Rob,
You need 2 static routes added with the following commands:
«sudo route add -net 10.242.28.0 netmask 255.255.255.0 gw 10.242.28.1 dev eth0»
«sudo route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.100.1 dev eth1»
This will tell the kernel to route all traffic that is for specific network through a specific eth device.
The router on the other side of eth0 and eth1 must be programmed according to the settings above.

Hope this helps
Regards
Tony

What would you have to do in order to make this survive a reboot?

Оцените статью