加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

route命令使用总结

发布时间:2016-09-30 00:51:26 所属栏目:Linux 来源:站长网
导读:副标题#e# 考试题一:linux下如何添加路由(百度面试题) 以上是原题,老男孩老师翻译成如下3道题。 a.如何用命令行方式给linux机器添加一个默认网关,假设网关地址为10.0.0.254? b. 192.168.1.0网段, 192.168.1.1网关的某一服务器想连入172.16.1.0/24段,该

b.网络路由:即去往某一网络或网段的路由

一般多网段之间互相通信,希望建立一条优先路由,而不是通过默认网关时就可以配置网络路由。还是拿房子比喻,你现在不是要出门,而是卧室,卫生间,去卧室就要经过卧室的门,去卫生间也要经过卫生间的门,这里的卧室和卫生间的门就可以认为是去往某一网段的路由,而不是默认路由(即房子的门。)

实际工作中会有需求,两个不同的内部网络之间互访,而不是出网访问,就是上面例子的情况。

本题的答案:

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

解答实践:

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

SIOCADDRT: 网络不可达 #==>当连不通地址192.168.1.1时,无法添加路由。

[root@oldboy ~]# ifconfig eth0:0 192.168.1.1/24 up#==>添加一个IP别名用于临时测试,如果永久生效最好加双网卡或写入到配置文件。

[root@oldboy ~]# ifconfig eth0:0 #==>查看添加的IP别名(网络里把这种多IP的方式称为子接口)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:65:A4:FD

 inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0

 UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

再来添加去192.168.1.0的数据包,交给192.168.1.1处理。

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

[root@oldboy ~]# netstat -rn   #==>和route -n很像。

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

10.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0

192.168.1.0     192.168.1.1     255.255.255.0   UG        0 0          0 eth0 #==>这就是网络路由

192.168.1.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

0.0.0.0         10.0.0.254      0.0.0.0         UG        0 0          0 eth0

拓展:其他写法

[root@oldboy ~]# route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0  #==>指定设备而不是地址。

[root@oldboy ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0

192.168.1.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

0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

[root@oldboy ~]# route del -net 192.168.1.0/24 dev eth0  

[root@oldboy ~]# route add -net 192.168.1.0/24 dev eth0  

[root@oldboy ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

192.168.1.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

0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0

总结:

route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1

route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

route add -net 192.168.1.0/24 dev eth0

route del -net 192.168.1.0/24 dev eth0

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读