There are numerous ways to add static routes in Linux (CentOS). The easiest way is via the terminal by using one of the following examples.

How to add a static route for a specific host in Linux.
route add -host 192.168.1.47 gw 192.168.10.1
route del -host 192.168.1.47 gw 192.168.10.1


How to add a static route for a specific network in Linux.
route add -net 192.168.1.0/24 gw 192.168.10.1
route del -net 192.168.1.0/24 gw 192.168.10.1

How to add a default gateway.
route add default gw 192.168.10.1
route del default gw 192.168.10.1

The best place to add the default gateway is in the file /etc/sysconfig/network which would then look something like the below.

NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=server.example.com
GATEWAY=192.168.0.10

Also note that default gateways are added on a per interface level in their startup files located in /etc/sysconfig/network-scripts. Example: /etc/sysconfig/network-scripts/ifcfg-eth0

One of the places to add a static route so it is added each time you reboot the server is to add it to /etc/sysconfig/rc.local. Your rc.local file would then look something like the below.

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

# Static Routes
/sbin/route add -net 192.168.1.0/24 gw 192.168.10.1
/sbin/route add -host 192.168.1.47 gw 192.168.10.1


List Price: $39.99 USD
New From: $20.94 USD In Stock
Used from: $0.53 USD In Stock

DeliciousStumbleUponDiggTwitterFacebookRedditLinkedInEmail
Tags: , , , , ,
19 Responses to “How To Add Static Routes In CentOS”
  1. John Richards says:

    I have to disagree with the approach here to make the routes persistant.
    The correct way to add permanent static routes to a system besides setting a default gateway is to create a route-eth0 (or whichever interface you want to route out of) in /etc/sysconfig/network-scripts/. You can add as many routes as you would like per interface by simply incrementing the number at the end of each statement. Once saved a restart of the network services will force a read of the route file.
    The syntax of the file should look like this:
    ADDRESS0=x.x.x.x
    GATEWAY0=x.x.x.x
    NETMASK0=x.x.x.x
    ADDRESS1=x.x.x.x
    GATEWAY1=x.x.x.x
    NETMASK1=x.x.x.x

    [Reply]

    alex Reply:

    Hello John,

    Thanks for the response. I wasn’t aware you could add routes in that manner under each interface so thank you again for responding.

    I do have a question. When you add a network, such as 192.168.20/24, you just enter the network as an address attached to the interface? I have always added multiple addresses to each interface however added routes elsewhere since routes are not specifically attached to each interface per say.

    Also what makes this a better method? Not that I think otherwise I am curious as to why it makes a difference.

    Anyhow thanks again. Always interested to learn new information.

    [Reply]

    John Richards Reply:

    I think I understand your question… Let me know if I didn’t or I don’t explain it well.
    When you specify a route, in your example 192.168.20.0/24 you are only telling the machine where to send the packets to get to the destination network, if the interface was already in that network no routes would be needed. The device that holds the address you set as the gateway should have another routing table it uses to forward the packets again until they arrive at the final destination.

    As an example, I have a web server that has a default gateway out to my DMZ to get to the network and I use etho with a default gateway on it. Which means all traffic will be sent to the default gateway. I also have two internal networks, 10.1.1.0/24 and 192.168.2.0.24 the route out through a different gateway I can add the routes to those networks with the route-eth0 file. What is also common for me is to have a default gateway on the eth0 interface and need to route to specific networks using eth1. I can’t have default gateways on both interfaces (you actually can but it does not work very well) to route outside the local networks so I would not use a gateway statement in the ifcfg-eth1 file and use the route-eth1 file instead.

    The biggest issue with using the rc.local approach is that the file gets overwritten easily when updating your system. Once the system is rebooted the routes are lost. We have quite a few remote systems that I rely on these routes for device administration. Once the route is gone I have to get to the console of the machine to add them back in. Not a huge deal but a pain nonetheless. I see this approach posted everywhere so I know a lot of people use it but they don’t have to. Centos has this baked right in.

    [Reply]

    alex Reply:

    Hello John,

    Yeah you understood and answered it. Makes sense and I actually the organization of it much better specifically if you have a bunch of various routes you have added.

    Thanks again…

    [Reply]

    alex Reply:

    I aslo wanted to add…

    I love people that can post a response like that and not have a harsh response because of their opinion wether it is right or not. If you have any interest in writing posts on issues you run into on the daily and posting them on QD let me know. You can contact me on the contact form of the site. I will modify your user to provide the access to post any articles related to technology that you would like to post.

    [Reply]

  2. Pamela Lloyd says:

    Hi guys,
    I’m running a server/gateway running shorewall on my network. I have a ftp server running on my internal network with a different IP (from same ISP) then my server. The FTP servers internal IP is 192.168.101.10 and the Gateways internal IP is 192.168.101.27. When I try to access the FTP server from the outside, it doesn’t seem to hit the Gateway, I know from doing a tcpdump. Would this be the case, that I just need to add a static route on my gateway server? ie.

    ADDRESS0=192.168.101.10
    GATEWAY0=192.168.101.27
    NETMASK0=255.255.0.0

    Thanks!

    [Reply]

    alex Reply:

    You need to set up one to one NAT. Hope that helps.

    [Reply]

    Scott Gutman Reply:

    actually, i would like to know more about one to one NAT.

    Sorry to hijack this tread but really could use the help and you guys are on topic.

    Can one to one nat be setup on the same machine or does this require another box? Can one box with 2 nics be configured to pass traffic between different subnets with out the setup of iptables and more …?

    [Reply]

    alex Reply:

    Hello Scott,

    Yes however typically you would be setting up NAT to provide one box as the box that faces the public so you can control access to every other server/port/etc. on your local network. It all really depends on what you are trying to do an more than likely is out of the scope of this article depending on how complex the configuration is.

    [Reply]

    Scott Gutman Reply:

    Actually, i figured out the problem.

    I set ip_forwarding using echo 1 > /proc/sys/net/ipv4/ip_forward and then “server network restart” as I thought was needed(windows world thinking). But that just reset the ip_forward to 0. I had to change /etc/sysctl.conf and set net.ipv4.ip_forward = 1.

    Now it works the way i wanted, i can talk to a foreign subnet through the network.

    Now if i also wanted that foreign subnet to see the internet, then i would have to do the one to one nat, as the cable router is only natting for its subnet.

    Thanks.

    [Reply]

    alex Reply:

    Hello Scott,

    Great. Thanks for the update. Looks like you got it worked out.

    [Reply]

  3. Pamela Lloyd says:

    When I created that route-eth0 file in /network-scripts and restarted the network service I get this error:

    Shutting down interface eth0: [ OK ]
    Shutting down interface eth1: [ OK ]
    Shutting down loopback interface: [ OK ]
    Disabling IPv4 packet forwarding: net.ipv4.ip_forward = 0
    [ OK ]
    Bringing up loopback interface: [ OK ]
    Bringing up interface eth0: RTNETLINK answers: Invalid argument
    [ OK ]
    Bringing up interface eth1: [ OK ]

    [Reply]

    alex Reply:

    Hello Pamela,

    I imagine you have this resolved already but if not could you post the configuration of eth0? If you did resolve the issue could you post what was done to resolve the problem?

    Thanks.
    alex

    [Reply]

  4. Захар Сенников says:

    Я конечно в этом не особо разбираюсь, но после вашего поста стал гораздо больше понимать. Спасибо :)

    [Reply]

    alex Reply:

    Hello Захар Сенников,

    No problem. Thanks for leaving feedback.

    Thanks.
    alex

    [Reply]

  5. none says:

    route add -host 192.168.1.47 gw 192.168.10.1 does not really work. e,g

    # route add -host 1.12.60.331 gw 10.0.3.126
    SIOCADDRT: No such process

    [Reply]

    alex Reply:

    Hello none,

    That is a common error when attempting to add a route to a host through a gateway that is not reachable from the computer/server you are trying to add it on. You might check which hosts are directly reachable using “netstat -r” and then add the host specific route accordingly.

    Thanks.
    alex

    [Reply]

  6. moriteza says:

    i have a ubuntu 12.4 server no file /etc/sysconfig/rc.local
    help me to configure and add route on startup

    [Reply]

    alex Reply:

    Hello moriteza,

    updatedb
    locate rc.local

    Thanks.
    alex

    [Reply]

  7.  
Leave a Reply

*Type the letter/number combination in the abvoe field before clicking submit.

*