歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 如何用Linux撥號上網

如何用Linux撥號上網

日期:2017/2/27 9:49:04   编辑:更多Linux
一、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' account 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 con



  
  附錄一: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 con



Copyright © Linux教程網 All Rights Reserved