Linux命令学习手册-route命令
route [-CFvnee]
route [-v] [-A family] 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] del [-net|-host] target [gw Gw] [netmask Nm] [metric N] [[dev] If]
route [-V] [--version] [-h] [--help]
[功能]
设定和操作路由器表。
[描述]
route用来操作管理内核的IP路由表。它的作用是在使用ifconfig命令配置好网络接口之后,通过网络接口设定到特定主机或者网络的静态路由表。
当使用add或者del选项的时候,route修改路由表。如果没有这些选项,那么route会显示当前路由表中的内容。
常用项:
-n:不使用主机名称而直接使用主机IP或端口号。
-ee:使用详细信息显示。
增加(add)和删除(del)选项的参数:
-net:表示后面所指定的是为一个网络。
-host:表示后面所指定的是一个主机的路由。
netmask:和网络相关,可以设定netmask来决定网络大小。
gw:表示gateway(网关),后面连接其IP。
dev:指定连接的设备(网卡),例如eth0。
[举例]
1、需要预先了解的内容:
(1)路由可为各种品牌路由(例如cisco),PC为访问网络的设备(例如主机)。
(2)路由器上面至少有两类网口,一个可连接WAN,一个可连接PC组成LAN。
(3)PC只有一个网口,要么连接WAN,要么连接路由组成LAN。
(4)路由可与PC直连,与之组成LAN;路由也可再与WAN相连,使其LAN内主机可经它访问WAN;也可PC直连到WAN。
(5)内网:又叫LAN,是路由和PC组成的内部局域网,其网内主机假设为172.168.1.*。
(6)外网:又叫WAN,是外部一个更大的局域网,其网内主机假设为10.1.10.*。
(7)PC内网网址(LAN):可静态指定,也可通过路由的dhcp服务动态获取(例如172.168.1.105)。
(8)路由内网网址(LAN):连接到路由的PC以它作为默认网关,PC也可通过它登陆并配置路由(如172.168.1.14)。
(9)PC外网网址(WAN):可静态指定,也可通过WAN网中的dhcp服务动态获取(如10.1.10.182)。(似PC从路由)
(10)路由的外网网址(WAN):可静态指定,也可通过WAN网中的dhcp服务获取(如10.1.10.32)。(似PC从外网)
2、当把路由器插连接WLAN,然后pc连接路由器组成LAN之后:
pc的网址(LAN):172.168.1.105(可以pc自己指定静态的,也可通过路由的dhcp服务动态获取)。
cisco的网址(LAN):172.168.1.14。
pc的网关设置成(LAN):172.168.1.14。
*查看PC机器ip信息:
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:24:7e:03:62:14
inet addr:172.168.1.105 Bcast:172.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::224:7eff:fe03:6214/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1776511 errors:12 dropped:1 overruns:0 frame:8
TX packets:1174511 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:934065225 (934.0 MB) TX bytes:107898203 (107.8 MB)
Interrupt:16 Memory:fc500000-fc520000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:310438 errors:0 dropped:0 overruns:0 frame:0
TX packets:310438 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:28063379 (28.0 MB) TX bytes:28063379 (28.0 MB)
*查看当前路由如下:
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.168.1.14 0.0.0.0 UG 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
172.168.1.0 * 255.255.255.0 U 1 0 0 eth0
可以看到机器路由,其中(其他参见man手册):
Destination:目标网络或者主机。
Gateway:网关地址,如果没有设置则为*。
Genmask:目标网络掩码;如果目标主机则用"255.255.255.255",如果默认路由则用"0.0.0.0"。
Flags标志说明:
U Up表示此路由当前为启动状态
H Host,表示此网关为一主机
G Gateway,表示此网关为一路由器
R Reinstate Route,使用动态路由重新初始化的路由
D Dynamically,此路由是动态性地写入
M Modified,此路由是由路由守护程序或导向器动态修改
! 表示此路由当前为关闭状态
Metric:到目标的距离(一般为跳数),当前kernel可能不用,但是路由守护进程可能会需要它。
Ref:此路由引用数目(当前kernel不用)。
Use:对此路由的查找。
Iface:对于这个路由,数据包将要发送到那个接口(网卡)。
*查看路由信息的另外一个方法:
# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.168.1.14 0.0.0.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
这里,信息类似,速度稍快,不象route需要等待一段时间,(区别是什么?)。然后可以用如下命令查看连通性:
# ping 10.1.10.100
PING 10.1.10.100 (10.1.10.100) 56(84) bytes of data.
From 172.168.1.14 icmp_seq=1 Destination Net Unreachable
From 172.168.1.14 icmp_seq=2 Destination Net Unreachable
可见无法ping通(这里10.1.10.100是外网的一台机器),原因是因为没有给route插入外网网线,而是只将route的lan口与机器相连,当将外网和route的wan口相连之后,就可以ping通了。
# ping 172.168.1.14
PING 172.168.1.14 (172.168.1.14) 56(84) bytes of data.
64 bytes from 172.168.1.14: icmp_req=1 ttl=64 time=0.457 ms
64 bytes from 172.168.1.14: icmp_req=2 ttl=64 time=0.425 ms
可见可以ping通本网段(这里172.168.1.14是路由器)。
3、让pc直接连接到外部网络,不经过路由器:
pc的网址:10.1.10.182。
外网其网内主机ip是10.1.10.*。
*查看PC基本信息:
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:24:7e:03:62:14
inet addr:10.1.10.182 Bcast:10.1.10.255 Mask:255.255.255.0
inet6 addr: fe80::224:7eff:fe03:6214/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1776739 errors:12 dropped:2 overruns:0 frame:8
TX packets:1174650 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:934090706 (934.0 MB) TX bytes:107913085 (107.9 MB)
Interrupt:16 Memory:fc500000-fc520000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:310442 errors:0 dropped:0 overruns:0 frame:0
TX packets:310442 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:28063715 (28.0 MB) TX bytes:28063715 (28.0 MB)
*查看PC路由信息:
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default ns.amazon.esdl. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.1.10.254 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
在这个路由信息的基础上,测试连通性:
# ping 10.1.10.119
PING 10.1.10.119 (10.1.10.119) 56(84) bytes of data.
64 bytes from 10.1.10.119: icmp_req=1 ttl=64 time=3.72 ms
这里,10.1.10.119是网内另外一台主机。
*添加一个192.168.1.0网络:
# route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default ns.amazon.esdl. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
*再添加192.0.0.0和192.0.0.0网络:
# route add -net 192.0.0.0 netmask 255.255.0.0 dev eth0
# route add -net 192.0.0.0 netmask 255.0.0.0 dev eth0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default ns.amazon.esdl. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.0.0.0 * 255.255.0.0 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
这里,网络地址结合子网掩码来标识。
*删除192.0.0.0:
# route del 192.0.0.0
SIOCDELRT: No such process
# route del -net 192.0.0.0
SIOCDELRT: Invalid argument
# route del -net 192.0.0.0 netmask 255.255.0.0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default ns.amazon.esdl. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.0.0.0 * 255.255.255.0 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
# route del -net 192.0.0.0 netmask 255.255.255.0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway.amazon. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
从这里可知,如果没指定子网掩码,那么如果两个一样的ip的网络就无法删除成功。
*添加一台主机:
# route add -host 10.1.10.47 dev eth0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway.amazon. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
quietheart * 255.255.255.255 UH 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.1.10.254 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.1.10.47 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
# ping 10.1.10.47
PING 10.1.10.47 (10.1.10.47) 56(84) bytes of data.
64 bytes from 10.1.10.47: icmp_req=1 ttl=64 time=8.02 ms
64 bytes from 10.1.10.47: icmp_req=2 ttl=64 time=9.49 ms
从这里我们可以看出,主机10.1.10.47名称是"quietheart",注意,添加主机的时候,不指定netmask。添加之后,仍然可以ping通。
*添加一个默认的网关:
# route add default gw 10.1.10.1
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.1.10.1 0.0.0.0 UG 0 0 0 eth0
default gateway.amazon. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
quietheart * 255.255.255.255 UH 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
这里,默认的路由"default"在没有其他路由匹配的时候,会被使用到,可以有特殊的命令修改默认路由(例如"route add default gw mango-gw"会添加mango-gw主机),具体可以参见"man"手册。
*删除一个默认网关:
# route del default gw 10.1.10.1
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway.amazon. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
quietheart * 255.255.255.255 UH 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
*为一台主机添加一个特定网关:
# route add -host 10.1.10.47 gw 10.1.10.1 dev eth0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway.amazon. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
quietheart 10.1.10.1 255.255.255.255 UGH 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
这里10.1.10.47的主机名称为quietheart,这里如果gw后面指定的ip地址是不存在的,则命令运行失败,例如:
# route add -host 10.1.10.47 gw 172.168.1.1 dev eth0
SIOCADDRT: No such process
这里,172.168.1.1是不存在的,但是192.168.1.1是存在的,因为路由表中有192.168.1.0的网络,可是实际上192.168.1.1不可达。
*添加一个不可达的路由也无法正常工作,例如:
# route add -host 10.1.10.47 gw 192.168.1.1 dev eth0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default ns.amazon.esdl. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
quietheart 192.168.1.1 255.255.255.255 UGH 0 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
# ping 10.1.10.47
PING 10.1.10.47 (10.1.10.47) 56(84) bytes of data.
因为192.168.1.1不可达,所以即使添加上去了,也不能正常工作。
*删除一个主机:
# route del -host 10.1.10.47 dev eth0
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default ns.amazon.esdl. 0.0.0.0 UG 0 0 0 eth0
10.1.10.0 * 255.255.255.0 U 1 0 0 eth0
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
[其它]
待更新。
参考资料:
http://www.2cto.com/os/201203/125081.html
http://blog.chinaunix.net/uid-22646981-id-3048260.html
man手册
Linux命令学习手册-route命令的更多相关文章
- Linux命令学习手册-printf命令(转)
分类: LINUX 参考资料:http://sns.linuxpk.com/space-566-do-blog-id-15819.html printf FORMAT [ARGUMENT]... pr ...
- (转) Linux命令学习手册-arp命令
arp 原文:http://blog.chinaunix.net/uid-9525959-id-3318814.html [功能] 管理系统的arp缓存. [描述] 用来管理系统的arp缓存,常用的命 ...
- 【转】Linux命令学习手册-split命令
转自:http://blog.chinaunix.net/uid-9525959-id-3054325.html split [OPTION] [INPUT [PREFIX]] [功能]将文件分割成多 ...
- 《Linux命令学习手册》系列分享专栏
<Linux命令学习手册>系列分享专栏 <Linux命令学习手册>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/207 ...
- Git版本控制软件结合GitHub从入门到精通常用命令学习手册(转)
简要参考:http://www.tuicool.com/articles/mEvaq2 http://gitref.org/zh/index.html GIT 学习手册简介 本站为 Git 学习参考手 ...
- Linux命令学习神器!命令看不懂直接给你解释!
大家都知道,Linux 系统有非常多的命令,而且每个命令又有非常多的用法,想要全部记住所有命令的所有用法,恐怕是一件不可能完成的任务. 一般情况下,我们学习一个命令时,要么直接百度去搜索它的用法,要么 ...
- linux命令总结之route命令
route命令用于显示和操作IP路由表.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现.在Linux系统中,设置路由通常是 为了解决以下问题:该Linu ...
- linux shell 学习笔记--内部命令学习
.基本命令 新手必须要掌握的初级命令 ls 基本的列出所有文件的命令.但是往往就是因为这个命令太简单,所以我们总是低估它.比如 ,用 -R 选项,这是递归选项,ls 将会以目录树的形式列出所有文件, ...
- Linux基础学习之基础命令(1)--2019-11-14
查看命令路径其他方法: which 命令: which [options] [--] programname [...] -a:显示所有匹配的程序文件,而非第一个: --skip-alias:略过别名 ...
随机推荐
- list的迭代器能解决并发问题,collection 的迭代器不能解决并发问题,for可以解决并发问题
list的迭代器能解决并发问题,collection 的迭代器不能解决并发问题 为什么list支持add,collection不支持 例如有两个人同时添加第三个元素 list的迭代器能锁定线程 只有等 ...
- HDU 6035 Colorful Tree(dfs)
题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...
- 【bzoj3203】[Sdoi2013]保护出题人 凸包+二分
题目描述 输入 第一行两个空格隔开的正整数n和d,分别表示关数和相邻僵尸间的距离.接下来n行每行两个空格隔开的正整数,第i + 1行为Ai和 Xi,分别表示相比上一关在僵尸队列排头增加血量为Ai 点的 ...
- 洛谷P1943 LocalMaxima_NOI导刊2009提高(1)(分段打表)
显然只需要算出每个数比前面所有数大的期望然后全部加起来就好了,一个数的期望怎么算呢? 对于一个数我们需要考虑比它大的数,因为比它小的数放它前面放它后面都可以,但是比它大的数只能放它后面.考虑大于等于它 ...
- 框架----Django之文件上传
一.文件上传 1. 浏览器访问 http://127.0.0.1:8000/f1/ http://127.0.0.1:8000/f2/ 2. urls from django.conf.urls im ...
- Servlet3.0 新特性
Servlet3.0 的注解 Servlet 允许开发人员采用注解的方式来配置 Servlet.Filter.Listener. Servlet3.0 规范在 javax.servlet.annota ...
- LVS三种模式的区别及负载均衡算法
LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,是一个虚拟的服务器集群系统,由章文嵩博士在1998年5月成立,在linux2.6+后将lvs自动加入了kernel ...
- 通过循环判断size()清理queue的问题
今天犯了个二逼问题,我想清理一个queue里对象,用了以下方法: ;i<objQueue.size();++i) { T* p_obj = objQueue.front(); delete p_ ...
- github访问很慢的问题
公司一直用着svn, 之前也的确用过github的版本管理,但是一直都是可视化的操作 这几天面试了几名前端,问了一下发现他们在之前的公司里都是用git的, 于是今天好好温故了一下怎么用命令行进行一下g ...
- 原生JS实现点击一个按钮显示一个div,再点击按钮div隐藏,或点击除div外其它空白处div隐藏
<!DOCTYPE html> <html style="font-size: 24px"> <head> <title>js点击按 ...