- 浏览: 4729889 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
我使用过的Linux命令之route - 显示和操作IP路由表
本文链接:http://codingstandards.iteye.com/blog/1125312 (转载请注明出处)
用途说明
route命令用于显示和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。本文中的例子中会验证这一点。
常用参数
格式:route
格式:/sbin/route
用于打印路由表(display the current routing table)。
在非root用户使用时需要使用完整路径执行route命令。
格式:route -n
格式:/sbin/route -n
用于打印路由表,加上-n参数就是在输出的信息中不打印主机名而直接打印ip地址。像netstat命令也有此参数。
格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}
用于设置默认路由(adds a default route, which will be used if no other route matches),其中,
参数{IP-ADDRESS): 用于指定路由器(网关)的IP地址(Specify router IP address);
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0(Specify interface name such as eth0)。使用/sbin/ifconfig -a可以显示所有接口信息。
adds a default route (which will be used if no other route matches). All packets using this route will
be gatewayed through "mango-gw". The device which will actually be used for that route depends on how we
can reach "mango-gw" - the static route to "mango-gw" will have to be set up before.
格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
添加到指定网络的路由规则,其中
参数{NETWORK-ADDRESS}: 用于指定网络地址
参数{NETMASK}: 用于指定子网掩码
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。
adds a route to the network 192.56.76.x via "eth0". The Class C netmask modifier is not really necessary
here because 192.* is a Class C IP address. The word "dev" can be omitted here.
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
This is an obscure one documented so people know how to do it. This sets all of the class D (multicast)
IP routes to go via "eth0". This is the correct normal configuration line with a multicasting kernel.
格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject
设置到指定网络为不可达,避免在连接到这个网络的地址时程序过长时间的等待,直接就知道该网络不可达。
This installs a rejecting route for the private network "10.x.x.x."
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject
用于删除路由设置。参数指定的方式与route add相似。
route命令输出的路由表字段含义如下:
Destination 目标
The destination network or destination host. 目标网络或目标主机。
Gateway 网关
The gateway address or '*' if none set. 网关地址,如果没有就显示星号。
Genmask 网络掩码
The netmask for the destination net; '255.255.255.255' for a
host destination and '0.0.0.0' for the default route.
Flags Possible flags include 标志,常用的是U和G。
U (route is up) 路由启用
H (target is a host) 目标是主机
G (use gateway) 使用网关
R (reinstate route for dynamic routing)
D (dynamically installed by daemon or redirect)
M (modified from routing daemon or redirect)
A (installed by addrconf)
C (cache entry)
! (reject route)
Metric 距离、跳数。暂无用。
The 'distance' to the target (usually counted in hops). It is
not used by recent kernels, but may be needed by routing dae-
mons.
Ref 不用管,恒为0。
Number of references to this route. (Not used in the Linux ker-
nel.)
Use 该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。
Count of lookups for the route. Depending on the use of -F and
-C this will be either route cache misses (-F) or hits (-C).
Iface 接口,即eth0,eth0等网络接口名
Interface to which packets for this route will be sent.
使用示例
示例一 打印当前路由表(root用户)
[root@jfht ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
211.103.28.0 * 255.255.255.224 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
default 211.103.28.1 0.0.0.0 UG 0 0 0 eth0
[root@jfht ~]# /sbin/route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
211.103.28.0 * 255.255.255.224 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
default 211.103.28.1 0.0.0.0 UG 0 0 0 eth0
[root@jfht ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
211.103.28.0 0.0.0.0 255.255.255.224 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
0.0.0.0 211.103.28.1 0.0.0.0 UG 0 0 0 eth0
[root@jfht ~]# /sbin/route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
211.103.28.0 0.0.0.0 255.255.255.224 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
0.0.0.0 211.103.28.1 0.0.0.0 UG 0 0 0 eth0
[root@jfht ~]#
示例二 打印当前路由表(非root用户)
[web@hnweb1 ~]$ route
-bash: route: command not found
[web@hnweb1 ~]$ /sbin/route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.66.10.0 * 255.255.255.128 U 0 0 0 eth0
192.130.12.0 10.66.10.1 255.255.255.0 UG 0 0 0 eth0
10.0.0.0 * 255.255.255.0 U 0 0 0 eth1
10.66.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
134.161.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
10.20.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
172.224.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
default 10.66.10.22 0.0.0.0 UG 0 0 0 eth0
[web@hnweb1 ~]$ route -n
-bash: route: command not found
[web@hnweb1 ~]$ /sbin/route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.66.10.0 0.0.0.0 255.255.255.128 U 0 0 0 eth0
192.130.12.0 10.66.10.1 255.255.255.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.66.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
134.161.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
10.20.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
172.224.0.0 10.66.10.1 255.255.0.0 UG 0 0 0 eth0
0.0.0.0 10.66.10.22 0.0.0.0 UG 0 0 0 eth0
[web@hnweb1 ~]$
示例三 设置到某网络的路由的例子
下面的例子 来自一个实际的服务器配置。 route命令写在了/etc/rc.local中,这样就设置了一条永久路由。
[root@jf07 root]# grep route /etc/rc.local
route add -net 10.0.0.0/8 gw 10.33.149.1 dev eth1
[root@jf07 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.32.181.182 10.33.149.1 255.255.255.255 UGH 0 0 0 eth1
10.33.136.135 10.33.149.1 255.255.255.255 UGH 0 0 0 eth1
10.32.208.13 10.33.149.1 255.255.255.255 UGH 0 0 0 eth1
10.33.149.0 * 255.255.255.128 U 0 0 0 eth1
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
10.0.0.0 10.33.149.1 255.0.0.0 UG 0 0 0 eth1
default 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
[root@jf07 root]#
示例四 添加拒绝路由的测试
[root@jfht ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
211.103.28.0 * 255.255.255.224 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
default 211.103.28.1 0.0.0.0 UG 0 0 0 eth0
[root@jfht ~]# ping 10.33.11.12
PING 10.33.11.12 (10.33.11.12) 56(84) bytes of data.
Ctrl+C
--- 10.33.11.12 ping statistics ---
21 packets transmitted, 0 received, 100% packet loss, time 19999ms
[root@jfht ~]# route add -net 10.0.0.0 netmask 255.0.0.0 reject
[root@jfht ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
211.103.28.0 * 255.255.255.224 U 0 0 0 eth0
192.168.1.0 * 255.255.255.0 U 0 0 0 eth1
169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
10.0.0.0 - 255.0.0.0 ! 0 - 0 -
default 211.103.28.1 0.0.0.0 UG 0 0 0 eth0
[root@jfht ~]# ping 10.33.11.12
connect: Network is unreachable
[root@jfht ~]#
示例五 设置路由之后重启机器的测试
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]# route add -net 10.0.0.0 netmask 255.0.0.0 reject
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 - 255.0.0.0 ! 0 - 0 -
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]# reboot
Broadcast message from root (pts/0) (Thu Jul 7 05:31:26 2011):
The system is going down for reboot NOW!
[root@node34 root]#
Last login: Thu Jul 7 05:30:50 2011 from 192.168.227.1
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]#
上面的测试表明route设置的路由在机器重启之后就消失了。
示例六 将route命令添加到/etc/rc.local来设置永久路由的测试
先用vi在/etc/rc.local后面添加route命令。
[root@node34 root]# tail /etc/rc.local
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
# 2011.07.15 add permanent route test
route add -net 10.0.0.0 netmask 255.0.0.0 reject
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]# reboot
Broadcast message from root (pts/0) (Fri Jul 15 14:43:44 2011):
The system is going down for reboot NOW!
[root@node34 root]#
Last login: Fri Jul 15 14:40:22 2011 from 192.168.227.1
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 - 255.0.0.0 ! 0 - 0 -
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]#
示例七 删除路由的测试
删除路由的时候只需将route add改成route del,其他参数类似。如果报“无效的参数”或“没有那个进程”,可能是因为提供的参数不够。
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
10.0.0.0 - 255.0.0.0 ! 0 - 0 -
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]#
[root@node34 root]# route del -net 10.0.0.0
SIOCDELRT: 无效的参数
[root@node34 root]# route del -net 10.0.0.0 netmask 255.0.0.0
SIOCDELRT: 没有那个进程
[root@node34 root]#
[root@node34 root]# route del -net 10.0.0.0 netmask 255.0.0.0 reject
[root@node34 root]#
[root@node34 root]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.227.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.227.2 0.0.0.0 UG 0 0 0 eth0
[root@node34 root]#
问题思考
相关资料
【1】nixCraft Linux setup default gateway with route command
http://www.cyberciti.biz/faq/linux-setup-default-gateway-with-route-command/
【2】360doc Linux route命令
http://www.360doc.com/content/11/0418/14/2054285_110501874.shtml
【3】Linux公社 Linux下route add route del 用法
http://www.linuxidc.com/Linux/2010-11/30032.htm
【4】鳥哥的 Linux 私房菜 第五章、 Linux 常用網路指令
http://linux.vbird.org/linux_server/0140networkcommand.php#route
发表评论
-
在Linux用tar归档压缩文件时忽略某些文件和目录
2013-02-01 10:19 16913在Linux下,常用tar对文 ... -
使用nmap扫描服务器端口的一次操作
2012-11-01 17:00 15069使用nmap扫描服务器端口的一次操作 本文来自:http ... -
我使用过的Linux命令之wget - ooo
2011-09-14 13:10 0我使用过的Linux命令之wg ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:53 9078简明 Vim 练级攻略 http://coolshell.c ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:49 1简明 Vim 练级攻略 http://coolshell.c ... -
我使用过的Linux命令之:(冒号) - 啥也不做(除了……)
2011-08-29 13:18 11968我使用过的Linux命令之: ... -
我使用过的Linux命令之date - 显示、修改系统日期时间
2011-08-25 09:21 41896我使用过的Linux命令之da ... -
我使用过的Linux命令之declare - 声明shell变量(不知道没关系、知道了就更好的内建命令)
2011-08-16 09:22 21731我使用过的Linux命令之declare - 声明shell变 ... -
我使用过的Linux命令之alias - 设置命令的别名,让 Linux 命令更简练
2011-08-11 09:31 28698我使用过的Linux命令之alias - 设置命令的别名,让 ... -
我使用过的Linux命令之ar - 创建静态库.a文件
2011-08-08 10:40 51801我使用过的Linux命令之ar - 创建静态库.a文件 本 ... -
我使用过的Linux命令之crontab - 设置例行任务(类似于Windows中的任务计划)
2011-08-04 22:26 9686我使用过的Linux命令之crontab - 设置例行任务(类 ... -
我使用过的Linux命令之chmod - 改变文件/目录的访问权限
2011-08-03 21:33 10634我使用过的Linux命令之chmod - 改变文件/目录的访问 ... -
我使用过的Linux命令之export - 设置或显示环境变量
2011-08-02 19:55 25356我使用过的Linux命令之export - 设置或显示环境变量 ... -
我使用过的Linux命令之wc - 统计文件行数、单词数或字节数
2011-07-26 10:50 28918我使用过的Linux命令之wc - 统计文件行数、单词数或字节 ... -
我使用过的Linux命令之groupdel - 删除群组
2011-07-22 22:13 9611我使用过的Linux命令之gr ... -
我使用过的Linux命令之ifconfig - 网络配置命令
2011-07-21 20:43 60356我使用过的Linux命令之ifconfig - 网络配置命令 ... -
我使用过的Linux命令之ll - 列出文件/目录的详细信息
2011-07-20 21:22 7093我使用过的Linux命令之ll ... -
我使用过的Linux命令之mkdir - 创建目录
2011-07-20 20:58 13599我使用过的Linux命令之mkdir - 创建目录 本文链 ... -
我使用过的Linux命令之perror - 解释错误代码
2011-07-18 20:29 24948我使用过的Linux命令之perror - 解释错误代码 ... -
我使用过的Linux命令之ping - 测试与目标主机的连通性
2011-07-16 10:46 26206我使用过的Linux命令之ping - 测试与目标主机的连通性 ...
相关推荐
路由表中的metric,路由表中的metric,路由表中的metric
Linux系统中的route命令能够用于IP路由表的显示和操作。它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0。当使用”add”或者”del”参数时,路由表被修改,如果没有参数,则显示...
在linux的命令行下获取当前网络环境的gateway的IP并不是一件难事,常用的命令有 ip route或者 route -n,route -n 是通过读取proc文件系统下的文件来从内核获取路由表的,但 ip route 是通过netlink来获取的路由表;...
Linux 系 统 管 理 linux网络及服务器配置 几个网络管理命令 ifconfig: 配置网卡接口 ...route 显示或设置ip路由表 rlogin 远程登录 tcpdump 测试网络通信量 网络设置及访问方法 局域网设置:ip ,子网掩码和网关设置
◆ 网络操作命令:ifconfig、ip、ping、netstat、telnet、ftp、route、rlogin、rcp、finger、mail、 nslookup; ◆ 系统安全相关命令:passwd、su、umask、chgrp、chmod、chown、chattr、sudo ps、who; ◆ 其它...
linux双网卡 路由配置 访问特定ip网段走指定网卡,命令已实测
route命令用来显示并设置linux内核中的网络路由表,route命令设置的路由主要是静态路由。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。 在linux系统中设置...
进行了调整,以使用SO_MARK,高级路由,LD_PRELOAD和文件名称空间安装在Linux上强制进行特定于应用程序的路由。 它允许使用指定的路由表路由由基于套接字的应用程序(几乎所有)生成的数据包。 通过将修改后的...
添加路由的命令: 1.route add route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0 #添加一条静态路由 route add default gw 192.168.0.1 #添加默认路由 route del -...
ip 是iproute2 软件包里面的一个强大的网络配置工具,本书详细介绍了ip 命令及其选项。 其中包括: link: 网络设备; address:一个设备的协议(IP 或者IPV6)地址; neighbour: ARP 或者NDISC 缓冲...
linux服务器双网卡双IP实现双线 ifcfg-eth0, ifcfg-eth1 两个里面只能写一个网关 ①、vi /etc/iproute2/rt_tables 添加 252 tel (电信线路路由表) 251 cnc (网通线路路由表)
linux配置网卡方法 linux配置网卡方法: 基于命令行方式的手工配置方法 手工方法是在以太网卡已装入的情况下编辑配置文件来配置网络的方法。具体步骤如下: 1.编辑/etc/rc.d/rc.local文件 在该文件中加入类似...
一 在Linux下执行route命令 [root@localhost backup]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3 192.168.0.0 ...
在一个Ubuntu Linux服务器中,分别使用route命令和ip route命令打印出该服务器的路由表如下:(注:截图中的src 192.168.152.1
首先让我们查看一下当前机器的路由表,执行如下命令:route -n [root@vnode33 network-scripts]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 135.252.214....
软件包rtnetlink允许读取和更改内核的路由表。 网络路由,IP地址,链接参数,邻居设置,排队规则,流量类别和数据包分类器都可以受到控制。 它基于netlink消息。 使用软件包可以方便,高级地使用API包装器。 ...
4.3 配置路由表在Host_1虚拟机中用route命令配置缺省路由,将Host_1虚拟机缺省路由网关设置为Virtual_Router中ens33的IP地址