First, add the route command using the route command
To set and view the routing table, you can use the route command to set the command format for the kernel routing table:
# route [Add|del] [-net|-host] target [netmask Nm] [GW GW] [[Dev] If]
which
- Add: Adding a routing rule
- Del: Delete a routing rule
- -net: The destination address is a network
- -host: The destination address is a host
- Target: Destination network or host
- Netmask: netmask for Destination Address
- GW: Gateway through which routed packets
- Output item description of the route command output item description gateway
Destination |
target network segment or host |
gateway address, "*" indicates the destination is the network to which this host belongs, does not require routing |
genmask |
netmask |
Flags | The
tag. Some possible tokens are as follows: |
|
U-Route is active |
|
H-Target is a Host |
|
G-route to Gateway |
|
R-Resume dynamic routing table entries generated |
|
D-Dynamically installed by a routed daemon |
|
m-routed background thread |
|
!-Deny Route |
Metric |
route distance, number of connections required to reach the specified network (not used in the Linux kernel) |
Ref |
number of route entry references (not used in Linux kernel) |
use | td> the number of times this route entry was found by the routing software
Iface |
output interface for this route table entry |
- Dev: The network interface specified for the route
- Routes added using the route command, the machine restarts, or the network card restarts, the route is invalidated, by:
A, the route added to the host
# route add–host 192.168.1.10 Dev eth0
# route Add–host 192.168.1.10 GW 192.168.1.1
B. Routes added to the network
# route add–net 192.168.1.0 netmask 255.255.255.0 eth0
# route add–net 192.168.1.0 netmask 255.255.255.0 GW 192.168.1.1
# route Add–net 192.168.1.0/24 eth1
C. Add a default gateway
# route add default GW 192.168.1.1
D. Delete a route
# route del–host 192.168.1.10 Dev eth0
Second, add a permanent route
A, # vi/etc/rc.local (add to end)
Statement:
Route add-net 192.168.3.0/24 Dev eth0
Route add-net 192.168.2.0/24 GW 192.168.2.254
B, # vi/etc/sysconfig/network (add to end)
Statement: Gateway=gw-ip or Gateway=gw-dev
C, # vi/etc/sysconfig/static-routes (without static-routes, manually create a file like this)
Statement:
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
D. Turn on IP forwarding:
# echo "1" >/proc/sys/net/ipv4/ip_forward (temporary)
# vi/etc/sysctl.conf-net.ipv4.ip_forward=1 (permanently on)
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.
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. What is the Static-routes file, this is a file that is called when the network script executes, and the file is placed in the/etc/sysconfig directory, where it is located in the network script:
# 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
As you can see from this script, this is the way to add static routes, and Static-routes's notation is:
Any net 192.168.1.0/16 GW gateway IP
In this case, the route is added automatically when the network script is started, and because the network is started in front of the netfs, it is naturally normal to mount NFS. So, if you need to add a static route, Using the Static-routes file is better than using the rc.local, and when the network configuration is changed, the corresponding static route can be added automatically, but if you use Rc.local, the original static route is added when you restart the networking service. Disappeared.
This article is from the Linux commune website (www.linuxidc.com) Source Link: http://www.linuxidc.com/Linux/2012-05/61253.htm
CentOS6 How to add permanent static routes to Linux systems