一、 简介
route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。
注意:此命令知识临时生效,重启之后会清空。想要永久生效可以写在/etc/rc.local内。
二、 命令选项
-A:设置地址类型;
-C:打印将Linux核心的路由缓存;
-v:详细信息模式;
-n:不执行DNS反向查找,直接显示数字形式的IP地址;
-e:netstat格式显示路由表;
-net:到一个网络的路由表;
-host:到一个主机的路由表。
三、 命令参数
add:增加指定的路由记录;
del:删除指定的路由记录;
target:目的网络或目的主机;
gw:设置默认网关;
mss:设置TCP的最大区块长度(MSS),单位MB;
window:指定通过路由表的TCP连接的TCP窗口大小;
dev:路由记录所表示的网络接口。
四、 命令实例
1.查看当前主机路由表
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 eth3
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
其中Flags为路由标志,标记当前网络节点的状态,Flags标志说明:
U Up表示此路由当前为启动状态。
H Host,表示此网关为一主机。
G Gateway,表示此网关为一路由器。
R Reinstate Route,使用动态路由重新初始化的路由。
D Dynamically,此路由是动态性地写入。
M Modified,此路由是由路由守护程序或导向器动态修改。
! 表示此路由当前为关闭状态。
2. 在eth0网卡添加一条指向某个网络的路由
[root@localhost ~]# route add -net 10.0.0.0 netmask 255.255.255.0 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
解释:eth0网卡添加一个10.0.0.0/24的路由(为指定目标网络的参数,需要ip地址或地址范围、子网掩码用于确定网络范围。)
3. 添加到某一个ip的路由
[root@localhost ~]# route add -host 192.1.1.123 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.1.1.123 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
解释:默认添加的掩码默认都是255。
4.屏蔽某一个路由
[root@localhost ~]# route add -net 10.10.10.128 netmask 255.255.255.128 reject dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.10.10.128 - 255.255.255.128 ! 0 - 0 -
192.1.1.123 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
5.删除路由
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.10.10.128 - 255.255.255.128 ! 0 - 0 -
192.1.1.123 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
[root@localhost ~]# route del -net 10.10.10.128 netmask 255.255.255.128 reject
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.1.1.123 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
注意:记得最后的reject,不然会报错。要安装路由表删除,以免误删除。
6.设置默认网关
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.1.1.123 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
[root@localhost ~]# route add default gw 192.168.205.160
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.205.160 0.0.0.0 UG 0 0 0 eth3
0.0.0.0 192.168.205.224 0.0.0.0 UG 100 0 0 eth3
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.1.1.123 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.205.0 0.0.0.0 255.255.255.0 U 100 0 0 eth3
1万+

被折叠的 条评论
为什么被折叠?



