route(传统配置命令)
route:
route:显示路由信息(网关指的就是下一跳的地址,下一跳地址必须与本地主机位于同一网络)
-n:使用数字格式显示,不反解地址到主机名
route add //添加路由
-host HOST_IP gw Next [dev Device]
-net Net_ADDR
-net 0.0.0.0 目标地址为任意地址,即默认路由
route del //删除路由
-host HOST_IP gw 下一跳
-net Net_ADDR gw 下一跳
示例:
# route
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.81.2 0.0.0.0 UG 100 0 0 eno33554960
172.24.8.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728
192.168.81.0 0.0.0.0 255.255.255.0 U 100 0 0 eno33554960
示例中对于目表为192.168.81.0以及172.24.8.0网络段的访问,Gateway为4个0表示没有网络,也就是说属于直连网络,无需路由
对于目标为default的(当使用route -n时,default为4个0)网络段时,通过缺省网关192.168.81.2连接目标指向单个
主机的为主机路由,目标网络地址的为网络路由,目标指向所有地址(0.0.0.0或者default)的为默认路由。
route输出具体描述
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:总共有多个旗标,代表的意义如下:
U (route is up):该路由是启动的;
H (target is a host):目标是一部主机 (IP) 而非网域;
G (use gateway):需要透过外部的主机 (gateway) 来转递封包;
R (reinstate route for dynamic routing):使用动态路由时,恢复路由资讯的旗标;
D (dynamically installed by daemon or redirect):已经由服务或转 port 功能设定为动态路由
M (modified from routing daemon or redirect):路由已经被修改了;
! (reject route):这个路由将不会被接受(用来抵挡不安全的网域!)
A (installed by addrconf)
C (cache entry)
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
Iface 从哪一个网络接口路由出去,如本例中的eno33554960
Interface to which packets for this route will be sent.
添加网络路由
当前本机IP地址为172.24.8.131
假定要到达目标地址为:192.168.10.0/24,下一跳为 172.24.8.2 则添加路由命令如下
# route add -net 192.168.10.0/24 gw 172.24.8.2 ###此时为网络网关(路由)
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.81.2 0.0.0.0 UG 100 0 0 eno33554960
172.24.8.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728
192.168.10.0 172.24.8.2 255.255.255.0 UG 0 0 0 eno16777728
192.168.81.0 0.0.0.0 255.255.255.0 U 100 0 0 eno33554960
可以看到上述多出了一条路由记录,目标网络为192.168.10.0
新添加的路由接口设备自动使用了eno16777728网卡,因为172.24.8.2与eno16777728 IP为同一网段内
添加主机路由
假定要到达目标地址为:192.168.20.1,下一跳为: 172.16.100.177
# route add -host 192.168.20.1 gw 172.16.100.177
SIOCADDRT: Network is unreachable #提示网络不可达,因为172.16.100.177不在当前两块网卡的任一网段内
下面修改下一跳地址为172.24.8.254再次尝试添加
# route add -host 192.168.20.1 gw 172.24.8.254 ###此处无需指定隐码
# route -n ###在下面的列表中Flags有一个H,表示此条为主机路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.81.2 0.0.0.0 UG 100 0 0 eno33554960
172.24.8.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728
192.168.10.0 172.24.8.2 255.255.255.0 UG 0 0 0 eno16777728
192.168.20.1 172.24.8.254 255.255.255.255 UGH 0 0 0 eno16777728
192.168.81.0 0.0.0.0 255.255.255.0 U 100 0 0 eno33554960
路由表描述
对于请求到192.168.10.0的网路,路由地址为172.24.8.2,经由网卡eno16777728
对于请求到192.168.20.1的主机,路由地址为172.24.8.254,经由网卡eno16777728
对于请求172.24.8.0以及192.168.81.0的网络,为直连网络,无需路由
对于请求到0.0.0.0的网络经由192.168.81.2从网卡eno33554960进行路由,如访问百度则由此路由
# ping www.baidu.com -c 2
PING www.baidu.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=128 time=5.76 ms
64 bytes from 14.215.177.38: icmp_seq=2 ttl=128 time=6.13 ms
删除路由
假定需要删除默认路由
# route del -net 0.0.0.0 netmask 0.0.0.0 ###等价于 route del default
# route |grep -i default
# ping www.baidu.com ###缺省路由被删除后,ping百度不可达
connect: Network is unreachable
删除之前添加的网络路由和主机路由
# route del -net 192.168.10.0/24
# route del -host 192.168.20.1
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.24.8.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777728
192.168.81.0 0.0.0.0 255.255.255.0 U 100 0 0 eno33554960
添加缺省网关
# route add default gw 192.168.81.2
# route add -net 0.0.0.0 gw 192.168.81.2 ###与上一条命令等价
路由静态配置文件
/etc/sysconfig/network
/etc/sysconfig/network-scripts/route-ethX | route-pppX
配置文件的格式1:每行一个要配置的路径条目
目标 via 下一跳
例子:
192.168.0.0/24 via 172.16.100.177
配置文件的格式2:每一组一个路由条目
ADDRESS#=目标
NETMASK#=掩码
GATEWAY#=下一跳
ADDRESS0=192.168.0.0
NETMASK0=255.255.255.0
GATEWAY0=172.16.100.1