首页 > 系统相关 >Linux 路由表详解

Linux 路由表详解

时间:2023-02-02 17:37:46浏览次数:45  
标签:0.0 route Destination 255.255 详解 Linux eth0 路由 路由表

一、查看路由表

复制代码
[root@VM_139_74_centos ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         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 eth0
10.139.128.0    0.0.0.0         255.255.224.0   U     0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.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-0ab63c131848
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bccbfb788da0
172.20.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-7485db25f958
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.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 eth0
10.139.128.0    0.0.0.0         255.255.224.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.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-0ab63c131848
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-bccbfb788da0
172.20.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-7485db25f958
复制代码

二、路由表各字段说明

Destination 目标网络或目标主机。Destination 为 default(0.0.0.0)时,表示这个是默认网关,所有数据都发到这个网关(这里是 10.139.128.1
Gateway 网关地址,0.0.0.0 表示当前记录对应的 Destination 跟本机在同一个网段,通信时不需要经过网关(同一个局域网内2台主机通信不需要经过网关)
Genmask Destination 字段的网络掩码,Destination 是主机时需要设置为 255.255.255.255,是默认路由时会设置为 0.0.0.0
Flags

标志

  • U - Up表示有效
  • G - Gateway表示连接路由,若无这个字段表示直连目的地址
  • H - Host表示目标是具体主机,而不是网段
  • R 恢复动态路由产生的表项
  • D 由路由的后台程序动态地安装
  • M 由路由的后台程序修改
  • ! 拒绝路由
 Metric  路由距离,到达指定网络所需的中转数,是大型局域网和广域网设置所必需的 (不在Linux内核中使用。)
 Ref  路由项引用次数 (不在Linux内核中使用。)
 Use  此路由项被路由软件查找的次数
 Iface  网卡名字,例如 eth0;要去往这个网段需要使用哪一个网络接口。也就是eth0这张网卡

 

三、linux路由的种类

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

[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.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 Iface
default         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),单位MB
  • window:指定通过路由表的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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
224.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 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
224.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.1
SIOCADDRT: Network is unreachable
[root@VM_139_74_centos ~]# route del default gw 192.168.1.1
SIOCDELRT: No such process

标签:0.0,route,Destination,255.255,详解,Linux,eth0,路由,路由表
From: https://www.cnblogs.com/zhuanqian-yangmeng/p/17086740.html

相关文章

  • linux网络知识:路由策略(ip rule,ip route)
    一,路由策略(使用iprule命令操作路由策略数据库)基于策略的路由比传统路由在功能上更强大,使用更灵活,它使网络管理员不仅能够根据目的地址而且能够根据报文大小,应用或IP源地......
  • Kroger EDI 850 采购订单报文详解
    本文着重讲述KrogerEDI项目中,Kroger发给供应商的X12850报文(采购订单)。在此前的文章 如何读懂X12 中,我们对X12已经做了详细的解读,接下来让我们以KrogerEDI项目中对X......
  • Python内置函数详解-总结篇
     一直以来都是现查现学Python的相关内置函数,想看看全部的汇总版本,最近还真发现有大佬早已把相关的内置函数全部汇总完毕了。博主十月狐狸将Python3.5版本中的68......
  • bootstrap suggest搜索建议插件使用详解
    近日因工作需要看了下此插件。首先下载bootstrapjs包。添加此插件的引用。注意css样式要引用,不能忘记。前台页面代码,因为楼主做的是选项卡切换查询不同的结果。......
  • Linux 的目录查看 tree
    一、安装Ubuntuapt-getinstalltreecentosyum-yinstalltree不是root权限别忘了加sudo二、使用最常用的几个命令:tree-L4#查看当前目录下4层tree-a......
  • mysql索引详解
    本文转载自https://javaguide.cn/database/mysql/mysql-index.html索引介绍索引是一种用于快速查询和检索数据的数据结构,其本质可以看成是一种排序好的数据结构。索引......
  • nginx 日志分析之 access.log 格式详解
    说明:access.log的格式是可以自己自定义,输出的信息格式在nginx.conf中设置一般默认配置如下:http{...log_formatmain'$remote_addr-$remote_user[$time_lo......
  • Linux运维的简单解释与分析
    运维解释:这里指互联网运维,通常属于技术部门,与研发、测试、系统管理同为互联网产品技术支撑的四大部门,这个划分在国内和国外以及大小公司间都会有不同。一个互联网产品的......
  • JavaScript的this指向详解
    一、概念:函数的上下文(this)由调用函数的方式决定,function是“运行时上下文”策略;函数如果不调用,则不能确定函数的上下文。二、规则:对象打点调用它的方法函数,......
  • linux英文词典推荐
    在windows下一般用有道就可以应付绝大部分问题了,其他的一些词典在windows下表现也都不错,但是在linux很多词典并不好用。有道有道词典推出了linux版本,但是都是很久之前的,......