Linux之(23)route命令

Linux之(23)理由命令总结

Author:OnceDay Date:2023年2月14日

漫漫长路,有人对你微笑过嘛…

参考文档:

1.概述

Route操作内核的IP路由表。它的主要用于设置到特定主机或网络的静态路由。

默认情况下,Route显示当前路由表的内容。

onceday@ubuntu:~$ route --help
Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} [<AF>]              Detailed usage syntax for specified AF.
       route {-V|--version}                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  <AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
    x25 (CCITT X.25)

一般使用的命令格式有下面几种:

route [-CFvnNee] [-A family |-4|-6]
route  [-v] [-A family |-4|-6] add [-net|-host] target [netmask
              Nm] [gw Gw] [metric N] [mss M] [window W] [irtt I]
              [reject] [mod] [dyn] [reinstate] [[dev] If]
route  [-v] [-A family |-4|-6] del [-net|-host] target [gw Gw]
              [netmask Nm] [metric M] [[dev] If]
route  [-V] [--version] [-h] [--help]

可以通过下面的文件来查看相关的路由信息:

文件路径描述
/proc/net/ipv6_routeIPv6路由表信息
/proc/net/routeIPv4路由表信息
/proc/net/rt_cache路由表缓存信息
1.2 配置永久性路由

似乎新的Linux发行版没看到这些文件,可行性有待进一步测试

直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;要想永久保存,有如下方法:

  • 在/etc/rc.local里添加。
  • 在/etc/sysconfig/network里添加到末尾。
  • /etc/sysconfig/static-router 。

大致思路是写个开机执行的脚本自动刷配置。

2.选项

支持的参数如下:

参数含义
-A family使用特定的地址族(如inet),可以使用-6-4代替--inet6-A inet
-F在内核的FIB(FORwarding Information Base)路由表上操作。这是默认选项。
-C在内核的路由缓存上操作。
-v选择丰富信息模式。
-n显示原始数字化的地址,不会进行去解析地址,从而获取对应的域名。
-e使用netstat格式来显示路由表,-ee将展示最多的信息。
del删除一个路由
add添加一个新的路由
target目标网络或者主机名,可以提供地址/网络符号/主机名字。
可以使用/prefixlen标注代替使用网络掩码选项。
-net目标是一个网络
-host目标是一个主机名
netmask NM添加网络路由时使用的网络掩码。
gw GW通过网关来路由报文。指定的网关必须首先要能可达,所以通常事先需要建立一个静态路由到达网关。如果指定网络接口的地址,那么将用来决定报文从哪个接口被转发。
metric M指定网络路由的质量。默认情况下,IPv6值为1,IPv4值为0。Iproute2的默认值有所区别,因此最好设定时总是显示的指定该值。
mss M设置MTU(Maximum transmission Unit)值,单位为bytes。
Note that the current implementation of the route command does not allow the option to set the Maximum Segment Size (MSS).
window W设置TCP窗口值为Wbytes,通常用于AX.25网络,在它的驱动无法处理back to backframes时。
irrt I设置初始化往返时间(initial round trip time),用于基于指定路由之上的TCP连接,范围是(1~12000)ms。通常用于AX.25网络,省略时通常使用RPC1122指定的300ms默认值。
reject安装一个阻塞路由(blocking route),它强制使一个路由查找失败。这通常用于去掩盖一个网络,而不是用于防火墙。
mod,dyn,reinstate安装一个动态或者可编辑的路由。通常用于诊断目的,仅由路由的守护进程创建。
dev if强制路由和指定的设备联系起来。否则,内核将尝试去决定设备接口,基于其自己的规则,通过检查已存在的路由和特定的装置,以及路由从何处添加而来。正常的网络中不需要设置这个。
3. 实例
3.1 全部路由信息输出

在这里插入图片描述

