Linux下如何利用PC机进行路由转发

2016-02-20 03:15 4 1 收藏

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享Linux下如何利用PC机进行路由转发吧。

【 tulaoshi.com - Linux教程 】

在Linux操作系统中,我们只要简单的设置就能将Linux变成一台优秀的路由器,实现路由转发,下面将通过三台机子的实现来给大家详细介绍下如何利用pc机来实现路由转发,一起来学习下吧。

1、网络拓扑

网络拓扑如下所示,我们在这里用到了三台机子做实验,分别是①、④、⑦号机,使用①号机ping⑦号机,④号机作为路由转发。

 Linux下如何利用PC机进行路由转发

2、错误的路由配置

首先我们使用如下的配置方法,配置这三台机子的路由表:

1)在①号机种配置如下,让目的网段是10.0.4.0/24的从eth1端口出去

route add -net 10.0.4.0/24 dev eth1

在①号机的查看路由表输入如下命令:

route -n

①号机的路由表的结果如下:

 Linux下如何利用PC机进行路由转发

2)在⑦号机使用同样方法配置路由,结果如下:

 Linux下如何利用PC机进行路由转发

3)在4号机配置路由转发功能,即将/etc/sysctl.conf文件里面的net.ipv4.ip_forward的值置1:

 Linux下如何利用PC机进行路由转发

4)所有的配置已经完成,我们在①号机ping④号机

ping 10.0.4.3

结果如下,即ping 不通:

PING 10.0.4.3 (10.0.4.3) 56(84) bytes of data.From 10.0.1.3 icmp_seq=2 Destination Host UnreachableFrom 10.0.1.3 icmp_seq=3 Destination Host UnreachableFrom 10.0.1.3 icmp_seq=4 Destination Host UnreachableFrom 10.0.1.3 icmp_seq=6 Destination Host UnreachableFrom 10.0.1.3 icmp_seq=7 Destination Host UnreachableFrom 10.0.1.3 icmp_seq=8 Destination Host Unreachable

这里为了方便研究,把①号机的eth1配置放出来

eth1 Link encap:Ethernet HWaddr 00:16:EC:AF:CB:CB inet addr:10.0.1.3 Bcast:10.255.255.255 Mask:255.255.255.0 inet6 addr: fe80::216:ecff:feaf:cbcb/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4564 errors:0 dropped:0 overruns:0 frame:0 TX packets:6688 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:459463 (448.6 KiB) TX bytes:546633 (533.8 KiB) Interrupt:23 Base address:0x6000

在①号机ping 的同时,我在④号机抓eth1包,结果如下:

[root@h4~]# tcpdump -i eth1 -enntcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes15:26:44.388614 00:16:ec:af:cb:cb 》 ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 60: Request who-has 10.0.4.3 tell 10.0.1.3, length 4615:26:45.391014 00:16:ec:af:cb:cb 》 ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 60: Request who-has 10.0.4.3 tell 10.0.1.3, length 4615:26:47.387821 00:16:ec:af:cb:cb 》 ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 60: Request who-has 10.0.4.3 tell 10.0.1.3, length 4615:26:48.391220 00:16:ec:af:cb:cb 》 ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 60: Request who-has 10.0.4.3 tell 10.0.1.3, length 4615:26:49.392621 00:16:ec:af:cb:cb 》 ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 60: Request who-has 10.0.4.3 tell 10.0.1.3, length 46

可见①号一直在寻找配有10.0.4.3 IP的机子的mac地址,即一直在发arp包。但是路由器(④号机)默认是不转发arp报文的,所有①号机永远也ping不通⑦号机。

3、正确的配置

在①号机种配置路由,命令如下:

route add -net 10.0.4.0/24 gw 10.0.1.2

这时候①号机的路由表:

[root@h1 ~]# [root@h1 ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface10.0.4.0 10.0.1.2 255.255.255.0 UG 0 0 0 eth110.0.5.0 0.0.0.0 255.255.255.0 U 0 0 0 eth210.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1192.168.99.0 0.0.0.0 255.255.255.0 U 1 0 0 eth00.0.0.0 192.168.99.1 0.0.0.0 UG 0 0 0 eth0

同样的方法配置⑦号机的路由表

root@h7:~# route -n内核 IP 路由表目标 网关 子网掩码 标志 跃点 引用 使用 接口0.0.0.0 192.168.99.1 0.0.0.0 UG 0 0 0 eth010.0.1.0 10.0.4.2 255.255.255.0 UG 0 0 0 eth110.0.4.0 0.0.0.0 255.255.255.0 U 1 0 0 eth110.0.7.0 0.0.0.0 255.255.255.0 U 1 0 0 eth2192.168.99.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0

下面再进行ping测试,在①号机ping⑦号机,结果能够ping通。在这里我们问了方便分析,首先列出各网卡的MAC地址

①号机 eth1:HWaddr 00:16:EC:AF:CB:CB④号机 eth1:HWaddr 40:61:86:32:8F:0B ④号机 eth4:HWaddr 40:61:86:32:8F:0E⑦号机 eth1:HWaddr 00:25:90:93:40:79

④号机eth1抓包如下:

