歡迎您光臨本站 註冊首頁

Linux使用adsl計時制分享

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
前言∶

最近台灣網路環境的蓬勃發展,許多寬頻上網的方案更是讓大家爭相申請,其中 ADSL 也是許多網路族的最愛,以 Hinet 所推出的 ADSL 為例,有分固定製和計時制兩種,其中最大的差別在於固定製有固定的 IP 位址,所以價格較貴,而計時制則採用動態分配 IP 的方式,價格較為便宜.一般來說,如果小型的公司機構或是個人 SOHO 族會申請固定製,以提供一些伺服器的功能,如 Web、FTP……等等,而一般的民眾則會選擇申請計時制的.
一般說來,現在擁有超過兩部電腦的家庭越來越多了,或者是學生們一起租外面的公寓,每個人有一部電腦也不是甚麼稀奇的事,這時候大家就會希望以最少的花費讓所有的電腦都能上網,但以 Hinet 的 ADSL 計時製為例,最多也只能同時提供兩部電腦同時撥接,如何能利用其中一部電腦來提供網路分享的功能,也變成網路上非常的熱門的話題,而 Linux 優異的網路特性,當然能夠為大家提供理想的網路共享環境.
由於計時制的 ADSL 有較多的人申請,所以這篇文章會教大家如何配合 Linux 來讓 ADSL 計時制達到最大的效益.

安裝前準備∶

硬體∶
一般說來,要以 Linux 拿來做路由的 PC 等級並不需要很高,486 或 Pentium 100 等級的電腦就綽綽有餘了,重點是在網路卡,他們負責所有的網路交通,所以挑選兩張較好的網卡是網路順暢的關鍵.

軟體∶
Linux 的作業系統∶
在這篇文章中將以 OpenLinux 的 eServer 2.3 及 eDesktop 2.4 為例,所使用的 packet filter 工具為 ipchains 套件(內建),以避免因 Linux Distributions 的版本不同而產生的問題.
pppd 撥接軟體∶
要使用 ADSL 計時制的撥接軟體,要先確定你的系統中有安裝 pppd 才能安裝,版本必需為 2.3.10 或之後的版本,在 OpenLinux 的 eServer 2.3 及 eDesktop 2.4 中皆有.


rp-pppoe ADSL 撥接軟體∶
ADSL 計時制是采 PPPoE (Point-to-Point Over Ethernet) 的通訊協定,所以我們必須要找一套在 Linux 上執行的撥接軟體,本文所採用的軟體為 rp-pppoe,目前最新的版本為 3.2 版,你可以至 http://www.roaringpenguin.com/pppoe/ 查詢相關的資料及下載檔案,本文所下載的檔案範例為 tarBall 的原始檔 rp-pppoe-3.2.tar.gz.

在進行安裝之前,請先確定你的兩張網卡皆驅動正常,且先確定哪張網卡對內,哪張網卡對外,本文將以 eth0 對內,且 ip 設為私有 ip (192.168.1.254),eth1 對外,將 ip 設為 dhcp 的方式取得,但預設開機時不啟動,你可以使用 ifconfig 的指令檢查如下就表示正確了.

[root@linux /root]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:00:E8:11:E2:F1
inet addr:192.168.1.254 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
Interrupt:5 Base address:0x300

eth1 Link encap:Ethernet HWaddr 00:00:E8:11:E2:F2
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
Interrupt:10 Base address:0x320

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:3924 Metric:1
RX packets:24 errors:0 dropped:0 overruns:0 frame:0
TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0

開始安裝∶

將你於 http://www.roaringpenguin.com/pppoe/ 所取得的 rp-pppoe-3.2.tar.gz 以 tar 指令解開.

[root@linux /root]# tar zxvf rp-pppoe-3.2.tar.gz

解開後會產生一個名為 rp-pppoe-3.2 的目錄,你可以先閱讀裡面的 README 檔,裡面有簡介一些使用及安裝方式,本文介紹自行編譯的方式,所以先將工作目錄切換至 rp-pppoe-3.2/src/ 下.



[root@linux /root]# cd rp-pppoe-3.2/src/

接著依序輸入下列指令,就可以完成安裝.

[root@linux src]# ./configure
[root@linux src]# make
[root@linux src]# make install

設定∶

安裝完成之後,接著我們來設定撥接 ADSL 的相關資料,輸入下面的指令開始設定∶

[root@linux /root]# adsl-setup
Welcome to the Roaring Penguin ADSL client setup. First, I will run
some checks on your system to make sure the PPPoE client is installed
properly...

Looks good! Now, please enter some information:

