≡ Menu

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

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 30 comments… add one }

  • mastrboy November 22, 2007, 11:12 am

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

  • nixCraft November 22, 2007, 1:34 pm

    Open /etc/network/interfaces and setup gateway parameter

    gateway=192.168.1.254

    Restart networking.

    /etc/init.d/networking restart
    • LiNDA January 17, 2012, 8:08 am

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

      • nixCraft January 17, 2012, 8:49 am

        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
        • Manju January 16, 2013, 10:18 pm

          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

        • bigtexun April 10, 2013, 3:34 pm

          Actually the title of the article is “Red Hat / CentOS Linux Setting a Default Gateway”. And LiNDA is correct to ask because /etc/init.d/networking does NOT exist in Centos. LiNDA’s only mistake was in hitting reply under your comment rather than going to the top and hitting “add one”

          “we” are here talking about centos…

  • wizkidno November 29, 2007, 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.)

  • BiOg November 14, 2008, 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.

  • nixCraft November 15, 2008, 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

  • Rudy January 29, 2009, 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

  • spiderik April 20, 2009, 6:59 pm

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

  • jamie April 21, 2009, 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.

  • Raghavan alias Saravanan M September 16, 2009, 2:42 pm

    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.

  • DunxD October 21, 2009, 10:36 am

    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

  • bobby February 3, 2010, 9:31 am

    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

  • bobby February 4, 2010, 4:23 am

    How to add specific route for another network

  • MadMac66 February 22, 2010, 9:10 am

    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.

  • Colin May 5, 2010, 12:23 am

    /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

    • nixCraft May 5, 2010, 1:31 am

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

      HTH

      • Manju January 16, 2013, 10:17 pm

        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

  • Henrik A December 22, 2010, 2:04 am

    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?

  • Allison May 14, 2011, 5:52 pm

    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.

  • Norton May 18, 2011, 5:06 pm

    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.

    • Muhammad El-Sergani February 16, 2012, 12:44 pm

      Nice!

    • Ray May 6, 2013, 2:09 pm

      Good call Norton!

  • Chris July 5, 2011, 2:23 pm

    On Centos 4 and 5 it seems to be init.d/network not networking, is networking a typo?

    /etc/init.d/network restart

  • izack August 11, 2011, 8:11 am

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

  • Ravi Saive September 21, 2011, 11:06 am

    Nice information….

  • Sobgames October 6, 2012, 11:30 am

    how does install TP-link tl-wn727n wireless adapter into centos?

    ——————-
    Thank for all

  • Mighty October 15, 2013, 5:40 am

    What is the difference between using this command:
    # route add default gw 74.86.49.129 eth0

    And inserting the gateway in the file below?
    # vi /etc/sysconfig/network
    GATEWAY=74.86.49.129

Leave a Comment