如何用 Linux 拨号上网

/ns/cn/jc/data/20020311140526.htm

作者: 陈汉柱

一、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 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