歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> 如何用Linux撥號上網

如何用Linux撥號上網

日期:2017/3/6 15:39:23   编辑:關於Unix
一、Linux上的PPP Linux中已經包括了點對點協議PPP,其主體是pppd,如果你已經使用過 Windows 95或NT的撥號 網絡 , 那麼你已經具備了PPP的基本經驗了。在Windows 95的PPP在同ISP連接後,視ISP情況不同,會自 動為Windows 客戶機提供IP地址和DNS 服務器 地址 一、Linux 上的 PPP
Linux 中已經包括了點對點協議PPP,其主體是 pppd ,如果你已經使用過 Windows95 或 NT 的撥號網絡
那麼你已經具備了 PPP 的基本經驗了。在 Windows95 的 PPP 在同 ISP 連接後,視 ISP 情況不同,會自
動為 Windows 客戶機提供 IP 地址和 DNS 服務器地址,而 Linux 的 PPP 不會自動接收 DNS 地址,這是
Linux 與 Windows 的一點區別,所以,Linux 上的 PPP 在協議連接成功後,要多一步 DNS 的配置。

二、Kernel 的准備
你的 Linux 系統必須在 kernel 中配置支持 PPP 功能。如果你的 kernel 目前不支持 PPP,你需要重新生
成一個支持 PPP 的 kernel。重新編譯 kernel 的方法可看相關 Howto。

三、用輔助腳本配置 PPP 撥號網絡
我一開始想運行 pppd 來連接撥號網絡,但很快發現這是太麻煩了,因為選項太多。後來我發現,其實在
Linux 上已經提供了幾個 PPP 撥號程序模版,只要對這些模板略作修改,就可輕松聯上 PPP 了。根據我
的經驗,如果順利的話,10分鐘就能完成配置。

(1)撥號程序所在的目錄

對於 RedHat 5.0,這幾個撥號程序(Script)在 /usr/doc/ppp-2.2.0f/scripts 下,如果要使用這些腳本,
需要把它們拷貝到正確的目錄中,具體如下:
ppp-on: PPP 連接腳本 拷貝到 /usr/sbin
ppp-off: PPP 斷開腳本 拷貝到 /usr/sbin
ppp-on-dialer: PPP 登錄會話腳本 拷貝到 /etc/ppp
ppp-redialer: 可輪循多個電話號碼的登陸腳本 拷貝到 /usr/sbin (本文不用)
對於 Slackware 的用戶,這些腳本已經安裝到正確的目錄了,無需做什麼。
假定你在你的系統上找不到上述文件,可直接在本文後面的附錄中cut。

(2)創建自己的撥號程序

假定現在我們要用 PPP 連接 163 網,我們就可以利用上述腳本來作很簡單的配置。雖然我們可以直接在這
些模板上做修改,我還是建議為自己拷貝一份模板,在新的拷貝上作修改比較好。所以,我們先做如下拷貝:

cd /usr/sbin
cp ppp-on ppp.163
chmod +x ppp.163
cd /etc/ppp
cp ppp-on-dialer dialer.163
chmod +x dialer.163

此外,要保證 ppp-off 也是可執行的

(3)定制自己的撥號程序

首先編輯你的 ppp.163,其中多數內容都不必改動,只要修改以下幾處:

TELEPHONE=163
ACCOUNT=
PASSWORD=
DIALER_SCRIPT=/etc/ppp/dialer.163

另外,在最後的 exec 啟動 pppd 一行裡,注意提供你的 Modem 所在的串口設備文件(通常是
COM1 /dev/cua0 或 /dev/ttyS0,COM2 /dev/cua1 或 /dev/ttyS1),並設置串口的通信速度(缺省是 38400,
對於現在的高速 Modem,這個速度太低了,可設置成 57600以上)。完成後的 ppp.163 內容是:

#!/bin/sh
#
# Script to initiate a ppp connection. This is the first part of the
# pair of scripts. This is not a secure pair of scripts as the codes
# are visible with the 'ps' command. However, it is simple.
#
# These are the parameters. Change as needed.
TELEPHONE=163 # 撥號號碼
ACCOUNT=YourUserName # 用戶名
PASSWORD=YourPassword # 口令,注意限制其他人讀取本文件!
LOCAL_IP=0.0.0.0 # 本地IP地址,0.0.0.0 表示動態分配
REMOTE_IP=0.0.0.0 # 原端IP地址,一般是 0.0.0.0
NETMASK=255.255.255.0 # 子網掩碼
export TELEPHONE ACCOUNT PASSWORD
#
# This is the location of the script which dials the phone and logs
# in. Please use the absolute file name as the $PATH variable is not
# used on the connect option. (To do so on a 'root' aclearcase/" target="_blank" >ccount would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=/etc/ppp/dialer.163 # 撥號登錄腳本文件名
#
# Initiate the connection
#
# I put most of the common options on this command. Please, don't
# forget the 'lock' option or some programs such as mgetty will not
# work. The asyncmap and escape will permit the PPP link to work with
# a telnet or rlogin connection. You are welcome to make any changes
# as desired. Don't use the 'defaultroute' option if you currently
# have a default route to an ethernet gateway.
#
exec /usr/sbin/pppd debug lock modem crtscts /dev/ttycua0 57600 \
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

