CentOS 6.7 Add all methods for permanent static routes
When using a dual-nic and two gateways at the same time, you need to add a static route. Of course there are still many times that you need to add routes.
Operating system version centos-6.4 64bit
I. Use the route command to add
1. Use the route command to add a route. After the machine restarts or the NIC restarts, the route will become invalid. Method:
// 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 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 a route
# Route del-host 192.168.1.11 dev eth0
2. You can also use ip commands to add or delete 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 viagateway dev interface
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:
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 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.
Description of NFS solution:
Rc. the content in local is executed only after all linux services are started. That is to say, the content is executed only after NFS, that is to say, when NFS is started, the static route on the server is not added, so NFS mounting fails.
4. Add a route under/etc/sysconfig/network-script/route-interface (each interface has one file. If no file exists, only the route for this interface can be added)
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
# Add the following content (dev eth0 can be omitted)
0.0.0.0/0 via 172.16.10.2 dev eth0
Ps: note that the mask here is 0 rather than 32, because it is a network segment rather than a route.
After saving and exiting, the service network restart.
Use route-n or netstat-r to view the route table.
[Root @ localhost ~] # Route-n
Kernel IP routing table
DestinationGatewayGenmaskFlags Metric RefUse Iface
172.16.10.00.0.0.0255.255.255.0U000 eth0
192.168.122.00.0.0.0255.255.255.0U000 virbr0
169.254.0.00.0.0.020.255.0.0u100200 eth0
0.0.0.0172.16.10.20.0.0.0UG000 eth0
The default route has been added to the route table.
Note that if you have two NICs, you must set the default route to access the internet.
All methods for adding static routes are verified on centos6.4 and correct.