其标志字段含义如下:

  • Destination,目的网络或者目的主机地址(主机地址不需要网络掩码后缀)。
  • Gateway,网关地址或者未指定*
  • Genmask,目标网络的网络掩码,对于一个主机地址是255.255.255.255,对于默认路由default route0.0.0.0
  • Flags,可能的标志值:
    • U,(target is UP),路由有效。
    • H,(target is a host),目标是一个主机地址。
    • G,(use gateway),使用网关。
    • R,(reinstate route for dynamic routing),使用动态路由时,恢复路由资讯的旗标。
    • D,(dynamically installed by daemon or redirect),已经由服务或转 port 功能设定为动态路由。
    • M,(modified from routing daemon or redirect),路由已经被修改了。
    • A,(installed by addrconf),由addrconf安装。
    • C,(cache entry),缓存实体。
    • !,(reject route),拒绝查询的路由。
  • Metric,The ‘distance’ to the target (usually counted in hops).衡量路由质量,通常用到目的地的跳数来计数。
  • Ref,此条路由的引用计数(在Linux内核中未被使用)。
  • Use,此条路由查找的次数,通常由路由的使用次数决定。
  • Iface,这条路由上的报文将送往的网络接口。
  • MSS,Default maximum segment size for TCP connections over this route. 该路由上TCP连接默认最大的段长度。
  • Window,Default window size for TCP connections over this route.该路由上TCP连接默认的最大窗口大小。
  • irtt,初始化往返时间,内核使用该参数去评估最佳的TCP协议参数,从而不用等待较慢的回复报文。
  • HH,仅在Cache模式。缓存路由的ARP实体和路由数量,和硬件头部缓存有关。如果缓存路由的接口不需要硬件地址,那么该值为-1。如lo本地环回口就无硬件地址。
  • Arp,仅在Cache模式。缓存路由的硬件地址是否是最新的。
3.2 查看当前的路由

如下命令:

命令描述
route/route -F默认展示内核FIB的表项。
route -C展示路由缓存的表项
route -v展示更多的信息
route -n/route -N展示默认的内核FIB表项,并且不解析IP地址为符号化名字。
route -n -6展示IPv6的路由信息,且不解析IP地址。
route -46展示内核FIB里的IPv4和IPv6表项信息。
route -46 -n -ee不解析IP地址,且展示最多的信息和内容。
onceday@ubuntu:~$ route -46 -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.80.1     0.0.0.0         UG    0      0        0 eth0
172.17.80.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
172.17.80.0     0.0.0.0         255.255.240.0   U     0      0        0 eth0
Kernel IPv6 routing table
Destination                    Next Hop                   Flag Met Ref Use If
::1/128                        ::                         Un   0   2     0 lo
fe80::215:5dff:fe6d:b884/128   ::                         Un   0   2     0 eth0
fe80::/64                      ::                         U    256 1     0 eth0
ff00::/8                       ::                         U    256 3     0 eth0
::/0                           ::                         !n   -1  1     0 lo

状态标识可对比前面章节的说明,未涵盖标识可参考下面宏定义:

// IPv4 Linux宏定义
#define	RTF_UP			0x0001		/* route usable		  	*/
#define	RTF_GATEWAY		0x0002		/* destination is a gateway	*/
#define	RTF_HOST	    0x0004		/* host entry (net otherwise)	*/
#define RTF_REINSTATE	0x0008		/* reinstate route after tmout	*/
#define	RTF_DYNAMIC		0x0010		/* created dyn. (by redirect)	*/
#define	RTF_MODIFIED	0x0020		/* modified dyn. (by redirect)	*/
#define RTF_MTU			0x0040		/* specific MTU for this route	*/
#define RTF_MSS			RTF_MTU		/* Compatibility :-(		*/
#define RTF_WINDOW		0x0080		/* per route window clamping	*/
#define RTF_IRTT		0x0100		/* Initial round trip time	*/
#define RTF_REJECT		0x0200		/* Reject route			*/
//IPv6 Linux宏定义
#define RTF_DEFAULT		0x00010000	/* default - learned via ND	*/
#define RTF_ALLONLINK	0x00020000	/* (deprecated and will be removed)
					   fallback, no routers on link */
