How to add a route under Linux

Source: Internet
Author: User

Can be divided into 3 small questions:

  1. How to add a default gateway to liunx with the command line, assuming the gateway address is 10.0.0.254
  2. 192.168.1.0 network segment, 192.168.1.1 gateway a server wants to connect to the 172.16.1.0/24 segment, how to add a route (Qihoo 360)
  3. How to add a host route
    Answer:
    First introduce the route command
    One, the default gateway route
    The default gateway is that the packet does not match any of the set routing rules, and finally flows through the address gates! The gateway literally means the network's threshold. Make a metaphor: the door of your house, you just have to pass through it.
    [Email protected] ~]# route-n
    Kernel IP Routing Table
    Destination Gateway genmask Flags Metric Ref use Iface
    192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
    169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
    0.0.0.0 192.168.56.2 0.0.0.0 UG 0 0 0 eth0
    The xxx section of this is the default gateway.
    The Route-n command is to display the kernel routing table.
    When we go online, how to route it? Through the above routing table from top to bottom, if not, that will consider the default gateway, if the default gateway does not, that indicates a problem with the gateway configuration. If the front can go out, it will not consider surfing the Internet from the default gateway.
    About the configuration of the gateway, you can consider my article CENTOS6 and CENTOS7 network NIC and gateway DNS overview
    To remove a gateway command:
    [Email protected] ~]# route del default GW 192.168.56.2
    [Email protected] ~]# route-n
    Kernel IP Routing Table
    Destination Gateway genmask Flags Metric Ref use Iface
    192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
    169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
    [[email protected] ~]# Ping baidu.com
    Connect:network is unreachable
    After removing the default gateway, we found that we could not surf the internet, that is to say we want to surf the internet, must have a gateway.
    To add a default gateway:
    [[email protected] ~]# route add default GW 192.168.56.2
    [Email protected] ~]# route-n
    Kernel IP Routing Table
    Destination Gateway genmask Flags Metric Ref use Iface
    192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
    169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
    0.0.0.0 192.168.56.2 0.0.0.0 UG 0 0 0 eth0
    This command line only takes effect on a temporary basis. Refer to my article CENTOS6 and CENTOS7 network NIC and gateway DNS overview
    Network routing: That is, the route to a network or network segment
    General multi-segment communication between each other, want to establish a priority route, rather than through the default network for communication (this walk more, wasting resources and time), you can configure a network route. For example: you want to go to another village, there are three villages (A,b,c), a for your village, B for another village, C for the destination you are going to. You go to C village, originally there is a default road through village B, then to C village. Now feel a bit detour, people created a new road, direct from A to C village, not through village B. This new road is called network routing.
    The actual work will also have a corresponding demand: there are two different internal network segments 192 and 10 need to communicate, rather than through the external network to communicate, is to add a network route.
    Route add-net 192.168.1.0 netmask 255.255.255.0 GW 192.168.1.1
    Communication across the network, is required to have a router,
    The following is a schematic, omitting the switch. Describes the network route.
    , the 10.0.0.7 and 192.168.1.7 are communicated by adding the following routing command in 10.0.0.7.

    Note: Here just adds a one-way, need to add the returned route on the B-side
    Method Two: Two-segment communication is achieved by adding a network route to the gateway.

    Note: Here just adds a one-way, need to add the returned route on the B-side
    The command to add a route here can be translated as follows:
    Route add-net 192.168.1.0/24 GW 10.0.0.1
    The above command will fail after restarting the NIC, how to save it?
    The routed profile/etc/sysconfig/network-scripts/route-eth0, which is not present by default, needs to be added by itself.
    The default gateway can be added in the configuration file of the NIC.
    [Email protected] ~]# More/etc/sysconfig/network-scripts/route-eth0
    192.168.1.0/24 via 192.168.1.1
    Third, host routing
    Go to a host address how to configure routing
    Route add-host 192.168.2.11 Dev eth2
    Route add-host 10.0.0.5 Dev Lo
    The above host routing is used when you configure the keepalived or heartbeat high-availability server pair to use a separate NIC to connect the heartbeat line traffic.

How to add a route under Linux

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.