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系统状态的查看及管理工具:     pstree, ps, pidof, pgrep, top, htop, glance,pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup pstree命…

    系统运维 2016-09-11
  • Linux计划任务和进程

    一、进程管理 1.进程简介 一个程序对应多个进程;一个进程对应一个程序。 2.进程状态的查看与控制 查看进程状态 w 查看个别用户的进程 eg: w userName list-info JCPU: PCPU: WAHT: from: IDLE: 用户空闲时间 load average: ps -aux -a: 显示所有用户的进程 -u:显示用户名和启动时间…

    2017-09-09
  • 进程管理

    linux维护工作中,常常需要对系统上运行的程序进行管理,那么进程管理就显得相当重要。 进程初识 什么是进程?   进程是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念。它代表CPU所能处理的单个任务。 什么是线程?   线程是程序流执行的最小单元,可以理解为进程中执行的一段程序片段。 进程与线程联…

    Linux干货 2016-09-07
  • 文件系统管理

                                高级文件系统管理 磁盘配额 控制用户在一个分区上使用多大的空间。 操作步骤: 1、创建一个10G的分区 /dev/sdc1并将其格式化,挂载 2、如果是新…

    Linux干货 2016-08-29
  • 网络配置

    Internet 协议特征  运行于OSI 网络层  面向无连接的协议  独立处理数据包  分层编址  尽力而为传输  无数据恢复功能     IP地址 它们可唯一标识IP 网络中的每台设备 每台主机(计算机、网络设备、外围设备)必须具有唯一的地址&…

    Linux干货 2016-09-06
  • nginx负载均衡实验

    实验一、 实验环境:     1、一台director主机。并部署nginx服务。        内网iP:192.168.1.1  外网iP:10.1.64.1     2、一台后端服务器。并部署h…

    Linux干货 2016-10-30

评论列表(1条)

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

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