然後,檢查一下你的登錄會話程序 /etc/ppp/dialer.163,在我所在的 163 ISP,這個從模板拷貝過來的
會話程序可直接使用,如果有以下情況,你需要對這個會話程序略作改動:

(1)如果撥號提示輸入用戶名的提示符不是 login:
(2)如果登錄成功後還要用戶再交互打入命令 ,才啟動 ppp 協議
本文後面的附錄三是一個根據上述要求略加修改後的登錄腳本。
建議:假如你有多個 ISP 可連接,就創建多個撥號 PPP 連接腳本和相應的 PPP 登錄腳本。

四、啟動 PPP 和斷開 PPP
啟動你的 PPP 連接唯一要做的就是開啟 Modem,運行剛才寫好的 ppp.163。你的 ppp.163 腳本將自動撥
號、送出登錄信息、完成協議認證,等你看到你的 Modem 上的數據傳輸指示燈不再閃動並保持連接狀態,
PPP 連接就完成了,你可以打命令:

netstat -r

查看當前的路由表,如果 PPP 協議連接成功的話,路由表裡就會有連接你的 ISP 的缺省路由。你的 PPP
已經配置成功了。

如果沒有成功,注意看一下 /etc/ppp 下的 PPP 出錯記錄文件。

要斷開 PPP 連接,運行 ppp-off。

五、DNS 的配置
PPP 連接成功後,如果要使用 WWW 浏覽器、Ftp、Telnet 等服務,必須正確配置 DNS 域名解析。所以,
一旦 PPP 連接完畢,應該在 /etc/resolv.conf 中加入一行:

nameserver

注意 nameserver 後面的是你的 ISP 的 DNS 服務器的 IP 地址,並且這一行說明必須放在其他的
nameserver 說明之前,否則,你的 Linux 系統不會訪問你的 ISP 的 DNS 服務器。然後,你就能使用各
項 Internet 服務了。

六、自動配置 DNS 解析
本節內容並不是配置 PPP 必需的,但能對 PPP 的使用提供一些方便。

由於你的 Linux 並不總是連在 Internet 上,所以在不連接 PPP 時,需要的 resolv.conf 是不同的。
所以,我們希望有一種辦法能根據不同的情況,在 Linux 系統上自動設置合適的 resolv.conf 。

在 /etc/ppp 下,有兩個根據 PPP 連接狀態自動運行的腳本,ip-up 和 ip-down,分別在 PPP 建立連接
和斷開連接時運行,如果用戶希望在 PPP 連接或斷開時執行某些命令,可以在 /etc/ppp 目錄下建立腳
本文件 ip-up.local 和 ip-up.local,在這兩個文件中放入要執行的命令。當 PPP 建立連接後,ip-up
會調用 ip-up.local,執行用戶命令;當 PPP 斷開連接後,ip-down 會調用 ip-down.local ,執行用戶
命令。

所以我們可以利用這個機制實現 DNS 解析的自動配置,方法如下:

(1)在 /etc 下編輯分別適用於 PPP 連接狀態和正常本地狀態的 DNS 解析配置文件,我們假定這兩個
文件是 resolv.ppp 和 resolv.normal。

(2)在 /etc/ppp 下建立 ip-up.local,設置其執行權限,並在文件中加入:

cp /etc/resolv.ppp /etc/resolv.conf

(3)在 /etc/ppp 下建立 ip-down.local,設置其執行權限,並在文件中加入:

cp /etc/resolv.normal /etc/resolv.conf

這樣設置後,每當 PPP 連接成功後,/etc/resolv.conf 的內容就是 resolv.ppp,而 PPP 斷開後,
/etc/resolv.conf 被恢復成 resolv.normal 的內容。

附錄一:PPP 連接腳本模板 ppp-on

#!/bin/sh
#
# Script to initiate a ppp connection. This is the first part of the
# pair of scripts. This is not a secure pair of scripts as the codes
# are visible with the 'ps' command. However, it is simple.
#
# These are the parameters. Change as needed.
TELEPHONE=555-1212 # The telephone number for the connection
ACCOUNT=george # The account name for logon
PASSWORD=gracie # The password for this account
LOCAL_IP=0.0.0.0 # Local IP address if known. Dynamic = 0.0.0.0
REMOTE_IP=0.0.0.0 # Remote IP address if desired. Normally 0.0.0.0
NETMASK=255.255.255.0 # The proper netmask if needed
#
# Export them so that they will be available at 'ppp-on-dialer' time.
export TELEPHONE ACCOUNT PASSWORD
#
# This is the location of the script which dials the phone and logs
# in. Please use the absolute file name as the $PATH variable is not
# used on the connect option. (To do so on a 'root' account would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=/etc/ppp/ppp-on-dialer
#
# Initiate the connection
#
# I put most of the common options on this command. Please, don't
# forget the 'lock' option or some programs such as mgetty will not
# work. The asyncmap and escape will permit the PPP link to work with
# a telnet or rlogin connection. You are welcome to make any changes
# as desired. Don't use the 'defaultroute' option if you currently
# have a default route to an ethernet gateway.
#
exec /usr/sbin/pppd debug lock modem crtscts /dev/ttyS0 38400 \
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP \
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT

