一般来说,都是为了能访问别的子网才设置路由的,比如说,你的主机处于192.168.10.0/24,而你想访问192.168.20.0/24网的主机,当然你知道一个网关IP,例如192.168.10.1(必须和你主机处于同一子网),那么,你可以这样配置路由。
添加路由
route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.10.1
查看路由状态
route -n
删除路由
route del -net 192.168.20.0 netmask 255.255.255.0
摘自鸟哥的私房菜
路由修改
一般来说,只要有网路介面,该介面就会产生一个路由,例如在鸟哥实验室内部的主机有一个
[root@linux
[root@linux
[root@linux
观察的参数:
增加
范例一:单纯的观察路由状态
[root@linux
Kernel
Destination
192.168.10.0
169.254.0.0
0.0.0.0
[root@linux
Kernel
Destination
192.168.10.0
169.254.0.0
default
·
·
·
o
o
o
o
o
o
o
·
此外,观察一下上面的路由排列顺序喔,依序是由小网域(192.168.10.0/24
1)不是
3)0/0
Kernel
Destination
192.168.10.0
192.168.10.0
也就是说,由于路由是依照顺序来排列与传送的,所以不论封包是由那个介面
范例二:路由的增加与删除
[root@linux
#
#
#
[root@linux
#
#
#
#
#
[root@linux
#
#
#
# route 命令添加的路由,机器重启或者网卡重启后就没掉了,在linux下设置永久路由的方法:
1.在/etc/rc.local里添加
2.在/etc/sysconfig/network里添加到末尾
3./etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.y
使用实例:
实例1:显示当前路由
命令:
route
route -n
输出:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
e192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~] # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:
第一行表示主机所在网络的地址为192.168.120.0,若数据传送目标是在本局域网内通信,则可直接通过eth0转发数据包;
第四行表示数据传送目的是访问Internet,则由接口eth0,将数据包发送到网关192.168.120.240
其中Flags为路由标志,标记当前网络节点的状态。
Flags标志说明:
U Up表示此路由当前为启动状态
H Host,表示此网关为一主机
G Gateway,表示此网关为一路由器
R Reinstate Route,使用动态路由重新初始化的路由
D Dynamically,此路由是动态性地写入
M Modified,此路由是由路由守护程序或导向器动态修改
! 表示此路由当前为关闭状态
备注:
route -n (-n 表示不解析名字,列出速度会比route 快)
实例2:添加网关/设置网关
命令:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
输出:
[root@localhost ~] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#
说明:
增加一条 到达244.0.0.0的路由
实例3:屏蔽一条路由
命令:
route add -net 224.0.0.0 netmask 240.0.0.0 reject
输出:
[root@localhost ~] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
说明:
增加一条屏蔽的路由,目的地址为 224.x.x.x 将被拒绝
实例4:删除路由记录
命令:
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
输出:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
224.0.0.0 * 240.0.0.0 U 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~] # route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~] # route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~] #
说明:
实例5:删除和添加设置默认网关
命令:
route del default gw 192.168.120.240
route add default gw 192.168.120.240
输出:
[root@localhost ~] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
[root@localhost ~] # route add default gw 192.168.120.240
[root@localhost ~] # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~] #
===========================================================================================
简单的的操作如下,
查看路由状态:route print
只查看ipv4(ipv6)路由状态:route print -4(-6)
添加路由:route add 目的网络 mask 子网掩码 网关 ——重启机器或网卡失效
route add 192.168.20.0 mask 255.255.255.0 192.168.10.1
添加永久:route -p add
route -p add 192.168.20.0 mask 255.255.255.0 192.168.10.1
删除路由:route delete 目的网络 mask 子网掩码
route delete 192.168.20.0 mask 255.255.255.0