linux route command Detailed linux route command Detailed

linux route command Detailed

 

Related Links: https://blog.51cto.com/laodou/2066476

Transfer: https://www.cnblogs.com/lpfuture/p/5857738.html

 

A test question: How to add routes under linux (Baidu interview questions)

 

These are the original title, old boy teacher translated into the following three questions.

 

a. How to use the command line to add a default gateway linux machine, assuming that the gateway address 10.0.0.254?

b. 192.168.1.0 network segment, a gateway server 192.168.1.1 think even the 172.16.1.0/24 segment, how to add routes (Qihoo 360)

c. If you add a host route?

Please answer separately.

Answer: route  -net 172.16.1.0/24 gw 192.168.1.1

route command to use:

 

a. Default Gateway Routing

 

    The default gateway is the packet does not match any routing rule set, and finally flows through the address mark! Gateway literally is the network gateway, the equivalent of our house door of the house, as if we should go through the door, the packet is the same.

 

The answer to this question:

 

route del default gw 10.0.0.254

 

Answers practice:

 

[root @ Oldboy ~] # route -n  # ==> to view the routing table, netstat -rn can.

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

# ==> this is the default gateway information systems, expressed go anywhere (0.0.0.0), are sent to 10.0.0.254, because it is the default gateway, therefore, on the last one. There is also the order of the route, if it does not meet any of these rules to the default gateway process.

 

[root @ Oldboy ~] # route del default gw 10.0.0.254  # ==> this command is to delete the default gateway.

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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 In 0 0 0 eth0

 

[root @ Oldboy ~] # the Add default route gw 10.0.0.254     # ==> This command is to add the default gateway, also answer to this question.

 

[root@oldboy ~]# netstat -rn

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

 

10.0.0.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 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 # ==> back

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 # this is the default gateway added record.

 

With particular emphasis on: Actually route add default gw 10.0.0.254 equivalent route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.0.254

 

b network route: i.e. route to a network or network segments destined

 

    General multi-segment communicate with each other, want to establish a priority route, rather than through the default gateway can configure the network routing. Or take the house analogy, you are not going out, but the bedroom, bathroom, bedroom to go through the bedroom door, went to the bathroom is also subject to the bathroom door, the bedroom and the bathroom door here can be considered to be destined for a routing segment, rather than the default route (ie door of the house.)

 

    The actual work will be exchange of visits between the two different internal network needs, rather than the network access, the above example is the case.

 

    The answer to this question:

 

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

Answers practice:

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

SIOCADDRT: network unreachable # ==> when the address 192.168.1.1 while even unreasonable, can not add routes.

 

[root @ Oldboy ~] # ifconfig eth0: 0 192.168.1.1/24 up  # ==> Add IP alias for a temporary test, if made permanent plus the best two-card or write to the configuration file.

 

[root @ Oldboy ~] # ifconfig eth0: 0  # ==> view the added IP alias (This multi-IP network in the manner called sub-interfaces)

 

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:65:A4:FD 

 

          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0

 

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 

Again add a packet to 192.168.1.0 and 192.168.1.1 to deal with.

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

[root @ Oldboy ~] # netstat -rn    # ==> route -n and the like.

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

 

10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

 

192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0 # ==> This is the network routing

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

169.254.0.0 0.0.0.0 255.255.0.0 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

Development: Other writing

 

[root @ Oldboy ~] # route the Add -net 192.168.1.0 Netmask 255.255.255.0 dev eth0   # ==> specified device instead of an address.

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

169.254.0.0 0.0.0.0 255.255.0.0 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

[root@oldboy ~]# route del -net 192.168.1.0/24 dev eth0   

 

[root@oldboy ~]# route add -net 192.168.1.0/24 dev eth0   

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

169.254.0.0 0.0.0.0 255.255.0.0 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

to sum up:

 

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

 

route add -net 192.168.1.0/24 dev eth0 

 

route del -net 192.168.1.0/24 dev eth0  

 

With particular emphasis on: the above configuration will fail when restart the network, then how to make it permanent?

 

If we are permanent, there are several ways:

