How to add a route in Linux

Source: Internet
Author: User

Two Methods for adding a soft route in Linux
First:
Route add-net 172.16.6.0 netmask 255.255.255.0 GW 172.16.2.254 Dev eth0
/* Add a network named 172.16.6.0/24 to go through 172.16.2.254 eth0 */
/*-Net added network-host added host netmask subnet mask GW gateway Dev device, here is your Nic name */
Route del GW 172.16.2.254/* Delete the default gateway 172.16.2.254 */
Route del-net 172.16.86.0/24/* Delete the default network 172.16.86.0 */
Route/* display the current route table */
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 connect to the Internet
So we can use the following method.
Second:
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 eth0
IP Route

Deletion method:
Add route:
Route add-net 10.0.0.0 netmask route 0.0.0 Dev eth0
Delete A route:
Route del-net 10.0.0.0 netmask 255.0.0.0 Dev eth0
Add default route:
Route add default GW 10.0.0.1
Delete default route:
Route del default GW 10.0.0.1
Or
Route del default

Bytes ---------------------------------------------------------------------------------------
How to add a route in Linux:
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.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
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.