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
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 27 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.
I just wondering why you don’t use command #service network restart….???
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:
However, the latest version of Debian such as 6.x can use the service command:
Hello,
i am trying to establish a SFTP connecticvity ( user
+ key ) authetication from my sftp client to external sftp server,
I am getting the below error , could anyone help?
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /home/seeasown/.ssh/config
debug1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: identity file /home/seeasown/.ssh/id_rsa type -1
debug1: identity file /home/seeasown/.ssh/id_dsa type -1
debug1: loaded 2 keys
ssh_exchange_identification: Connection closed by remote host
Couldn’t read packet: Connection reset by peer
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.
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.
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 restartDear 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
How to add specific route for another network
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.
/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
You can add hook to network service or better configure static routing :) this will take care of service network restart command.
HTH
Hello,
i am trying to establish a SFTP connecticvity ( user
+ key ) authetication from my sftp client to external sftp server,
I am getting the below error , could anyone help?
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /home/seeasown/.ssh/config
debug1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: identity file /home/seeasown/.ssh/id_rsa type -1
debug1: identity file /home/seeasown/.ssh/id_dsa type -1
debug1: loaded 2 keys
ssh_exchange_identification: Connection closed by remote host
Couldn’t read packet: Connection reset by peer
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?
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.
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.
Nice!
Good call Norton!
hi
i have a problem on how to configure a red had linux pc as the gateway
in a single computer with to NIC
Nice information….
how does install TP-link tl-wn727n wireless adapter into centos?
——————-
Thank for all