Red Hat / CentOS Linux Setting a Default Gateway

by Vivek Gite on November 19, 2007 · 22 comments

Q. How do I setup up a default Gateway for all interfaces (eth0 and eth1) under CentOS Linux 5?

A.There are different ways to setup TCP/IP hosts so that you can reach to remote destinations. The simplest one is to setup default gateway by modifying the /etc/sysconfig/network

Setup a default Gateway

Open /etc/sysconfig/network file:
# vi /etc/sysconfig/network
Setup GATEWAY={Router-IP}, if router IP is 74.86.49.129, type:
GATEWAY=74.86.49.129
Save and close the file. Here is my complete file:
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=wan.nixcraft.in
GATEWAY=74.86.49.129

You need to restart networking service:
# /etc/init.d/networking restart

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 22 comments… read them below or add one }

1 mastrboy November 22, 2007

could we get a write-up on howto to do this on Debian also?

Reply

2 vivek November 22, 2007

Open /etc/network/interfaces and setup gateway parameter

gateway=192.168.1.254

Restart networking.

/etc/init.d/networking restart

Reply

3 LiNDA January 17, 2012

I just wondering why you don’t use command #service network restart….???

Reply

4 Vivek Gite January 17, 2012

Your command will only work with Redhat and friends. We are talking about Debian Linux here. The correct command that will work on all debian version:

/etc/init.d/networking restart

However, the latest version of Debian such as 6.x can use the service command:

/usr/sbin/service networking restart

Reply

5 wizkidno November 29, 2007

If you only do the editing in the /etc/network/interfaces file they will be overwritten at boot time.
Please edit /etc/sysconfig/network as described in the article as well.

You can allso set the gateway on each device by editing :
vim /etc/sysconfig/networking/devices/ifcfg-eth0
vim /etc/sysconfig/networking/devices/ifcfg-eth1
And changing the gateway declaration.

Alternativly you could use the util :
system-config-network (from both command line and X.)

Reply

6 BiOg November 14, 2008

With 2 (or more) NIC I notice that the default gateway is always set to the last NIC.

I.E: if you have eth0 and eth1 and configure you default gateway via /etc/sysconfig/network, /etc/sysconfig/network-scripts/ifcfg-eth* and event tweak the routes via /etc/sysconfig/network-scritps/route-eth* and /etc/sysconfig/static-routes to have the default be on eth0

It still end up being the eth1 network used as the default route (shown by the route command).

The script executed during the boot just goes through all the interface configuration and then set the default route on the last visited interface; trashing previous settings (go figure why).

Somebody suggested to me that there is a way to select the default device (or interface) but I am yet to find a good hit on Google.

Reply

7 vivek November 15, 2008

Open /etc/sysconfig/network and append
GATEWAY=eth0.gateway.IP.address

Open /etc/sysconfig/network-scripts/route-eth1 and add routing for eth1 and restart it. For example:
202.54.1.2/29 via 202.54.2.254

Also, set static route for /etc/sysconfig/network-scripts/route-eth0:
10.1.2.3/8 via 10.10.38.95

Do not add gateway entries to
/etc/sysconfig/network-scripts/ifcfg-eth1 and /etc/sysconfig/network-scripts/ifcfg-eth0

Reply

8 Rudy January 29, 2009

BiOg is right in that if you have multiple interfaces it will pick the last one. The fix to that is pretty much what vivek said.
Set your default gateway in the /etc/sysconfig/network and make sure none of the interface /etc/sysconfig/network-scripts/ifcfg- files have a “GATEWAY” line in them. That will force the system to pick the default GW from the network file. You can set static routes in the /etc/sysconfig/network-scripts/route- files

Reply

9 spiderik April 20, 2009

But I have eth0 and eth1 under DHCP and I need the eth0 to be the default gw / any hints?

Reply

10 jamie April 21, 2009

route add -net 192.168.60.0 netmask 255.255.255.0 gw 192.168.30.1
route add -net 192.168.30.0 netmask 255.255.255.0 gw 192.168.30.1