method one:

 

vi / etc / sysconfig / network-scripts / route-eth0 # default file does not exist

 

Add the following:

 

192.168.1.0/24 via 192.168.1.1

 

Tip: write to the configuration, the Network Service Restart and reboot the system will take effect!

 

 

 

 

Method Two:

 

vi / etc / sysconfig / static-routes # default file does not exist

 

Add the following:

 

any net 192.168.1.0/24 gw 192.168.1.1

 

Tip: write to the configuration, the Network Service Restart and reboot the system will take effect!

 

 

 

 

Method three:

 

we /etc/rc.local

 

Add the following:

 

route add -net 192.168.1.0/24 gw 192.168.1.1

 

PS: a recommended method for production environments

 

Tip: Method three /etc/rc.local Lane wrote only loaded at boot time, when manually restart the network will fail, but will take effect after reboot the system!

 

 

If you are configuring the default route gateway can then configure the card inside:

[root@oldboy ~]# grep GATEWAY /etc/sysconfig/network-scripts/ifcfg-eth0

GATEWAY=10.0.0.254

 

. C Host Routing: is destined for a host address how to configure routing

 

/sbin/route add -host 192.168.2.13 dev eth2

 

/sbin/route add -host 202.81.11.91 dev lo

 

For example: using between keepalived highly available server or a separate NIC heartbeat connection heartbeat communication routes will be used more hosts.

expand the route command:

 

Delete a default route:

 

route del default gw 10.0.0.254

 

To delete a static route:

 

route del & ndash; net target network netmask

 

Such as: route del -net 192.168.1.0/24 or route del -net 192.168.1.0 netmask 255.225.255.0

 

To delete a host route:

 

route del -host 192.168.1.10 dev eth0

 

Further details about the route we need to execute the command man route access help, and carefully summarized.

 

On this question, we are talking about a production network division and multisegment routing Environmental Solutions (1000 machine network solutions division). We can feel the different functions Applications route command.

A test question: How to add routes under linux (Baidu interview questions)

 

These are the original title, old boy teacher translated into the following three questions.

 

a. How to use the command line to add a default gateway linux machine, assuming that the gateway address 10.0.0.254?

b. 192.168.1.0 network segment, a gateway server 192.168.1.1 think even the 172.16.1.0/24 segment, how to add routes (Qihoo 360)

c. If you add a host route?

Please answer separately.

Answer: route  -net 172.16.1.0/24 gw 192.168.1.1

route command to use:

 

a. Default Gateway Routing

 

    The default gateway is the packet does not match any routing rule set, and finally flows through the address mark! Gateway literally is the network gateway, the equivalent of our house door of the house, as if we should go through the door, the packet is the same.

 

The answer to this question:

 

route del default gw 10.0.0.254

 

Answers practice:

 

[root @ Oldboy ~] # route -n  # ==> to view the routing table, netstat -rn can.

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

# ==> this is the default gateway information systems, expressed go anywhere (0.0.0.0), are sent to 10.0.0.254, because it is the default gateway, therefore, on the last one. There is also the order of the route, if it does not meet any of these rules to the default gateway process.

 

[root @ Oldboy ~] # route del default gw 10.0.0.254  # ==> this command is to delete the default gateway.

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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 In 0 0 0 eth0

 

[root @ Oldboy ~] # the Add default route gw 10.0.0.254     # ==> This command is to add the default gateway, also answer to this question.

 

[root@oldboy ~]# netstat -rn

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

 

10.0.0.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 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 # ==> back

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.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 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 # this is the default gateway added record.

 

With particular emphasis on: Actually route add default gw 10.0.0.254 equivalent route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.0.254

 

b network route: i.e. route to a network or network segments destined

 

    General multi-segment communicate with each other, want to establish a priority route, rather than through the default gateway can configure the network routing. Or take the house analogy, you are not going out, but the bedroom, bathroom, bedroom to go through the bedroom door, went to the bathroom is also subject to the bathroom door, the bedroom and the bathroom door here can be considered to be destined for a routing segment, rather than the default route (ie door of the house.)

 

    The actual work will be exchange of visits between the two different internal network needs, rather than the network access, the above example is the case.

 

    The answer to this question:

 

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

