linux系统的路由功能

1、概要

   大家应该都明白,不同网段的报文包传播,是需要路由的转发转发功能的,那么,一个linux操作系统能不能充当路由器呢?下面不妨来验证一下。



2、网络拓扑

wKioL1fL2HvQjQ9HAABcwhriRjg125.png


3、实现过程

   <1>俩个centos6分别充当Router1和Router2。并添加足够的网卡。

   <2>关闭NetworkManager

   <3>配置centos6-1

[root@centos6 network-scripts]# vim ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.0.1
PREFIX=24
[root@centos6 network-scripts]# vim ifcfg-eth1
DEVICE=eth1
IPADDR=10.0.0.1
PREFIX=8
[root@centos6 network-scripts]# service network restart

    <4>配置centos6-2

[root@centos6 network-scripts]# vim ifcfg-eth0
DEVICE=eth0
IPADDR=172.16.0.1
PREFIX=16
[root@centos6 network-scripts]# vim ifcfg-eth1
DEVICE=eth1
IPADDR=10.0.0.2
PREFIX=8
[root@centos6 network-scripts]# service network restart

    <5>添加路由

# centos6-1添加路由
[root@centos6 network-scripts]# route add -net 172.16.0.0/16 gw 10.0.0.2 dev eth1
[root@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添加路由
[root@centos6 network-scripts]# route add -net 192.168.0.0/24 gw 10.0.0.1 dev eth1
[root@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>启用路由功能

# centos6-1启用
[root@centos6 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward
# centos6-2启用
[root@centos6 network-scripts]# echo 1 > /proc/sys/net/ipv4/ip_forward

    <8>清空防火墙

# centos6-1清空
[root@centos6 network-scripts]# iptables -F
# centos6-1清空
[root@centos6 network-scripts]# iptables -F

    <8>测试

准备
#另一台虚拟机
[root@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
[root@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至已经改变,减少一跳
[root@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 *.*.*.*/#

原创文章,作者:mfwing,如若转载,请注明出处:http://www.178linux.com/43612

(0)
mfwingmfwing
上一篇 2016-09-06 08:51
下一篇 2016-09-06 08:51

相关推荐

  • 第三章 Linux文件系统初步

    用户界面:      可分为两种GUI和CLI         GUI(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。 GNOME(英语发音:/ɡˈnoʊm/或英语发音:/…

    Linux干货 2016-06-03
  • N25-第二周作业

    1、  Linux上的文件管理类命令都有那些,其常用的使用方法及其相关示例演示。 常用的管理类命令有:rm、mv、cp 1)         rm:remove rm [ OPTION ] FILE 常用选项:     &nbsp…

    Linux干货 2016-12-13
  • BIND的编译安装\DNS的搭建及测试

    编译安装bind          1、下载bind: isc.org: bind-9.10 bind-9.11 bind-10          2、 #tar xvf bind-9.10.5.tar.gz       &nbs…

    2017-07-31
  • 马哥教育网络班21期+第5周课程练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@centos ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf  root (hd0,0) kernel /vmlinuz-2.6.3…

    Linux干货 2016-07-29
  • 马哥教育网络班21期-第八周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥:是将广播域划分为多个小的冲突域,但广播域没有变。工作在OSI模型的数据链路层,端口很少;基于软件;可以处理上层事务。集线器:对接收到的信号进行再生整形放大,扩大网络传输距离;属于纯硬件网络底层设备,工作在OSI模型的物理层,不具有交换机的”智能记忆”和”学习”的能力;也不具…

    Linux干货 2016-08-29
  • RAID概述

    RAID 什么是RAID RAID(redundant arrays of independents disk,独立磁盘冗余阵列)是利用数组的方式组成磁盘组。RIAD大多是由廉价的磁盘组成容量巨大的磁盘组。,利用个别磁盘提供数据所产生的加成效果提升整个磁盘系统效能。 使用RAID目的 加快访问速度 使用一个虚拟磁盘,从而增加存储容量 减少磁盘失效带来不利的影…

    Linux干货 2016-11-22

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-10 15:03

    文章整体思路清晰,从一个问题引入,到通过自己的实验来论证自己的想法,最后希望作者也能将实验结果单独总结出来。