This forces all traffic through my 30 subnets gateway, which is 192.168.30.1.

Reply

11 Raghavan alias Saravanan M September 16, 2009

Hi there,

Yours is a good site and have found useful at various places and searches.

Thanks for the efforts and keep up the good work :)

The way you restart the service, should NOT it be ‘/etc/init.d/network’ instead of ‘networking’? Just check that out.

Cheers,
Raghavan alias Saravanan M.

Reply

12 DunxD October 21, 2009

You don’t want multiple default gateways, else you will get intermittent networking problems!

What you want is one default gateway, then if you need specific routes for specific subnets you should set those up as required.

Restart network is also available on RHEL and CentOS by services network restart

Reply

13 bobby February 3, 2010

Dear Vivek,can u please more brief on Nics,i have 2 networks and 2 lans total 4 lan ports,
now i have 2 difrnt n/w coming from single cable,(IT N/w,signaling N/w),and both n/w have diffrent Gateways.i can not add gateways in /etc/sysconfig/network-script/ifcfg-eth0 and eth1,in /etc/sysconfig/network i have added gateway of signaling..

now i can ping at signaling n/w but not IT network?how to add gateway for another network

Reply

14 bobby February 4, 2010

How to add specific route for another network

Reply

15 MadMac66 February 22, 2010

Hi,
if possible, simply edit /etc/rc.local and add all the routings you want.
There you can setup specific routings for specific NICs and of course also the Default Gateway. example:
route add -net netmask gw eth0
route add -net netmask gw eth1
route add default gw eth0

this way you can setup are more complex routing.

Reply

16 Colin May 5, 2010

/etc/rc.local
route add -net netmask gw eth0
route add -net netmask gw eth1
route add default gw eth0

This is the way I would imagine doing it myself. but what happens after a services network restart

Reply

17 Vivek Gite May 5, 2010

You can add hook to network service or better configure static routing :) this will take care of service network restart command.

HTH

Reply

18 Henrik A December 22, 2010

Hi,
I am trying to set up a RHEL5 box with two nic’s.
eth2 is connected to the internet
eth3 is connected to a firewall that runs our lan and has a IPSec connection to a customer.

My problem is this.
When I try to start up eth3 it is automatically assigned a default gateway, which is the same as for eth2. So when I try to set a static route i get “RTNETLINK answer: file exists” because it already has a gateway.

ifcfg-eth3:

# Intel Corporation 82571EB Gigabit Ethernet Controller
DEVICE=eth3
BOOTPROTO=none
HWADDR=00:15:17:8C:7F:1B
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
NETMASK=255.255.255.0
IPADDR=192.168.2.99

route-eth3:
192.168.2.0/24 via 192.168.2.254
10.212.1.0/24 via 192.168.2.254

the resulting route table:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
xxx.xxx.xxx.xxx 0.0.0.0 255.255.255.248 U 0 0 0 eth2
10.212.1.0 192.168.2.254 255.255.255.0 UG 0 0 0 eth3
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth3
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth3
0.0.0.0 xxx.xxx.xxx.xxx 0.0.0.0 UG 0 0 0 eth2

The destination 192.168.2.0 should have the gateway 192.168.2.254

Any ideas on how to get this working?

Reply

19 Allison May 14, 2011

im having issues changing my server NIC from DHCP to static, i keep getting the error : “Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
does not seem to be present, delaying initialization.
[FAILED] ”

Help will be appreciated.

Reply

20 Norton May 18, 2011

Regarding setting the default route and Vivek’s mildly complicated solution:

In CentOS5 (at least) you can set GATEWAYDEV=eth0 in /etc/sysconfig/network, and you do not need to modify ifcfg-eth* to remove the GATEWAY definitions.

Reply

21 izack August 11, 2011

hi
i have a problem on how to configure a red had linux pc as the gateway
in a single computer with to NIC

Reply

22 Ravi Saive September 21, 2011

Nice information….

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 8 + 3 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: