Linux Change Static routes

Source: Internet
Author: User

One: Use the  route  command to add   use the route  command to add the route, the machine restarts or the network card restarts after the route is invalidated, method:  //added to the host Route  # 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 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 / /Remove Route # route del –host 192.168.1.11 dev eth0 Delete default route #route del default  gw 192.168.2.1  II: How to set up a permanent route under Linux:  1. Add   method in/etc/rc.local:   route add  -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw  192.168.2.254 2. In/etc/sysconfIg/network Add to end   method:gateway=gw-ip  or  gateway=gw-dev 3./etc/sysconfig/static-routes :   (Manually create a file like this 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. Opening   ip  forwarding: # echo  "1"  >/proc/sys/net/ipv4/ip_forward  (temporary)  # vi / etc/sysctl.conf --> net.ipv4.ip_forward=1  (permanently open)   There is no mistake here, except that there was no prior contact to these parameters, no idea, This time the study of the next, probably understand the meaning of the next parameter.   First from the simplest is the Route add method:  route add This is the command to add a route, this is nothing to say, then the-host and net parameters behind, Depending on the information that follows the parameter, you can find that host refers to adding a host to the route, and net is adding a network segment to the route. At the back of the parameters is dev and GW, the literal understanding plus the value immediately followed by the judgment, Dev indicates that this host or this segment of NET is the network port, GW is gateway, naturally through which gateway. Why the designated network port or the designated gateway is OK, my understanding is that, the network port is configured with gateway information, the specified network port is actually going to read the gateway information corresponding to this network, equivalent to the specified GW parameters. However, the designated network port is generally due to a number of network environments, if the network port does not specify a gateway, will go to read the default gateway, so here is generally required to network gateway information configuration up, in Linux many times gateway information is not configured in the network port configuration file, such as:/etc/sysconfig /network-scripts/ifcfg-eth0  file inside, andis in the/etc/sysconfig/network, so be careful when using the dev parameter, as much as possible by specifying the GW method. Why   is introduced to the Route add method, because it will be useful later. Now back to the topic, about Static-routes's settings. Online general is written: any net 192.168.3.0/24 gw 192.168.3.254  set is the entire network segment, but if it is a single host how to set it? At the same time from the manufacturers to give a script is: eth0 x.x.x.x netmask 255.255.255.192 gw x.x.x.x  looks also very right look, But the execution found that the added information is not netstat -an inside, not at all.   Then we are looking at the role of static-routes this file, according to the online statement, Static-routes actually in the network this script call, open this script:  /etc/rc.d/init.d/ network  , it's written in: # 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 network at the time of execution to StAtic-routes inside the line looking for any start, and then execute  /sbin/route add -$args   equivalent to manually execute the Route add script, then things are simple, Back to the manual execution method, according to the previous instructions, the correct script is:  any host 10.0.0.10 gw 192.168.1.1 network execution of the time to remove any , get the following parameter:host 10.0.0.10 gw 192.168.1.1  and then execute route add -host 10.0.0.10  gw 192.168.1.1  note here that the front of the host-is the network comes with, do not need to configure to the Static-routes file.  ############################### echo  "ROUTE ADD -NET 172.16.100.0/24 GW  192.168.6.249 " >>/etc/rc.local  allows you to add this command after the system restarts.  ################################################################ debian/ubuntu redhat/centos/ How Fedora permanently adds static routes   The current Linux system network configuration is basically the same, but different distributions are somewhat different, the main distinction between the two major types of:  (1) RedHat (RHEL)/centos/fedora  Linux is placed in a large pile of files under the/etc/sysconfig/network-scripts directory.   (2) Debian/ubuntu linux network configuration is present in several files under the/etc/network/directory.   For example, if we want to add static routes to the Linux system, we need to differentiate between different platforms, RedHat (RHEL)/centos/fedora linux environment Permanently add static routes are usually written in such as/ETc/sysconfig/network-scripts/route-eth0 in these files. Instead of Debian/ununtu, the script that adds the route is placed in the/etc/network/interfaces to execute.   Debian Platform Example:  debian/ununtu Add a routed script into the/etc/network/interfaces execution:  auto eth0 iface  eth0 inet static address 192.168.1.88 netmask 255.255.255.0 gateway  192.168.1.1 up route add -net 192.168.2.0 netmask 255.255.255.0  gw 192.168.2.1 down route del -net 192.168.2.0 netmask 255.255.255.0  gw 192.168.2.1  RedHat, CentOS, Fedora Platform instance:  use configuration file/etc/sysconfig/network-scripts/ Route-interface,interface can be replaced by the actual network card adapter name, such as I want to add static route on eth0, then write route-eth0  configuration file format as follows:  address0=x.x.x.x  NETMASK0=x.x.x.x GATEWAY0=X.X.X.x  If there are multiple static routes to be established, then continue to append in the file, ADDRESS, NETMASK, Gateway, the number behind the increment can be. If there are 2 records:  address0=x.x.x.x netmask0=x.x.x.x gateway0=x.x.x.x address1=x.x.x.x netmask1= x.x.x.x gateway1=x.x.x.x  the use of standardized methodsThe benefits are as follows:  1. These static routes are added automatically when the system starts, without writing rc.local 2. If you modify a static route, you only need to service network restart. No need to manually use the route command to delete static routes

This article from "Insist is Victory" blog, reproduced please contact the author!

Linux Change Static routes

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.