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

相关推荐

  • 多网卡实验

    1、虚拟网卡实现一个网卡多个地址     网卡别名多用于虚拟机,可理解为一块网卡MAC地址对应多个IP地址,这样比较方便管理,在不需要通过添加网卡的形式就可以使用到新的IP 在此图可看到     eth1     eth1:100   &n…

    Linux干货 2016-09-06
  • Linux 第七天: (08月05日) Linux文本处理工具

    Linux 第七天: (08月05日) 文本处理工具       head -n 指定货权前n行tail -n 指定获取后n行tail -f 显示文件新追加内容 tail -n 0 -f /var/log/messages & 后台监控日志 cut -d 指明分隔符,默认tabcut -f 第几个字段cut -c 按字符…

    Linux干货 2016-08-08
  • 初入linux

    一、认识linux 1、在windows中通过盘符区分文件存放位置,而linux中没有盘符之说,只有文件和文件夹 2、在linux中,万物从根开始,没有后缀之说 二、文件系统的目录结构 /:根目录 /boot:系统启动的相关配置文件 /dev:设备文件 块设备:随机访问,数据块为单位 字符设备:线性访问,按字符为单位 设备号:主设备号(major)和次设备号…

    Linux干货 2017-05-20
  • bash脚本编程实例

    bash脚本编程实例 1.写一个脚本,判断当前系统上所有用户的shell是否为可登录的shell(即用户的shell不是/sbin/nologin),分别统计这两类用户的个数(通过字符串比较来实现) #!/bin/bash cat /etc/passwd|awk -F: ‘BEGIN{nologin=0;login=0}{if($NF==”/sbin/nol…

    Linux干货 2017-08-28
  • Linux文件系统:从inode理解软链接与硬链接

    什么是inode? 在Linux磁盘存储文件系统中,我们以块划分磁盘为两部分:超级块(superblock)和数据块(data block);同时划分单文件为用户数据(user data)和元数据(meta data)两个部分。 用户数据记录的是文件的真实内容。比如你的血液、骨骼和各器官等等。 元数据这是附加于文件的属性信息。比如身高、体重、血型和年龄等等。…

    Linux干货 2016-08-07
  • Linux的文本处理工具及grep正则表达式的使用

    文本处理工具及grep正则表达式的使用 本章节学习的内容: 1、各种文本工具来查看、分析、统计文本文件 2、grep正则表达式 3、扩展正则表达式 一、抽取文本的工具: 1、按文件内容:less和cat 2、按文件截取:head和tail 3、按列抽取:cut 4、按关键字抽取:grep 二、文件查看命令:cat, tac,rev 1、命令cat: (1)文…

    Linux干货 2016-08-05

评论列表(1条)

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

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