您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页linux路由表详解

linux路由表详解

来源:华佗小知识
linux路由表详解

⼀、查看路由表

[root@VM_139_74_centos ~]# routeKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Ifacedefault gateway 0.0.0.0 UG 0 0 0 eth0

10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth010.139.128.0 0.0.0.0 255.255.224.0 U 0 0 0 eth0link-local 0.0.0.0 255.255.0.0 U 1002 0 0 eth0172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0

172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-0ab63c131848172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-bccbfb788da0172.20.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-7485db25f958[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 10.139.128.1 0.0.0.0 UG 0 0 0 eth0

10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth010.139.128.0 0.0.0.0 255.255.224.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0

172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-0ab63c131848172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-bccbfb788da0172.20.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-7485db25f958

⼆、路由表各字段说明

DestinationGatewayGenmask

⽬标⽹络或⽬标主机。Destination 为 default(0.0.0.0)时,表⽰这个是默认⽹关,所有数据都发到这个⽹关(这⾥是 10.139.128.1)

⽹关地址,0.0.0.0 表⽰当前记录对应的 Destination 跟本机在同⼀个⽹段,通信时不需要经过⽹关(同⼀个局域⽹内2台主机通信不需要经过⽹关)

Destination 字段的⽹络掩码,Destination 是主机时需要设置为 255.255.255.255,是默认路由时会设置为 0.0.0.0标志

U - Up表⽰有效

G - Gateway表⽰连接路由,若⽆这个字段表⽰直连⽬的地址H - Host表⽰⽬标是具体主机,⽽不是⽹段R 恢复动态路由产⽣的表项

D 由路由的后台程序动态地安装M 由路由的后台程序修改! 拒绝路由

路由距离,到达指定⽹络所需的中转数,是⼤型局域⽹和⼴域⽹设置所必需的 (不在Linux内核中使⽤。) 路由项引⽤次数 (不在Linux内核中使⽤。) 此路由项被路由软件查找的次数

⽹卡名字,例如 eth0;要去往这个⽹段需要使⽤哪⼀个⽹络接⼝。也就是eth0这张⽹卡

Flags

Metric Ref Use Iface

三、linux路由的种类

1、主机路由,路由表中指向单个 IP 地址或主机名的路由记录,其 Flags 字段为 H。下⾯⽰例中,对于 10.0.0.10 这个主机,通过⽹关 10.139.128.1 ⽹关路由:

[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth0...

2、⽹络路由,主机可以到达的⽹络。下⾯⽰例中,对于 10.0.0.0/24 这个⽹络,通过⽹关 10.139.128.1 ⽹关路由:

[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface10.0.0.0 10.139.128.1 255.255.255.0 UG 0 0 0 eth0

3、默认路由,当⽬标主机的 IP 地址或⽹络不在路由表中时,数据包就被发送到默认路由(默认⽹关)上。默认路由的 Destination 是 default或 0.0.0.0。

[root@VM_139_74_centos ~]# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Ifacedefault gateway 0.0.0.0 UG 0 0 0 eth0

四、route命令

选项:

-A:设置地址类型

-C:打印 Linux 内核的路由缓存-v:显⽰详细信息

-n:不执⾏ DNS 反向查找,直接显⽰数字形式的 IP 地址-e:netstat 格式显⽰路由表-net:到⼀个⽹络的路由表-host:到⼀个主机的路由表

参数:

add:增加路由记录del:删除路由记录

target:⽬的⽹络或⽬的主机gw:设置默认⽹关

mss:设置TCP的最⼤区块长度(MSS),单位MBwindow:指定通过路由表的TCP连接的TCP窗⼝⼤⼩dev:路由记录所表⽰的⽹络接⼝

1、添加主机路由,添加主机路由时,需要指定⽹络 ID 和主机 ID,此时需要设置 netmask 255.255.255.255:

[root@VM_139_74_centos ~]# route add -net 10.0.0.10 netmask 255.255.255.255 gw 10.139.128.1 dev eth0[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth0...

2、添加⽹络路由,添加⽹络路由时,只需指定⽹络 ID,通过 netmask 设置掩码长度:

[root@VM_139_74_centos ~]# route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.139.128.1 dev eth0[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface10.0.0.0 10.139.128.1 255.255.255.0 UG 0 0 0 eth0...

3、添加同⼀个局域⽹的主机,不指定 gw 选项时,添加的路由记录不使⽤⽹关:

[root@VM_139_74_centos ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface224.0.0.0 0.0.0.0 240.0.0.0 U 0 0 0 eth0...

4、屏蔽路由

[root@VM_139_74_centos ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject[root@VM_139_74_centos ~]# route -nKernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface224.0.0.0 - 240.0.0.0 ! 0 - 0 -...

5、删除可⽤路由

route del -net 224.0.0.0 netmask 240.0.0.0

6、删除屏蔽路由

route del -net 224.0.0.0 netmask 240.0.0.0 reject

7、删除和添加设置默认⽹关,添加或删除默认⽹关时,Linux 会⾃动检查⽹关的可⽤性:

[root@VM_139_74_centos ~]# route add default gw 192.168.1.1SIOCADDRT: Network is unreachable

[root@VM_139_74_centos ~]# route del default gw 192.168.1.1SIOCDELRT: No such process

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务