Linux下利用PC機(jī)進(jìn)行路由轉(zhuǎn)發(fā)將Linux變成一臺(tái)路由器

在Linux操作系統(tǒng)中,我們只要簡(jiǎn)單的設(shè)置就能將Linux變成一臺(tái)優(yōu)秀的路由器,實(shí)現(xiàn)路由轉(zhuǎn)發(fā),下面將通過(guò)三臺(tái)機(jī)子的實(shí)現(xiàn)來(lái)給大家詳細(xì)介紹下如何利用pc機(jī)來(lái)實(shí)現(xiàn)路由轉(zhuǎn)發(fā),一起來(lái)學(xué)習(xí)下吧。
1、網(wǎng)絡(luò)拓?fù)?/strong>
網(wǎng)絡(luò)拓?fù)淙缦滤?,我們?cè)谶@里用到了三臺(tái)機(jī)子做實(shí)驗(yàn),分別是①、④、⑦號(hào)機(jī),使用①號(hào)機(jī)ping⑦號(hào)機(jī),④號(hào)機(jī)作為路由轉(zhuǎn)發(fā)。
2、錯(cuò)誤的路由配置
首先我們使用如下的配置方法,配置這三臺(tái)機(jī)子的路由表:
1)在①號(hào)機(jī)種配置如下,讓目的網(wǎng)段是10.0.4.0/24的從eth1端口出去
route add -net 10.0.4.0/24 dev eth1
在①號(hào)機(jī)的查看路由表輸入如下命令:
route -n
①號(hào)機(jī)的路由表的結(jié)果如下:
2)在⑦號(hào)機(jī)使用同樣方法配置路由,結(jié)果如下:
3)在4號(hào)機(jī)配置路由轉(zhuǎn)發(fā)功能,即將/etc/sysctl.conf文件里面的net.ipv4.ip_forward的值置1:
4)所有的配置已經(jīng)完成,我們?cè)冖偬?hào)機(jī)ping④號(hào)機(jī)
ping 10.0.4.3
結(jié)果如下,即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
這里為了方便研究,把①號(hào)機(jī)的eth1配置放出來(lái)
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
在①號(hào)機(jī)ping 的同時(shí),我在④號(hào)機(jī)抓eth1包,結(jié)果如下:
?。踨oot@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
可見(jiàn)①號(hào)一直在尋找配有10.0.4.3 IP的機(jī)子的mac地址,即一直在發(fā)arp包。但是路由器(④號(hào)機(jī))默認(rèn)是不轉(zhuǎn)發(fā)arp報(bào)文的,所有①號(hào)機(jī)永遠(yuǎn)也ping不通⑦號(hào)機(jī)。
3、正確的配置
在①號(hào)機(jī)種配置路由,命令如下:
route add -net 10.0.4.0/24 gw 10.0.1.2
這時(shí)候①號(hào)機(jī)的路由表:
?。踨oot@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
同樣的方法配置⑦號(hào)機(jī)的路由表
root@h7:~# route -n內(nèi)核 IP 路由表目標(biāo) 網(wǎng)關(guān) 子網(wǎng)掩碼 標(biāo)志 躍點(diǎn) 引用 使用 接口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
下面再進(jìn)行ping測(cè)試,在①號(hào)機(jī)ping⑦號(hào)機(jī),結(jié)果能夠ping通。在這里我們問(wèn)了方便分析,首先列出各網(wǎng)卡的MAC地址
?、偬?hào)機(jī) eth1:HWaddr 00:16:EC:AF:CB:CB④號(hào)機(jī) eth1:HWaddr 40:61:86:32:8F:0B ④號(hào)機(jī) eth4:HWaddr 40:61:86:32:8F:0E⑦號(hào)機(jī) eth1:HWaddr 00:25:90:93:40:79
④號(hào)機(jī)eth1抓包如下:
?。踨oot@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
?、芴?hào)機(jī)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
⑦號(hào)機(jī)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
從抓取的包中我們不難看出,①號(hào)機(jī)在ping ⑦號(hào)機(jī)時(shí),由于其中路由表配置了通過(guò)四號(hào)機(jī)的eth1(10.0.1.2)地址,這個(gè)地址對(duì)應(yīng)的mac①號(hào)機(jī)已經(jīng)緩存了,所有沒(méi)有進(jìn)行arp廣播就直接開(kāi) 始發(fā)送ICMP包,并且目的ip是⑦號(hào)機(jī),目的MAC是④號(hào)機(jī)的eth1的,之后在④號(hào)機(jī)路由中又將目的MAC變成了④號(hào)機(jī)的eth4的,目的ip不變, 回來(lái)的過(guò)程相仿。
4、結(jié)論
由于linux路由器默認(rèn)不轉(zhuǎn)發(fā)arp報(bào)文到,所有若像”錯(cuò)誤的配置“那樣配置路由,①號(hào)機(jī)一直處在詢問(wèn)目的MAC的階段而無(wú)法讓路由器④號(hào)機(jī)轉(zhuǎn)發(fā)數(shù)據(jù)包, 所有我們可以通過(guò)”正確的配置“那樣配置路由讓①號(hào)機(jī)使用④號(hào)機(jī)eth1的MAC出去,然后再一步一步轉(zhuǎn)發(fā)?;蛘咄ㄟ^(guò)”錯(cuò)誤的配置“那樣配置路由,然后在 ④號(hào)機(jī)中使用arp代理,從而讓①號(hào)機(jī)獲得⑦號(hào)機(jī)的MAC,從而從發(fā)送arp報(bào)文階段到發(fā)送ICMP包階段。
上面就是Linux下利用PC機(jī)實(shí)現(xiàn)路由轉(zhuǎn)發(fā)的方法介紹了,文中也有介紹了錯(cuò)誤的設(shè)置方法,以防你設(shè)置出錯(cuò),趕緊上手操作看看吧。
相關(guān)文章
- win10系統(tǒng)的文件怎么備份到小米路由器中?我們可以把電腦中比較重要的文件備份到小米路由器中,但是具體該怎備份?下面分享win10的文件備份到小米路由器上的詳細(xì)教程2015-12-15
如何打開(kāi)Win7路由器的管理頁(yè)面?Win7打開(kāi)路由器的管理頁(yè)面的方法
如何打開(kāi)Win7路由器的管理頁(yè)面?本文將提供Win7打開(kāi)路由器的管理頁(yè)面的方法供大家了解,希望可以幫助到大家2015-09-16win7系統(tǒng)下進(jìn)不了路由器設(shè)置該怎么辦?
有win7用戶,在使用中出現(xiàn)網(wǎng)絡(luò)連接不上,連路由器的管理界面也打不開(kāi),無(wú)法進(jìn)入管理界面,面對(duì)這種故障不知如何處理,下面分享win7系統(tǒng)下路由器進(jìn)不了的解決方案,需要的朋2015-09-13- win10修改wifi密碼之后怎么重新連接路由器?之前的密碼被蹭網(wǎng)的盜取了,于是換了一個(gè)復(fù)雜的密碼,但是換好wifi密碼以后,卻不知道win10怎么連接重置密碼的wifi,下面我們一2015-09-07
- 這篇文章主要介紹了在CentOS中制作BGP路由器的教程,依靠Quagga套件,文中示例同樣適用于RedHat系的其他Linux系統(tǒng),需要的朋友可以參考下2015-07-07
Win10打不開(kāi)路由器掛載的硬盤(pán)連接硬盤(pán)失敗的處理措施
安裝了Wn10系統(tǒng)后,就打不開(kāi)路由器掛載的硬盤(pán),經(jīng)過(guò)折騰終于找到了無(wú)法連接硬盤(pán)的處理措施,將成果與大家分享下2015-05-05Win7系統(tǒng)怎么設(shè)置無(wú)線路由器的WiFi中文名讓其與眾不同
Win7系統(tǒng)怎么設(shè)置無(wú)線路由器的WiFi中文名?怎么設(shè)置一個(gè)有個(gè)性又好記的WiFi中文名呢?既然是要設(shè)置WiFi名字,那么我們何不把WiFi設(shè)置成中文的呢?如何更改,詳看下文2015-03-19- 這篇文章主要介紹了配合路由器設(shè)置電腦靜態(tài)ip方法圖文教程,本文用非常詳盡的圖文方式講解了WinXP、Win7、Win8系統(tǒng)下設(shè)置靜態(tài)IP的方法,需要的朋友可以參考下2015-03-16
win10 9266預(yù)覽版怎么查看查看小米路由器硬盤(pán)?
win10 9266預(yù)覽版怎么查看查看小米路由器硬盤(pán)?自從更新了9266版本后,win10 的安全策略發(fā)生了變化,導(dǎo)致局域網(wǎng)共享出現(xiàn)問(wèn)題,下面我們一起看看解決辦法,需要的朋友可以參2015-03-15Win7 64位純凈版系統(tǒng)下忘記路由器密碼如何進(jìn)入后臺(tái)進(jìn)行設(shè)置
有很多win7系統(tǒng)用戶經(jīng)常會(huì)忘記登錄路由器的密碼和用戶名,這樣就無(wú)法進(jìn)入路由器后臺(tái)進(jìn)行設(shè)置了,下面以win7 64位純凈版系統(tǒng)為例,跟大家?guī)?lái)具體的操作方法2014-10-08