附錄二:PPP 登錄腳本模板 ppp-on-dialer

#!/bin/sh
#
# This is part 2 of the ppp-on script. It will perform the connection
# protocol for the desired connection.
#
exec chat -v \
TIMEOUT 3 \
ABORT '\nBUSY\r' \
ABORT '\nNO ANSWER\r' \
ABORT '\nRINGING\r\n\r\nRINGING\r' \
' \rAT \
'OK-+++\c-OK' ATH0 \
TIMEOUT 30 \
OK ATDT$TELEPHONE \
CONNECT ' \
ogin:--ogin: $ACCOUNT \
assword: $PASSWORD

附錄三:一個稍作修改的 PPP 登錄腳本
#!/bin/sh
#
# This is part 2 of the ppp-on script. It will perform the connection
# protocol for the desired connection.
#
exec chat -v \
TIMEOUT 3 \
ABORT '\nBUSY\r' \
ABORT '\nNO ANSWER\r' \
ABORT '\nRINGING\r\n\r\nRINGING\r' \
' \rAT \
'OK-+++\c-OK' ATH0 \
TIMEOUT 30 \
OK ATDT$TELEPHONE \
CONNECT ' \
sername:--sername: $ACCOUNT \
assword: $PASSWORD \
nnex: ppp

說明:(1)用戶登錄提示符從由 login: 改為 username:

(2)用戶登錄後,訪問服務器出現提示符 annex: ,需要手工輸入 ppp 命令

附錄四:可輪詢撥號的 PPP 連接腳本模板 ppp-redialer

#!/bin/sh
###################################################################
#
# These parameters control the attack dialing sequence.
#
# Maximum number of attempts to reach the telephone number(s)
MAX_ATTEMPTS=10
# Delay between each of the attempts. This is a parameter to sleep
# so use "15s" for 15 seconds, "1m" for 1 minute, etc.
SLEEP_DELAY=15s
###################################################################
#
# This is a list of telephone numbers. Add new numbers if you wish
# and see the function 'callall' below for the dial process.
PHONE1=555-1212
PHONE2=411
###################################################################
#
# If you use the ppp-on script, then these are passed to this routine
# automatically. There is no need to define them here. If not, then
# you will need to set the values.
#
ACCOUNT=my_account_name
PASSWORD=my_password
###################################################################
#
# Function to initialize the modem and ensure that it is in command
# state. This may not be needed, but it doesn't hurt.
#
function initialize
{
chat -v TIMEOUT 3 ' AT 'OK-+++\c-OK'
return
}
###################################################################
#
# Script to dial a telephone
#
function callnumber
{
chat -v \
ABORT '\nBUSY\r' \
ABORT '\nNO ANSWER\r' \
ABORT '\nRINGING\r\n\r\nRINGING\r' \
' ATDT$1 \
CONNECT ' \
ogin:--ogin: $ACCOUNT \
assword: $PASSWORD
#
# If the connection was successful then end the whole script with a
# success.
#
if [ "$?" = "0" ]; then
exit 0
fi
return
}
###################################################################
#
# Script to dial any telephone number
#
function callall
{
# echo "dialing attempt number: $1" >/dev/console
callnumber $PHONE1
# callnumber $PHONE2
}
###################################################################
#
# Initialize the modem to ensure that it is in the command state
#
initialize
if [ ! "$?" = "0" ]; then
exit 1
fi
#
# Dial telephone numbers until one answers
#
attempt=0
while : ; do
attempt=`expr $attempt + 1`
callall $attempt
if [ "$attempt" = "$MAX_ATTEMPTS" ]; then
exit 1
fi
sleep "$SLEEP_DELAY"
done

附錄五:PPP 斷開腳本 ppp-off

#!/bin/sh
######################################################################
#
# Determine the device to be terminated.
#
if [ "$1" = "" ]; then
DEVICE=ppp0
else
DEVICE=$1
fi
######################################################################
#
# If the ppp0 pid file is present then the program is running. Stop it.
if [ -r /var/run/$DEVICE.pid ]; then
kill -INT `cat /var/run/$DEVICE.pid`
#
# If the kill did not work then there is no process running for this
# pid. It may also mean that the lock file will be left. You may wish
# to delete the lock file at the same time.
if [ ! "$?" = "0" ]; then
rm -f /var/run/$DEVICE.pid
echo "ERROR: Removed stale pid file"
exit 1
fi
#
# Success. Let pppd clean up its own junk.
echo "PPP link to $DEVICE terminated."
exit 0
fi
#
# The ppp process is not running for ppp0
echo "ERROR: PPP link is not active on $DEVICE"
exit 1

Copyright © Linux教程網 All Rights Reserved