[root@h4 ~]# tcpdump -i eth1 -enntcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes16:02:26.809445 00:16:ec:af:cb:cb 》 40:61:86:32:8f:0b, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 1, length 6416:02:26.810723 40:61:86:32:8f:0b 》 00:16:ec:af:cb:cb, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 1, length 6416:02:27.811847 00:16:ec:af:cb:cb 》 40:61:86:32:8f:0b, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 2, length 6416:02:27.813136 40:61:86:32:8f:0b 》 00:16:ec:af:cb:cb, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 2, length 6416:02:28.813248 00:16:ec:af:cb:cb 》 40:61:86:32:8f:0b, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 3, length 6416:02:28.814551 40:61:86:32:8f:0b 》 00:16:ec:af:cb:cb, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 3, length 6416:02:29.814648 00:16:ec:af:cb:cb 》 40:61:86:32:8f:0b, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 4, length 64

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com)

④号机eth4抓包如下:

root@h4 ~]# tcpdump -i eth4 -enntcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth4, link-type EN10MB (Ethernet), capture size 65535 bytes16:02:26.809460 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 1, length 6416:02:26.810715 00:25:90:93:40:79 》 40:61:86:32:8f:0e, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 1, length 6416:02:27.811853 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 2, length 6416:02:27.813130 00:25:90:93:40:79 》 40:61:86:32:8f:0e, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 2, length 6416:02:28.813255 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 3, length 6416:02:28.814545 00:25:90:93:40:79 》 40:61:86:32:8f:0e, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 3, length 64

⑦号机eth1抓包如下:

root@h7:~# tcpdump -i eth1 -enntcpdump: verbose output suppressed, use -v or -vv for full protocol decodelistening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes16:02:27.222853 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 1, length 6416:02:27.222867 00:25:90:93:40:79 》 40:61:86:32:8f:0e, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 1, length 6416:02:28.225226 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 2, length 6416:02:28.225237 00:25:90:93:40:79 》 40:61:86:32:8f:0e, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 2, length 6416:02:29.226638 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 3, length 6416:02:29.226649 00:25:90:93:40:79 》 40:61:86:32:8f:0e, ethertype IPv4 (0x0800), length 98: 10.0.4.3 》 10.0.1.3: ICMP echo reply, id 8079, seq 3, length 6416:02:30.228059 40:61:86:32:8f:0e 》 00:25:90:93:40:79, ethertype IPv4 (0x0800), length 98: 10.0.1.3 》 10.0.4.3: ICMP echo request, id 8079, seq 4, length 64

从抓取的包中我们不难看出,①号机在ping ⑦号机时,由于其中路由表配置了通过四号机的eth1(10.0.1.2)地址,这个地址对应的mac①号机已经缓存了,所有没有进行arp广播就直接开 始发送ICMP包,并且目的ip是⑦号机,目的MAC是④号机的eth1的,之后在④号机路由中又将目的MAC变成了④号机的eth4的,目的ip不变, 回来的过程相仿。

4、结论

由于linux路由器默认不转发arp报文到,所有若像错误的配置那样配置路由,①号机一直处在询问目的MAC的阶段而无法让路由器④号机转发数据包, 所有我们可以通过正确的配置那样配置路由让①号机使用④号机eth1的MAC出去,然后再一步一步转发。或者通过错误的配置那样配置路由,然后在 ④号机中使用arp代理,从而让①号机获得⑦号机的MAC,从而从发送arp报文阶段到发送ICMP包阶段。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com)

上面就是Linux下利用PC机实现路由转发的方法介绍了,文中也有介绍了错误的设置方法,以防你设置出错,赶紧上手操作看看吧。

来源:https://www.tulaoshi.com/n/20160220/1635926.html

延伸阅读
1.源程序的编译     在Linux下面,如果要编译一个C语言源程序,我们要使用GNU的gcc编译器. 下面我们以一个实例来说明如何使用gcc编译器. 假设我们有下面一个非常简单的源程序(hello.c):  int main(int argc,char **argv)   { printf("Hello Linux/n");   } 要编译这个程序,我们只要在命令行下执行: gcc -o h...
限制用户 root 远程登录,对保证计算机系统的安全,具有实际意义。本文向大家介绍一些方法,能达到限制 root 远程登录的目的。 方法一:在/etc/default/login 文件,增加一行设置命令。 CONSOLE = /dev/tty01 设置后立即生效,无需重新引导。以后,用户只能在控制台(/dev/tty01)root登录,从而达到限制root远程登录,不过,...
标签: 服务器
Linux下针对路由功能配置iptables的方法详解   作为公司上网的路由器需要实现的功能有nat地址转换、dhcp、dns缓存、流量控制、应用程序控制,nat地址转换通过iptables可以直 接实现,dhcp服务需要安装dhcpd,dns缓存功能需要使用bind,流量控制可以使用tc,应用程序控制:例如对qq的封锁可以使用 netfilter-layer7-v2.22+17-protoco...
标签: 服务器
Linux如何利用ssh传送文件至FTP空间进行备份   在Linux系统中,可通过ssh将文件传送到FPT空间中,将一些重要的数据进行备份,那么要如何实现文件的传送呢?小编给大家具体介绍下Linux利用ssh传送文件到FTP空间的方法。 步骤: 假设要备份的目录为 /home/wwwroot/aaa/ ,远程FTP空间的 IP 为 55.44.33.22 1.进入...
  作者: Thorbjørn Ravn Andersen, ravn@dit.ou.dk 译者: Chou-Kai Wang, stylon@ms2.hinet.net v1.4, 7 August 1997 -------------------------------------------------------------------------------- 虽然大部份的 Linux 发行版本都附有一个用来把目录的内容列出来,而且可以利用彩色来增强输出在视觉上...

经验教程

861

收藏

67
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部