#define RTF_ADDRCONF	0x00040000	/* addrconf route - RA		*/
#define RTF_PREFIX_RT	0x00080000	/* A prefix only route - RA	*/
#define RTF_ANYCAST		0x00100000	/* Anycast			*/

#define RTF_NONEXTHOP	0x00200000	/* route with no nexthop	*/
#define RTF_EXPIRES		0x00400000

#define RTF_ROUTEINFO	0x00800000	/* route information - RA	*/

#define RTF_CACHE		0x01000000	/* read-only: can not be set by user */
#define RTF_FLOW		0x02000000	/* flow significant route	*/
#define RTF_POLICY		0x04000000	/* policy route			*/

#define RTF_PREF(pref)	((pref) << 27)
#define RTF_PREF_MASK	0x18000000

#define RTF_PCPU		0x40000000	/* read-only: can not be set by user */
#define RTF_LOCAL		0x80000000
3.3 添加路由
命令描述
route add -net 127.0.0.0 netmask 255.0.0.0 metric 1024 dev lo 本地回环口上配置网络路由,带有掩码。
route add -host 127.0.0.0 metric 1024 dev lo 本地环回口上配置主机路由,不需要掩码,或者掩码全为1.
route add default gw onceday.work添加一个默认网关地址,可以使用域名,会自动解析IP地址。网关IP需要直达才能设置成功
route add default gw onceday.work dev eth0在指定的网络接口上添加网关。
route add -net 10.0.0.0 netmask 255.0.0.0 metric 1024 reject安装一个拒绝路由,用于掩盖私用的10.0.0.0网络。
route add -host 172.17.81.2 metric 1024 gw 172.17.80.1 dev eth0添加一个通过网关的主机route,其绑定到设备eth0上,网关地址172.17.80.1要求是必须直达的。
route -6 add 2001:0002::/48 metric 1 dev eth0在接口eth0上指定一个metric为1的IPv6路由。

网关配置必须能直达才拥有,非直达的IP地址将配置失败

onceday@ubuntu:~$ sudo route add -host 172.17.81.2 metric 1024 gw 172.17.80.1 dev eth0
onceday@ubuntu:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         OnceDay.mshome. 0.0.0.0         UG    0      0        0 eth0
120.77.172.15   0.0.0.0         255.255.255.255 UH    1024   0        0 lo
127.0.0.0       0.0.0.0         255.255.255.255 UH    1024   0        0 lo
172.17.81.2     OnceDay.mshome. 255.255.255.255 UGH   1024   0        0 eth0
3.4 删除路由
命令描述
route del default删除当前的默认路由,其目的字段标记为default0.0.0.0
route del -net 127.0.0.0 netmask 255.0.0.0 删除指定的网络路由,带有网络掩码。
route del -net 172.17.80.0 netmask 255.255.255.0 dev eth0删除指定接口上的网路路由。
route del -host 127.0.0.0 dev lo删除指定接口上的主机路由。
route del -host 172.17.81.2 gw 172.17.80.1 dev eth0删除指定接口上的指定网关的默认路由。

删除之后。可以看到路由表的表项消失:

onceday@ubuntu:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
120.77.172.15   0.0.0.0         255.255.255.255 UH    1024   0        0 lo
172.17.80.0     0.0.0.0         255.255.240.0   U     0      0        0 eth0
172.17.81.2     OnceDay.mshome. 255.255.255.255 UGH   1024   0        0 eth0
onceday@ubuntu:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
120.77.172.15   0.0.0.0         255.255.255.255 UH    1024   0        0 lo
172.17.80.0     0.0.0.0         255.255.240.0   U     0      0        0 eth0
172.17.81.2     172.17.80.1     255.255.255.255 UGH   1024   0        0 eth0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值