举荐浏览:每天学一个 Linux 命令(62):ping

命令简介

route 命令用于显示和设置linux零碎的路由表(动态路由表)。

route 命令用来显示并设置 Linux 内核中的网络路由表,route 命令设置的路由次要是动态路由。在 Linux、BSD 和其余相似 Unix 的零碎上,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]

选项阐明

-A  #指定地址类型-C  #打印将Linux外围的路由缓存-v  #输入详细信息-n  #不执行DNS反向查找,间接显示数字模式的IP地址-e  #netstat格局显示路由表-net  #到一个网络的路由表-host  #到一个主机的路由表Add  #减少指定的路由记录Del  #删除指定的路由记录Target  #目标网络或目标主机gw  #设置默认网关mss  #设置TCP的最大区块长度(MSS),单位MBwindow  #指定通过路由表的TCP连贯的TCP窗口大小dev  #路由记录所示意的网络接口

利用举例

显示以后零碎的路由表

[root@CentOS7-1 ~]# routeKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Ifacedefault         gateway         0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

其中 Flags 为路由标记,标记以后网络节点的状态,Flags 标记阐明如下:

U  #Up 示意此路由以后为启动状态H  #Host 示意此网关为一主机G  #Gateway 示意此网关为一路由器R  #Reinstate Route 应用动静路由从新初始化的路由D  #Dynamically 此路由是动态性地写入M  #Modified 此路由是由路由守护程序或导向器动静批改!  #示意此路由以后为敞开状态

增加网关/设置网关

[root@CentOS7-1 ~]# route add -net 192.168.2.0 netmask 255.255.255.0 dev ens33[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 ens33#减少一条达到192.168.2.0子网的路由。

屏蔽一条路由

[root@CentOS7-1 ~]# route add -net 192.168.3.0 netmask 255.255.255.0 reject[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 ens33192.168.3.0     -               255.255.255.0   !     0      -        0 -   #减少一条屏蔽的路由,目标地址为192.168.3.0子网将被回绝。

删除路由记录

[root@CentOS7-1 ~]# route del -net 192.168.2.0 netmask 255.255.255.0[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33192.168.3.0     -               255.255.255.0   !     0      -        0 -[root@CentOS7-1 ~]# route del -net 192.168.3.0 netmask 255.255.255.0 reject[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

删除和增加设置默认网关

[root@CentOS7-1 ~]# route add default gw 192.168.1.1[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 ens330.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33[root@CentOS7-1 ~]# route del default gw 192.168.1.1[root@CentOS7-1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

每天学一个 Linux 命令(60):scp

每天学一个 Linux 命令(61):wget