in Uncategorized

Linux下VPN(pptp)客户端的设置

由于新注册一个VPN账号,所以特地想试用一下,在Windows下很简单,只要在创建连接中根据提示,填入信息一步一步下来就可以自动连接到VPN服务器上。但在Linux下相对比较麻烦,花了几个小时,终于设置成功了。

首先下载安装所需的PPTP程序包。

sudo apt-get install pptp-linux

然后创建或编辑文件/etc/ppp/options.pptp,具体内容如下:

###############################################################################
# $Id: options.pptp,v 1.1 2005/02/18 01:40:23 quozl Exp $
#
# Sample PPTP PPP options file /etc/ppp/options.pptp
# Options used by PPP when a connection is made by a PPTP client.
# This file can be referred to by an /etc/ppp/peers file for the tunnel.
# Changes are effective on the next connection.  See "man pppd".
#
# You are expected to change this file to suit your system.  As
# packaged, it requires PPP 2.4.2 or later from http://ppp.samba.org/
# and the kernel MPPE module available from the CVS repository also on
# http://ppp.samba.org/, which is packaged for DKMS as kernel_ppp_mppe.
###############################################################################

# Lock the port
lock

# Authentication
# We don't need the tunnel server to authenticate itself
noauth

# We won't do EAP, CHAP, or MSCHAP, but we will accept MSCHAP-V2
refuse-eap
refuse-chap
refuse-mschap

# Compression
# Turn off compression protocols we know won't be used
nobsdcomp
nodeflate

# Encryption
# (There have been multiple versions of PPP with encryption support,
# choose with of the following sections you will use.  Note that MPPE
# requires the use of MSCHAP-V2 during authentication)

# http://ppp.samba.org/ the PPP project version of PPP by Paul Mackarras
# ppp-2.4.2 or later with MPPE only, kernel module ppp_mppe.o
# {{{
# Require MPPE 128-bit encryption
require-mppe-128
# }}}

# http://polbox.com/h/hs001/ fork from PPP project by Jan Dubiec
# ppp-2.4.2 or later with MPPE and MPPC, kernel module ppp_mppe_mppc.o
# {{{
# Require MPPE 128-bit encryption
#mppe required,stateless
# }}}

接下来创建或编辑用户名及密码文件/etc/ppp/chap-secrets, 内容如下:

# Secrets for authentication using CHAP
# client	server	secret			IP addresses
username 	PPTP	password	*

完成之后,需要创建文件/etc/ppp/peers/$TUNNEL,注意,这里的$TUNNEL指的是你将来用pon [参数],中的参数名。我们可以把它设定为vpn, 或你公司的名字。文件内容如下:

pty "pptp $VPN_SERVER_URL --nolaunchpppd" #VPN_SERVER_URL应该为你的VPN服务器地址
name $USERNAME                                      #你的VPN账号
remotename PPTP
require-mppe-128
file /etc/ppp/options.pptp
ipparam $TUNNEL                                       #这里是vpn,或你公司的名字

完成之后就可以使用命令:

pon $TUNNEL  #你自己设定的名称

来启动拨号连接。有些时候可能会有问题,如果权限不够,请用root来启动。提供了一种排错的启动方式,可以用一下命令启动连接,查看问题原因:

pon $TUNNEL debug dump logfd 2 nodetach

连接成功了,可是发现我上网的数据并没有通过VPN服务器,肯定是路由表的问题了,接下来需要设定内核路由表。
首先保证连接到VPN服务器的路由正常,数据包必须从原网络适配器出发,走ISP的网关。比如我用的是3G无线上网,所以我默认的网络接口为ppp0, 新的VPN连接的网络接口被分配为ppp1. 通过route命令添加一条路由:

sudo route add -host vpn.server.com dev ppp0

这样就保证了从本机到VPN服务器链路的正常,没有这条路由,会导致数据包在PPP1网络接口上的死循环。
然后删除就的default路由信息和加入新的default路由:

sudo route del default dev ppp0
sudo route add default dev ppp1

好了,这样就完成了路由的转换,以后所有上网的数据包都回通过VPN服务器了。打开浏览器,却发现什么网也上不了,用Ping进行测试,提示Unknow server, 呵呵,肯定是DNS又连接不上了,一定是ISP限定了只有从内网出发的机器才能访问该DNS服务器。于是用

more /etc/resov.conf

查出DNS服务器地址为

nameserver 79.170.225.139
nameserver 79.170.224.1

并用route查出ISP内网中我的网关地址为10.87.249.85。route命令执行后的显示为:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.64.64.64     *               255.255.255.255 UH    0      0        0 ppp0
10.89.234.1     *               255.255.255.255 UH    0      0        0 ppp1
10.87.249.85    *               255.255.255.255 UH    0      0        0 ppp0
default         *               0.0.0.0         U     0      0        0 ppp1

从以上的路由表也可以看出,VPN服务器的内网网关地址为10.89.234.1。接下来添加到达DNS服务器的路由:

sudo route add -host 79.170.225.139 gw 10.87.249.85
sudo route add -host 79.170.224.1 gw 10.87.249.85

再次用Ping测试,一切正常了,浏览器也能顺利的打开网页,进入IP查询网站和水木,均显示IP不是我的真实IP,宣告配置成功。
参考文献:
http://pptpclient.sourceforge.net/howto-debian.phtml#install

Write a Comment

Comment