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
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 11-19-07

{ 8 comments… read them below or add one }
could we get a write-up on howto to do this on Debian also?
Open /etc/network/interfaces and setup gateway parameter
Restart networking.
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.)
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.
Open /etc/sysconfig/network and append
GATEWAY=eth0.gateway.IP.addressOpen /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.254Also, set static route for /etc/sysconfig/network-scripts/route-eth0:
10.1.2.3/8 via 10.10.38.95Do not add gateway entries to
/etc/sysconfig/network-scripts/ifcfg-eth1 and /etc/sysconfig/network-scripts/ifcfg-eth0
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
But I have eth0 and eth1 under DHCP and I need the eth0 to be the default gw / any hints?
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.