Method 1 for adding a soft route in Linux:
- route add -net 172.16.6.0 netmask 255.255.255.0 gw 172.16.2.254 dev eth0/*
Add a network 172.16.6.0/24 to go through 172.16.2.254 eth0 * // *-net to add network-host to add host, netmask subnet mask, gw gateway, dev device, and device, here is your Nic name */route del gw 172.16.2.254/* delete default gateway 172.16.2.254 */route del-net 172.16.86.0/24/* delete default network 172.16.86.0 */route/* display currentRoutingTable */This method is commonly used, but sometimes it does not work when you delete or delete a soft route. What is the prompt: SIOCADDRT: unable to access the network, so we can use the following method.
Method 2:
The implemented functions are the same as those above.
- ip route add 172.16.6.0/24 via 172.16.2.254 dev eth0
-
- ip route del gw 172.16.2.254
-
- ip route del 172.16.6.0/24 dev eth0ip route
Method To delete: Add a route:
The following is a code snippet:
- route add -net 10.0.0.0 netmask 255.0.0.0 dev eth0
Delete A route:
The following is a code snippet:
- route del -net 10.0.0.0 netmask 255.0.0.0 dev eth0
Add default route:
The following is a code snippet:
- route add default gw 10.0.0.1
Delete default route:
The following is a code snippet:
- route del default gw 10.0.0.1
Or
The following is a code snippet:
- route del default
How to add a route in linux:
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:
The following is a code snippet:
- // Route entry to the host
-
- # Route add-host 192.168.168.110 dev eth0
-
- # Route add-host 192.168.168.119 gw 192.168.168.1
-
- // Route added to the network
-
- # Route add-net IP netmask MASK eth0
-
- # Route add-net IP netmask MASK gw IP
-
- # Route add-net IP/24 eth1
-
- // Add a default gateway
-
- # Route add default gw IP
-
- // Delete a route
-
- # Route del-host 192.168.168.110 dev eth0
How to Set a permanent route in linux:
1. Add in/etc/rc. local
The following is a code snippet:
- route add -net 192.168.3.0/24 dev eth0
-
- route add -net 192.168.2.0/24 gw 192.168.3.254
2. Add to the end of/etc/sysconfig/network
Method: GATEWAY = gw-ip or GATEWAY = gw-dev
3./etc/sysconfig/static-router:
The following is a code snippet:
- any net x.x.x.x/24 gw y.y.y.y
You should be able to master and understand the method of adding soft routes in Linux. We will continue to sort out relevant content for your reference. For more highlights, see: In-depth Analysis of Using Linux + Zebra to build a soft route system