CentOS 6.4 Add permanent static routes all methods rollup

Source: Internet
Author: User
Tags centos

This article is very good, multi-Nic recommended with a fourth method.

Http://blog.sina.com.cn/s/blog_828e50020101ern5.html

Add a permanent static route to CentOS

A static route is required when using a dual NIC with 2 gateways at the same time. Of course, there are many times when you need to add routes.

Operating system version centos-6.4 64bit

One: Use the route command to add

1, the route command added by using the route, the machine restarts or the network card restarts after the route is invalid, method:

Routes added to the host

# route Add–host 192.168.1.11 Dev eth0

# route Add–host 192.168.1.12 GW 192.168.1.1

Routes added to the network

# route add–net 192.168.1.11 netmask 255.255.255.0 Dev eth0

# route add–net 192.168.1.11 netmask 255.255.255.0 GW 192.168.1.1

# route add–net 192.168.1.0/24 Dev eth1

Add a default gateway

# route add default GW 192.168.2.1

Delete route

# route Del–host 192.168.1.11 Dev eth0

2. You can also use IP commands to add and remove routes

IP route add default via 172.16.10.2 dev eth0

IP route add 172.16.1.0/24 via 172.16.10.2 Dev eth0

The format is as follows:

IP route

Default via Gateway Dev interface

Ip/netmask via Gateway Dev interface

Two: The method of setting the permanent route under Linux:

1. Add in/etc/rc.local

Method:

Route add-net 192.168.3.0/24 Dev eth0

Route add-net 192.168.2.0/24 GW 192.168.2.254

2. Add to end in/etc/sysconfig/network

Method:

Gateway=gw-ip

Or

Gateway=gw-dev

3./etc/sysconfig/static-routes:

Any net 192.168.3.0/24 GW 192.168.3.254

Any net 10.250.228.128 netmask 255.255.255.192 GW 10.250.228.129

If adding a route in Rc.local causes NFS to not mount the problem automatically, it is best to use the Static-routes method. Both the restart system and the service network restart will take effect.

To resolve the NFS problem description:

In the order of Linux boot, Rc.local inside the content is in Linux all services are started, and finally executed, that is, the contents of this is executed after Netfs, that is to say, when the Netfs startup, the static route on the server is not added, so netfs mount can not be successful.

4, add the route under/etc/sysconfig/network-script/route-interface (one file per interface, if not, create one, you can only add routes for that interface)

The format is as follows:

Network/prefix via Gateway Dev intf

For example, add a default gateway to eth0:

Vim/etc/sysconfig/network-scripts/route-eth0

#添加如下内容 (can omit dev eth0)

0.0.0.0/0 via 172.16.10.2 Dev eth0

PS: Note that the mask here is 0 instead of 32, because this is a network segment rather than a route.

After saving exits, service network restart.

Use Route-n or netstat-r to view the routing table.

[Root@localhost ~]# Route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
172.16.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 172.16.10.2 0.0.0.0 UG 0 0 0 eth0

The default route has been added to the routing table.

Note If you have two network cards, you need to set a default route to access the Internet.

All methods for adding static routes are validated on the centos6.4 and are correct.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.