Answers practice:

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

SIOCADDRT: network unreachable # ==> when the address 192.168.1.1 while even unreasonable, can not add routes.

 

[root @ Oldboy ~] # ifconfig eth0: 0 192.168.1.1/24 up  # ==> Add IP alias for a temporary test, if made permanent plus the best two-card or write to the configuration file.

 

[root @ Oldboy ~] # ifconfig eth0: 0  # ==> view the added IP alias (This multi-IP network in the manner called sub-interfaces)

 

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:65:A4:FD 

 

          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0

 

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 

Again add a packet to 192.168.1.0 and 192.168.1.1 to deal with.

 

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

[root @ Oldboy ~] # netstat -rn    # ==> route -n and the like.

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

 

10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

 

192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0 # ==> This is the network routing

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

169.254.0.0 0.0.0.0 255.255.0.0 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

Development: Other writing

 

[root @ Oldboy ~] # route the Add -net 192.168.1.0 Netmask 255.255.255.0 dev eth0   # ==> specified device instead of an address.

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

169.254.0.0 0.0.0.0 255.255.0.0 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

[root@oldboy ~]# route del -net 192.168.1.0/24 dev eth0   

 

[root@oldboy ~]# route add -net 192.168.1.0/24 dev eth0   

 

[root@oldboy ~]# route -n

 

Kernel IP routing table

 

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

 

10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

0.0.0.0 192.168.1.0 255.255.255.0 U 0 0 0 eth0

 

169.254.0.0 0.0.0.0 255.255.0.0 In 0 0 0 eth0

 

0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0

 

to sum up:

 

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

 

route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

 

route add -net 192.168.1.0/24 dev eth0 

 

route del -net 192.168.1.0/24 dev eth0  

 

With particular emphasis on: the above configuration will fail when restart the network, then how to make it permanent?

 

If we are permanent, there are several ways:

method one:

 

vi / etc / sysconfig / network-scripts / route-eth0 # default file does not exist

 

Add the following:

 

192.168.1.0/24 via 192.168.1.1

 

Tip: write to the configuration, the Network Service Restart and reboot the system will take effect!

 

 

 

 

Method Two:

 

vi / etc / sysconfig / static-routes # default file does not exist

 

Add the following:

 

any net 192.168.1.0/24 gw 192.168.1.1

 

Tip: write to the configuration, the Network Service Restart and reboot the system will take effect!

 

 

 

 

Method three:

 

we /etc/rc.local

 

Add the following:

 

route add -net 192.168.1.0/24 gw 192.168.1.1

 

PS: a recommended method for production environments

 

Tip: Method three /etc/rc.local Lane wrote only loaded at boot time, when manually restart the network will fail, but will take effect after reboot the system!

 

 

If you are configuring the default route gateway can then configure the card inside:

[root@oldboy ~]# grep GATEWAY /etc/sysconfig/network-scripts/ifcfg-eth0

GATEWAY=10.0.0.254

 

. C Host Routing: is destined for a host address how to configure routing

 

/sbin/route add -host 192.168.2.13 dev eth2

 

/sbin/route add -host 202.81.11.91 dev lo

 

For example: using between keepalived highly available server or a separate NIC heartbeat connection heartbeat communication routes will be used more hosts.

expand the route command:

 

Delete a default route:

 

route del default gw 10.0.0.254

 

To delete a static route:

 

route del & ndash; net target network netmask

 

Such as: route del -net 192.168.1.0/24 or route del -net 192.168.1.0 netmask 255.225.255.0

 

To delete a host route:

 

route del -host 192.168.1.10 dev eth0

 

Further details about the route we need to execute the command man route access help, and carefully summarized.

 

On this question, we are talking about a production network division and multisegment routing Environmental Solutions (1000 machine network solutions division). We can feel the different functions Applications route command.

Guess you like

Origin www.cnblogs.com/longchengruoxi/p/11202554.html