route 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

route 命令安装:


-bash/zsh: route: command not found

#Debian
apt-get install net-tools

#Ubuntu
apt-get install net-tools

#Alpine
apk add net-tools

#Arch Linux
pacman -S net-tools

#Kali Linux
apt-get install net-tools

#CentOS
yum install net-tools

#Fedora
dnf install net-tools

#Raspbian
apt-get install net-tools

#Docker
docker run cmd.cat/route route

route 命令补充说明:


在计算机网络中,路由器是负责转发网络流量的设备。 当数据报到达路由器时,路由器必须确定将其路由到目的地的最佳方法。

route 命令用来显示并设置 Linux 内核中的网络路由表,route 命令设置的路由主要是静态路由。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。

在 Linux 系统中设置路由通常是为了解决以下问题:

  • 该 Linux 系统在一个局域网中,局域网中有一个网关,能够让机器访问 Internet,那么就需要将这台机器的 IP 地址设置为 Linux 机器的默认路由。
  • 要注意的是,直接在命令行下执行 route 命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在 /etc/rc.local 中添加 route 命令来保证该路由设置永久有效。

在 Linux、BSD 和其他类似 Unix 的系统上,route 命令用于查看和更改内核路由表。 在不同的系统上,命令语法不同。 在这里,当涉及特定的命令语法时,我们将讨论 Linux 版本。

route 技术说明:


route 操作内核的 IP 路由表。 它的主要用途是在使用 ifconfig 程序配置接口后,通过接口设置到特定主机或网络的静态路由。

使用 add 或 del 选项时,route 修改路由表。 如果没有这些选项,则 route 将显示路由表的当前内容。

route 命令语法:


route [-CFvnee]

route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] 
      [metric N] i [mss M] [window W] [irtt m] [reject] [mod] [dyn] 
      [reinstate] [[dev] If]

route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] 
      [metric N] [[dev] If]

route [-V] [--version] [-h] [--help]

route 命令选项:


-A:设置地址类型;
-C:打印将Linux核心的路由缓存;
-v:详细信息模式;
-n:不执行DNS反向查找,直接显示数字形式的IP地址;
-e:netstat格式显示路由表;
-net:到一个网络的路由表;
-host:到一个主机的路由表。

route 命令参数:


Add:增加指定的路由记录;
Del:删除指定的路由记录;
Target:目的网络或目的主机;
gw:设置默认网关;
mss:设置TCP的最大区块长度(MSS),单位MB;
window:指定通过路由表的TCP连接的TCP窗口大小;
dev:路由记录所表示的网络接口。

route 命令实例


route 显示当前路由:

[root@localhost ~]
$ route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
112.124.12.0    *               255.255.252.0   U     0      0        0 eth1
10.160.0.0      *               255.255.240.0   U     0      0        0 eth0
192.168.0.0     10.160.15.247   255.255.0.0     UG    0      0        0 eth0
172.16.0.0      10.160.15.247   255.240.0.0     UG    0      0        0 eth0
10.0.0.0        10.160.15.247   255.0.0.0       UG    0      0        0 eth0
default         112.124.15.247  0.0.0.0         UG    0      0        0 eth1

[root@localhost ~]
$ route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
112.124.12.0    0.0.0.0         255.255.252.0   U     0      0        0 eth1
10.160.0.0      0.0.0.0         255.255.240.0   U     0      0        0 eth0
192.168.0.0     10.160.15.247   255.255.0.0     UG    0      0        0 eth0
172.16.0.0      10.160.15.247   255.240.0.0     UG    0      0        0 eth0
10.0.0.0        10.160.15.247   255.0.0.0       UG    0      0        0 eth0
0.0.0.0         112.124.15.247  0.0.0.0         UG    0      0        0 eth1

route 中 Flags 为路由标志,标记当前网络节点的状态,Flags 标志说明

# 含义
U Up, 表示此路由当前为启动状态。
H Host, 表示此网关为一主机。
G Gateway, 表示此网关为一路由器。
R Reinstate Route, 使用动态路由重新初始化的路由。
D Dynamically, 此路由是动态性地写入。
M Modified, 此路由是由路由守护程序或导向器动态修改。
! 表示此路由当前为关闭状态。

route 添加网关/设置网关:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 #增加一条到达244.0.0.0的路由。

route 屏蔽一条路由:

route add -net 224.0.0.0 netmask 240.0.0.0 reject   #增加一条屏蔽的路由,目的地址为224.x.x.x将被拒绝。

route 删除路由记录:

route del -net 224.0.0.0 netmask 240.0.0.0
route del -net 224.0.0.0 netmask 240.0.0.0 reject

route 删除和添加设置默认网关:

route del default gw 192.168.120.240
route add default gw 192.168.120.240

route 命令扩展阅读:




route 命令评论