USER NAME

>>> Enter your PPPoE user name (default bxxxnxnx@shinewave.com):84xxxxxx@hinet.net

在這裡輸入 ISP 給你的帳號,如 84xxxxxx@hinet.net

INTERFACE

>>> Enter the Ethernet interface connected to the ADSL modem
For Solaris, this is likely to be something like /dev/hme0.
For Linux, it will be ethn, where 'n' is a number.
(default eth1):eth1

輸入要用來撥接 ADSL 的界面,如本文例為 eth1

Do you want the link to come up on demand, or stay up continuously?
If you want it to come up on demand, enter the idle time in seconds
after which the link should be dropped. If you want the link to
stay up permanently, enter 'no' (two letters, lower-case.)
NOTE: Demand-activated links do not interact well with dynamic IP
addresses. You may have some problems with demand-activated links.
>>> Enter the demand value (default no):no

這裡是設定 ADSL 是否自動斷線,如果你希望 ADSL 一直保持連線狀態則輸入 no,如果你希望一段時間沒有使用則自動斷線的話,則輸入你希望的秒數

 

DNS

Please enter the IP address of your ISP's primary DNS server.
If your ISP claims that 'the server will provide DNS addresses',
enter 'server' (all lower-case) here.
If you just press enter, I will assume you know what you are


doing and not modify your DNS setup.
>>> Enter the DNS information here:168.95.1.1

輸入你的 DNS Server 的 ip,如 Hinet 所提供的 168.95.1.1

 

Please enter the IP address of your ISP's secondary DNS server.
If you just press enter, I will assume there is only one DNS server.
>>> Enter the secondary DNS server address here:168.95.192.1

輸入你次要的 DNS Server 的 ip,如 Hinet 所提供的 168.95.192.1

 

PASSWORD

>>> Please enter your PPPoE password:********
>>> Please re-enter your PPPoE password:********

輸入 ISP 提供給你撥接 ADSL 的密碼,並多輸入一次以確認密碼正確

 

FIREWALLING

Please choose the firewall rules to use. Note that these rules are
very basic. You are strongly encouraged to use a more sophisticated
firewall setup; however, these will provide basic security. If you
are running any servers on your machine, you must choose 'NONE' and
set up firewalling yourself. Otherwise, the firewall rules will deny
access to all standard servers like Web, e-mail, ftp, etc. If you
are using SSH, the rules will block outgoing SSH connections which
allocate a privileged source port.

The firewall choices are:
0 - NONE: This script will not set any firewall rules. You are responsible
for ensuring the security of your machine. You are STRONGLY
recommended to use some kind of firewall rules.
1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
for a LAN
>>> Choose a type of firewall (0-2):0

這個選項是設定 firewall,共有 0、1、2 三種設定,0 是沒有設定任何有關 firewall 的選項,只有提供撥接至 Internet 的功能,1 是為你提供一些基本的 firewall 功能,較適合當你的 Linux 機器是獨立的 Server 時選擇,2 是由程式幫你設定 firewall的功能,把 Linux 設成 Gateway 的角色,讓內部機器可以連外;這裡我們選擇 0,沒有 firewall 的設定來示範,這樣我們可以使用 ipchains 來設定自己的 firewall 的功能



** Summary of what you entered **

Ethernet Interface: eth1
User name: 84xxxxxx@hinet.net
Activate-on-demand: No
Primary DNS: 168.95.1.1
Secondary DNS: 168.95.192.1
Firewalling: NONE

>>> Accept these settings and adjust configuration files (y/n)?y

上表是將我們剛剛設定值的列表,在你確認所有的設定都正確無誤之後,就按 y

Adjusting /etc/ppp/pppoe.conf
Adjusting /etc/resolv.conf
(But first backing it up to /etc/resolv.conf-bak)
Adjusting /etc/ppp/pap-secrets and /etc/ppp/chap-secrets
(But first backing it up to /etc/ppp/pap-secrets-bak)
(But first backing it up to /etc/ppp/chap-secrets-bak)

Congratulations, it should be all set up!

Type 'adsl-start' to bring up your ADSL link and 'adsl-stop' to bring
it down. Type 'adsl-status' to see the link status.

到這裡,ADSL 的撥接程式的設定完成了


設定完成之後,我們可以先來測試看看,當然你要將 eth1 接上 ISP 所提供的ADSL Router

撥接 ADSL 啟動連線∶ [root@linux /root]# adsl-start

查看 ADSL 的連線狀態∶ [root@linux /root]# adsl-status

結束 ADSL 的連線∶ [root@linux /root]# adsl-stop

