Add permanent static routes in Linux

Source: Internet
Author: User

I. Use the route command to add
If you use the route entry added by the route command, the route becomes invalid after the machine is restarted or the NIC is restarted. The method is as follows:
// Route entry to the host
# Route add-host 192.168.1.11 dev eth0
# Route add-host 192.168.1.12 gw 192.168.1.1
// Route added to the network
# Route add-net 192.168.1.11 netmask 255.255.255.0 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 eth1
// Add a default gateway
# Route add default gw 192.168.2.1
// Delete a route
# Route del-host 192.168.1.11 dev eth0


Ii. How to set a permanent route in 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 the end of/etc/sysconfig/network
Method: GATEWAY = gw-ip or GATEWAY = gw-dev

3./etc/sysconfig/static-routes: (manually create a file without 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
4. Enable IP forwarding:
# Echo "1">/proc/sys/net/ipv4/ip_forward (temporary)
# Vi/etc/sysctl. conf --> net. ipv4.ip _ forward = 1 (permanently enabled)


If you add a route in rc. local, NFS cannot be automatically mounted. Therefore, static-routes is the best method. Both system restart and service network restart take effect.
Rc. the content in local is executed only after all linux services are started. That is to say, the content is executed only after netfs, that is to say, when netfs is started, the static route on the server is not added, so netfs cannot be mounted successfully.

What is the static-routes file? This is a file called when the network script is executed. The file is placed in the/etc/sysconfig directory, and the location in the network script is:
# Add non interface-specific static-routes.
If [-f/etc/sysconfig/static-routes]; then
Grep "^ any"/etc/sysconfig/static-routes | while read ignore args; do
/Sbin/route add-$ args
Done
Fi
From this script, we can see that this is the method for adding static routes. The static-routes method is
Any net 192.168.0.0/16 gw gateway ip Address
In this way, the route is automatically added when the network script is started. Because the network is started before netfs, it is normal when nfs is mounted.
In this case, if you need to add a static route, the use of the static-routes file is better than the use of rc. local is good, and when the network configuration is changed and the network script needs to be restarted, the corresponding static route can be automatically added, but if rc is used at this time. local, when the network service is restarted, the originally added static route disappears.

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.