Red Hat / CentOS Linux Setting a Default Gateway

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

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 8 comments… read them below or add one }

1 mastrboy 11.22.07 at 11:12 am

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

2 vivek 11.22.07 at 1:34 pm

Open /etc/network/interfaces and setup gateway parameter

gateway=192.168.1.254

Restart networking.

/etc/init.d/networking restart
3 wizkidno 11.29.07 at 1:39 pm

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.)

4 BiOg 11.14.08 at 9:39 pm

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.

5 vivek 11.15.08 at 4:18 am

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

6 Rudy 01.29.09 at 1:13 am

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

7 spiderik 04.20.09 at 6:59 pm

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

8 jamie 04.21.09 at 2:54 pm

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.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , ,

Previous post: How to compile program under Linux / UNIX / FreeBSD

Next post: Lighttpd as you compiled without pcre support error and solution