在完成了 ADSL 撥接程式的安裝及測試之後,我們要設定讓這一部 Linux 的機器能在開機時就能自動撥接上 Internet 且斷線后還能自動恢復連線,還有要加上 NAT 的功能讓所有的電腦都能共享頻寬.

在開機時就要讓 ADSL 的撥接程式就自動啟動,讓程式開機時就自動啟動的方式最簡單的就是在 /etc/rc.d/rc.local 這個檔案的加上一行你所要執行的程式的指令,如下所示∶
/usr/sbin/adsl-start

而有鑒於 ADSL 計時制的連線品質並不是很穩定,偶爾還會給你斷線一下,所以我在 /etc/crontab 檔案的加上一行下面的設定.
*/5 * * * * root /usr/sbin/adsl-start 2>/dev/null


這一行的設定表示,每5分鐘執行一次 adsl-start 來啟動 ADSL,而 adsl-start 這個程式本身會偵測現在 ADSL 的連線是否正常,如果連線正常,則會傳回一個錯誤訊息表示已經是連線狀態了,不給予執行,所以我使用 「2>/dev/null」 將這個錯誤訊息導出至 /dev/null 這個無底洞裝置,否則你每5分鐘就會收到一封 cron 程式寄給你的信 ( cron 程式執行時若有錯誤訊息,會以 mail 的方式通知管理者),如果連線中斷的話,除非是對方機房的問題,否則最多不超過5分鐘又可以恢復正常的連線羅!

接著我們要來使這一部 Linux 的機器有 NAT 的功能,要將 IPFORWARDING 的功能打開,我們有裝兩張網卡,要讓這兩張網卡的所接觸到的網段能透過這一部 Linux 的機器來達到溝通轉換的功能,所以我們在 /etc/sysconfig/network 這個檔案中加上一行∶
IPFORWARDING=yes
然後我們要使我們內部的網段(192.168.1)的封包,能夠藉由這一部 Linux 的機器偽裝出去, 192.168.1 的網段是屬於私有 ip,外界的 route 如果遇到這一類的封包會將之拋棄,所以我們必需利用我們所撥接 ADSL 所得到的公有ip,將我們的私有ip 重新包裝再轉送出去,所以我們在 /etc/rc.d/rc.local 這個檔案中再加上一行
ipchains -A forward -j MASQ -s 192.168.1.0/24
這一行的意思是在 forward 這個 chian 中增加一條規則,只要封包的來源 ip 是192.168.1.0/24的網段的,就將之偽裝后再送出.
,偽裝出去的封包,在使用某些通訊協定上會造成一些問題,如 ftp 是最常見的,好在現在大部份的問題都已經有方法可以排除,我們使用外掛模組的方式將這些問題解決,這些解決偽裝封包的問題的模組大多位在 /lib/modules/2.2.14/ipv4/ 的目錄下,並以 ip_masq_*.o 的方式命名,所以我們可以在 /etc/modules/default 這個檔案中加上模組的名稱,以讓其開機時就可以載入.


ip_masq_autofw
ip_masq_cuseeme
ip_masq_ftp
ip_masq_irc
ip_masq_mfw
ip_masq_portfw
ip_masq_quake
ip_masq_raudio
ip_masq_user
ip_masq_vdolive
到這裡為止,你的這一部 Linux 的電腦在重開機之後就是一部可以提供寬頻共享的機器了,

接下來我們來看看網路方面的架構,在這部 Linux 的機器上有兩張網卡,一張當然要接上 ISP 公司所提供的 ADSL Router,本文中是 eth1 這張網卡,而另外一張 eth0 則是提供 Gateway 的功能,讓大家可以透過它來上網,所以你必須準備一個 HUB 來串接所有的內部網路,如下圖∶



網路硬體的架構完成之後,只要設定 Client 端就好了,以本文為例,Client端可以使用的 ip 從 192.168.1.1~192.168.1.253,這麼多的 ip 你可以好好規劃,而每一部內部的電腦當然要把 Gateway 指向 192.168.1.254,此外 DNS 可以直接使用 ISP 公司所提供的,如 Hinet 所提供的 168.95.1.1 及 168.95.192.1,當然你也可以架設 DHCP Server 來自動幫所有的 Client 設定所有的 ip 資訊,設定完成後,就可以大家一起享受寬頻上網羅!還蠻划算的呢!


作者∶Jui-li CHIU
轉載:www.linuxcenter.com.tw


[火星人 ] Linux使用adsl計時制分享已經有527次圍觀

http://coctec.com/docs/linux/show-post-56446.html