歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 如何用Linux安裝TCP/IP路由器

如何用Linux安裝TCP/IP路由器

日期:2017/2/27 9:41:54   编辑:更多Linux
Linux 當 Router 的先決條件 要以 Linux 當 Router,首先必須確認你的 Linux Kernel 中有支援 IP Forwarding 的功能,也就是在 make Kernel 時,要選擇 IP Forwarding 的選項。 Router 分別以兩塊網路卡連接 Class B net 與 Subnet,所以讓 Linux 在開機的時候就要抓到這兩塊網路卡,成了一先決的條件。其方法是在 /etc/lilo。conf 中加入 append="ether=irq_0,io_port_0,eth0 ether=irq_1,io_port_1,eth1" 後,再值行 lilo C lilo。conf,之後再重新開機,應該就可以抓到兩塊網路卡了。 要檢查是否有抓到兩塊網路卡的話,可以看 /proc/net/dev 檔中是否有 eth0 和 eth1 兩個網路介面,或是值行 ifconfig,看是否有 eth0 和 eth1 兩個網路介面, 若是沒有的話,有可能是你的 Kernel 沒有支援那種網路卡,那就必須再重新 make Kernel,將那種網路卡的選項包含進來。 如何規劃 Subnet 的 IP address Router 的兩個網路介面分別連接兩個 net,且各有其 IP address, eth0=140.115.50.67 為連接 Class B net 的 IP address eth1=140.115.50.161 為連接 Subnet 的 IP address 現在我們就要來看看如何規劃 Subnet. 首先我們要先決定 Subnet 中有幾台機器,也就是要劃分幾個 IP address 於 Subnet 中,我以 Subnet 中有 32 台機器為例加以說明.因此 Subnet 之 IP 范圍為 140.115.50.160 ~ 140.115.50.191,WHY?? 140.115.50.160 ===> 140.115.50.101 00000 ^^^^^^^^^^^^^^ ^^^^^ network address host address 140.115.50.191 ===> 140.115.50.101 11111 ^^^^^^^^^^^^^^ ^^^^^ network address host address 其 netmask 皆為 255.255.255.224 ===> 255.255.255.111 00000 ^^^^^^^^^^^^^^^ ^^^^^ 其 broadcast 皆為 140.115.50.191 ===> 140.115.50.101 11111 ^^^^^^^^^^^^^^ ^^^^^ host address 有 5 個 bit,因此可以決定 2^5 = 32 台機器,當然同理你也可以用 140.115.50.96 ~ 140.115.50.127 為你的 Subnet address,只要不和別人相沖就好了. 所以我們可以將 Subnet 的網路設定做一個整理: IP address = 140.115.50.160 ~ 140.115.50.191 netmask = 255.255.255.224 broadcast = 140.115.50.191 network address = 140.115.50.160 如何設定 Router 的網路位址 決定好 Subnet 的 IP address 後,接下來就是 Config eth0 和 eth1 這兩個 網路介面,於 /etc/rc.d/rc,inet1 中加入 ifconfig eth0 140.115.50.67 netmask 255.255.0.0 broadcast 140.115.255.255 ifconfig eth1 140.115.50.161 netmask 255.255.255.224 broadcast 140.115.50.191 後,再執行 rc.inet1 或重新開機.可以利用 ifconfig 指令來檢查設定是否正確, 以下就是 Router 值行 ifconfig 的輸出: lo Link encap:Local Loopback inet addr:127.0.0.1 Bcast:127.255.255.255 Mask:255.0.0.0 UP BROADCAST LOOPBACK RUNNING MTU:2000 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 TX packets:24 errors:0 dropped:0 overruns:0 eth0 Link encap:10Mbps Ethernet HWaddr 04:00:23:53:90:21 inet addr:140.115.50.67 Bcast:140.115.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:107061 errors:0 dropped:0 overruns:0 TX packets:22060 errors:0 dropped:0 overruns:0 Interrupt:5 Base address:0x290 Memory:d0000d4000 eth1 Link encap:10Mbps Ethernet HWaddr 12:30:51:47:23:85 inet addr:140.115.50.161 Bcast:140.115.50.191 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:107061 errors:0 dropped:0 overruns:0 TX packets:22060 errors:0 dropped:0 overruns:0 Interrupt:6 Base address:0x350 Memory:d8000db000 如何設定 Router 的 Kernel Routing table 網路位址設定好後,接下來就是要設定 Kernel Routing table,當 Router 收到一個 Packet 時,會依照 Packet 中的目的位址去 Kernel Routing table 比對,而決定該往 那一個網路介面送,所以 Kernel Routing table設錯了,Packet 就無法由正確的網路介 面送到目的地. Kernel Routing table 的設定亦加入於 /etc/rc.d/rc.inet1 route add net 140.115.0.0 netmask 255.255.0.0 eth0 route add net 140.115.50.160 netmask 255.255.255.224 eth1 route add default gw 140.115.1.254 metric 1 ^^^^^^^^^^^^^^^^^^^^^^^^^ 此為通往校外的 Gateway 後,再執行 rc.inet1 或重新開機,可以利用 route 指令來檢查設定是否正確,以下就是 Router 值行 route 的輸出: Destination Gateway Genmask Flags MSS Window Use Iface 140.115.50.160 * 255.255.255.224 U 1436 0 234 eth1 140.115.0.0 * 255.255.0.0 U 1436 0 29125 eth0 127.0.0.0 * 255.0.0.0 U 1936 0 34 lo default 140.115.1.254 * UG 1436 0 79 eth0 如何設定 Subnet 中機器的網路位址與 Kernel Routing table Subnet 中的機器其設定方法較 Router 簡單.其只有一張網路卡,即 eth0,所以在 機器 ds170 的 /etc/rc.d/rc.inet1 中加入 ifconfig eth0 140.115.50.170 netmask 255.255.255.224 broadcast 140.115.50.191 route add net 140.115.50.160 netmask 255.255.255.224 eth0 route add default gw 140.115.50.161 metric 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 此我們將 Gateway 設為 Router 的 eth1 IP address 設定Proxy ARP 來連通 Subnet 以上設定完成後,你就可以由 Router 連上 ds170,亦可以由 ds170 聯上 Router, 但是 若要由 ds170 直接連到外面 Class B net 上之機器,如 dslab,則無法直接連通, 因為 ds170 直接將 package(注意是資料不是ARP) 送到 router(可由 static routing table 中決定),router 再幫你在 class B network 中送 ARP,所以 dslab 會聽到此 ARP ,但當 dslab 要將 ARP 送回來時,由於 router 或 gateway 會將 broadcast 和 multicast 的 package 擋下來,而不會forwarding,所以說 dslab 回應的 ARP 無法讓 ds170 所收到 ,於是我們需在 Router上設定 Proxy ARP,其功能就是代 subnet 中的機 器回應 class B 中機器所 broadcast 的 ARP,讓 class B 中要送往 subnet 的 package 先送到 router, 之後 router 再幫其送往 subnet 中的機器. 所以應該在 Router 的 /etc/rc.d/rc.inet1 中加入 arp s 140.115.50.170 04:00:23:53:90:21 pub ^^^^^^^^^^^^^^^^^ 此為 Router eth0 介面的 hardware address




Copyright © Linux教程網 All Rights Reserved