设为首页 收藏本站
查看: 1057|回复: 0

[经验分享] linux系统的路由功能

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-5 15:22:35 | 显示全部楼层 |阅读模式
1、概要
   大家应该都明白,不同网段的报文包传播,是需要路由的转发转发功能的,那么,一个linux操作系统能不能充当路由器呢?下面不妨来验证一下。



2、网络拓扑
wKioL1fL2HvQjQ9HAABcwhriRjg125.jpg



3、实现过程
   <1>俩个centos6分别充当Router1和Router2。并添加足够的网卡。
   <2>关闭NetworkManager
   <3>配置centos6-1
1
2
3
4
5
6
7
8
9
[iyunv@centos6 network-scripts]# vim ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.0.1
PREFIX=24
[iyunv@centos6 network-scripts]# vim ifcfg-eth1
DEVICE=eth1
IPADDR=10.0.0.1
PREFIX=8
[iyunv@centos6 network-scripts]# service network restart



    <4>配置centos6-2
1
2
3
4
5
6
7
8
9
[iyunv@centos6 network-scripts]# vim ifcfg-eth0
DEVICE=eth0
IPADDR=172.16.0.1
PREFIX=16
[iyunv@centos6 network-scripts]# vim ifcfg-eth1
DEVICE=eth1
IPADDR=10.0.0.2
PREFIX=8
[iyunv@centos6 network-scripts]# service network restart



    <5>添加路由
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# centos6-1添加路由
[iyunv@centos6 network-scripts]# route add -net 172.16.0.0/16 gw 10.0.0.2 dev eth1
[iyunv@centos6 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.1.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth2
172.16.0.0      10.0.0.2        255.255.0.0     UG    0      0        0 eth1 #添加的
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth2
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1
# centos6-2添加路由
[iyunv@centos6 network-scripts]# route add -net 192.168.0.0/24 gw 10.0.0.1 dev eth1
[iyunv@centos6 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     10.0.0.1        255.255.255.0   UG    0      0        0 eth1 #添加的
10.1.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth2
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1004   0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1005   0        0 eth2
172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1



补充:删除路由使用route del -net *.*.*.*/#
    <7>启用路由功能
1
2
3
4
# centos6-1启用
[iyunv@centos6 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward
# centos6-2启用
[iyunv@centos6 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward



    <8>清空防火墙
1
2
3
4
# centos6-1清空
[iyunv@centos6 network-scripts]# iptables -F
# centos6-1清空
[iyunv@centos6 network-scripts]# iptables -F



    <8>测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
准备
#另一台虚拟机
[iyunv@centos7~]#ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.222  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::20c:29ff:fe44:5b8d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:44:5b:8d  txqueuelen 1000  (Ethernet)
        RX packets 72946  bytes 6742470 (6.4 MiB)
        RX errors 0  dropped 10  overruns 0  frame 0
        TX packets 3002  bytes 488833 (477.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# windows配置172.16.0.100/16
[iyunv@centos7~]#ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=63 time=1.11 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=63 time=0.357 ms    # ttl至已经改变,减少一跳
[iyunv@centos7~]#ping 172.16.0.100
PING 172.16.0.100 (172.16.0.100) 56(84) bytes of data.
64 bytes from 172.16.0.100: icmp_seq=1 ttl=62 time=1.12 ms
64 bytes from 172.16.0.100: icmp_seq=2 ttl=62 time=0.783 ms
64 bytes from 172.16.0.100: icmp_seq=3 ttl=62 time=0.785 ms    # ttl减少2,成功



完成。      


补充:
手动指定IP:ifconfig eth0 *.*.*.*/#
            ifconfig eth0 up/down
手动添加路由 route add defaults gw IP

             route del -net *.*.*.*/#


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-268014-1-1.html 上篇帖子: CentOS6 网络管理之网卡配置及简单路由设置 下篇帖子: Error: Cannot retrieve metalink for repository: epel. Please verify its path ... linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表