CentOS 5/6 快速安装 VPN Server (PPTP)

“VPN”是一种常用于中、大型企业或团体与团体间的私人网络的通讯方法。借由 VPN 所建立的加密通道,可以让你从外部存取公司内部的资源,或透过连线到 VPN 的 Server 当跳板,去存取被限制的外部资源 (俗称“翻墙”)。

在使用 VPN 之前,你得先有一台提供服务的 VPN Server,现在满多人会去租用国外的 VPS 当 Server,要架站兼翻墙,Linode 是不错的选择。

以下把握几个重点即可快速完成 Linux 的 PPTP 的 VPN Server 架设:

  • PPP - 点对点协定 (Point-to-Point Protocol)
  • PPTP - 点对点通道协定 (Point-to-Point Tunneling Protocol)
  • iptables - 设定封包转发规则

安装步骤

1. 判断 ppp 是否可用:

$ cat /dev/ppp
cat: /dev/ppp: No such device or address

如果出现跟上面一样的讯息“No such device or address”,那不用担心,这表示 ppp 是可用的,可以正常架设 pptp。

如果出现的是“Permission denied”,表示 ppp 是关闭的,下面的步骤就可以不用再看了。

2. 安装 ppp 与 iptables

$ yum -y install ppp iptables
$ cp -rpf /etc/sysconfig/iptables /etc/sysconfig/iptables.pptpsave
$ cd /usr/src

3. 请依照你的系统环境下载适合的 PPTP 套件:

# for CentOS 6.x 32bit:
$ wget http://poptop.sourceforge.net/yum/stable/rhel6/i386/pptpd-1.3.4-2.el6.i686.rpm
# for CentOS 6.x x64:
$ wget http://poptop.sourceforge.net/yum/stable/rhel6/x86_64/pptpd-1.3.4-2.el6.x86_64.rpm
# for CentOS 5.x 32bit:
$ wget http://poptop.sourceforge.net/yum/stable/rhel5/i386/pptpd-1.3.4-2.rhel5.i386.rpm
# for CentOS 5.x x64:
$ wget http://poptop.sourceforge.net/yum/stable/rhel5/x86_64/pptpd-1.3.4-2.rhel5.x86_64.rpm

上面这些载点如果失效,有可能是版本更新了,请自行前往 poptop 的网站下载,或是备用载点

4. 安装上一个步骤所下载的 PPTP 套件:

$ rpm -ivh pptpd-1.3.4-2.el6.i686.rpm

5. 编辑 /etc/sysctl.conf,找到:

net.ipv4.ip_forward = 0

将后面的 0 改成 1

6. 下面的指令全部给它执行下去:

sysctl -p
echo "localip 192.168.240.1" >> /etc/pptpd.conf
echo "remoteip 192.168.240.101-200" >> /etc/pptpd.conf
echo "ms-dns 8.8.8.8" >> /etc/ppp/options.pptpd
echo "ms-dns 8.8.4.4" >> /etc/ppp/options.pptpd
iptables --flush POSTROUTING --table nat
iptables --flush FORWARD
iptables -A INPUT -p tcp -m tcp --dport 1723 -j ACCEPT
iptables -A INPUT -p gre -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.240.0/24 -o eth0 -j MASQUERADE
service iptables save
service pptpd restart
service iptables restart
chkconfig pptpd on
chkconfig iptables on

这样就大功告成了!!

上面这些指令会创造一个 192.168.240.* 的网段给连上来 VPN 的用户使用,并透过 Server 上的 eth0 转送对外上网的封包。你可以自行将 192.168.240eth0 改成符合你需求的设定。

加入 VPN 帐号

接着我们要加入 VPN 的连线帐号。

编辑 /etc/ppp/chap-secrets 以加入 VPN 帐号,一个帐号一行,格式为:

帐号名称 pptpd 密码 *

例如,我要新增一个帐号为“vpn”,密码为“test123”,则只要在 /etc/ppp/chap-secrets 加入一行:

vpn pptpd test123 *

加入之后不用重新启动 pptpd,你就可以在 Client 端以 PPTP 协定用 VPN 连线至这台 Server 了。

连线测试

连线成功之后,你可以连到 ip.calm9.com 查看你是否用 VPN Server 的 IP 在上网。

下面这张图的连线资讯是我用 linode 的试用帐号所建立的 VPS,位置在“日本”:

iptables 规则说明

在上面的指令中,最重要的是这三条 iptables 规则:

iptables -A INPUT -p tcp -m tcp --dport 1723 -j ACCEPT
iptables -A INPUT -p gre -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.240.0/24 -o eth0 -j MASQUERADE

说明 PPTP 是以 GRE (Generic Routing Encapsulation) 封包透过 1723 Port 进行传输,并且设定 VPN 的网段是由哪个网络接口转送对外的封包。

错误处理

807 错误

在拨接至 VPN 时,如果出现 807 错误 (在您的电脑与 VPN 服务器之间的网络连线已经中断),或是其它在确认帐号、密码之前出现的错误,如果确定不是自己电脑的问题,你可以编辑 Server 的 /etc/sysconfig/iptables,查看这两条规则是否有正确出现在档案里面:

-A INPUT -p tcp -m tcp --dport 1723 -j ACCEPT
-A INPUT -p gre -j ACCEPT

并且,这两条规则的位置不能在任何“-A INPUT -j REJECT ...”的规则下面。若有这种情形,请把它移到“-A INPUT -J REJECT ...”的上方。

储存之后,再重新启动 iptables:

service iptables restart

734 错误

在拨接至 VPN 时,如果出现 734 错误 (已经终止 PPP 连结控制通讯协定),请修改 Windows 的 VPN 安全性设定:

  1. 将资料加密改为“可省略加密”
  2. 勾选“未加密的密码”
  3. 勾选“Challenge Handshake 验证通讯协定”

800 错误

请尝试关闭电脑的防火墙,或防毒软件的防火墙。

有网友反应: VPN Server 安装成功,并且连上了 VPN,但是上不了网,也请尝试关闭防火墙看看

参考网页

  1. 我的 IP 位址
  2. 尝试在 Windows Vista 中进行拨号连线或 VPN 连线时,可能会收到的错误码的清单
    • OL
    • 09/11. 2013 3:27下午
    Using Google Chrome Google Chrome 29.0.1547.66 on Windows Windows 7

    这个教程非常不错 学习了

    • lee
    • 09/20. 2013 10:23下午
    Using Google Chrome Google Chrome 26.0.1410.5 on Windows Windows 7

    你好我按照这个安装以后,只有win7可以正常使用vpn,Mac and linux都无法使用,mac我测试了 MBP笔记本和 iphone手机,都是可以连接,也能看到有数据交互,但是就是不能显示网页。 能不能给点debug提示,这块我是新手。谢谢。

    • jasrnine
    • 11/26. 2013 1:30上午
    Using Google Chrome Google Chrome 31.0.1650.57 on Windows Windows NT

    我到第二部的第二条就出现了错误,提示iptables 不存在。

    • farmer6
    • 12/05. 2013 5:11下午
    Using Google Chrome Google Chrome 29.0.1547.76 on Windows Windows 7

    博主的文章写得很仔细,照着配置了一次,非常顺利。谢谢。

  1. Using Google Chrome Google Chrome 33.0.1750.154 on Windows Windows 7

    出现800错误了,怎么破?
    /etc/sysconfig/iptables 内容如下

    # Generated by iptables-save v1.4.7 on Fri Mar 28 11:05:27 2014
    *nat
    :PREROUTING ACCEPT [11344:885015]
    :POSTROUTING ACCEPT [2:169]
    :OUTPUT ACCEPT [2:169]
    -A POSTROUTING -s 192.168.240.0/24 -o eth0 -j MASQUERADE
    COMMIT
    # Completed on Fri Mar 28 11:05:27 2014
    # Generated by iptables-save v1.4.7 on Fri Mar 28 11:05:27 2014
    *filter
    :INPUT ACCEPT [33741:2636152]
    :FORWARD ACCEPT [27:2325]
    :OUTPUT ACCEPT [110:15695]
    -A INPUT -p tcp -m tcp --dport 1723 -j ACCEPT
    -A INPUT -p gre -j ACCEPT
    COMMIT
    # Completed on Fri Mar 28 11:05:27 2014

    • Lzh1104
    • 04/16. 2014 10:28上午
    Using Mozilla Firefox Mozilla Firefox 28.0 on Windows Windows XP

    连不上 800 错误

    cause unexpected results. Use restart-kill to
    destroy existing connections during a restart.
    [root@localhost src]# vim /etc/rsyslog.conf

    [1]+ Stopped vim /etc/rsyslog.conf
    [root@localhost src]# vim /etc/sysconfig/iptables

    # Generated by iptables-save v1.4.7 on Wed Apr 16 10:21:44 2014
    *nat
    :PREROUTING ACCEPT [1:78]
    :POSTROUTING ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A POSTROUTING -s 192.168.11.0/24 -o eth0 -j MASQUERADE
    COMMIT
    # Completed on Wed Apr 16 10:21:44 2014
    # Generated by iptables-save v1.4.7 on Wed Apr 16 10:21:44 2014
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [41:4504]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 20 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8005 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8983 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 30000:50000 -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A INPUT -p tcp -m tcp --dport 1723 -j ACCEPT
    -A INPUT -p gre -j ACCEPT
    COMMIT
    # Completed on Wed Apr 16 10:21:44 2014

      • Lzh1104
      • 04/16. 2014 10:35上午
      Using Mozilla Firefox Mozilla Firefox 28.0 on Windows Windows XP

      这个搞定了--

        • sekaiai
        • 11/12. 2014 3:41下午
        Using Google Chrome Google Chrome 38.0.2125.111 on Windows Windows NT

        请问下如何搞定的?

  2. Using Google Chrome Google Chrome 35.0.1916.114 on Windows Windows 7

    默认安装会807,照说明做好后连接成功

    • hee
    • 07/25. 2014 11:24上午
    Using Google Chrome Google Chrome 35.0.1916.153 on Windows Windows NT

    http://ihww.cn/2014/07/25/%E5%B7%A5%E5%85%B7/centos_pptpd/

  3. Using Google Chrome Google Chrome 39.0.2171.95 on Windows Windows NT

    大大 能不能写个centos 7的教学阿
    感觉centos7 有些不能适用你的文章
    感谢你

    • Using Google Chrome Google Chrome 39.0.2171.95 on Windows Windows NT

      错误807 请问该怎么办?
      第五步骤找不到 net.ipv4.ip_forward = 0 有办法解决吗? 谢谢

    • qiao
    • 02/12. 2015 10:01下午
    Using Mozilla Firefox Mozilla Firefox 35.0 on Windows Windows 7

    807错误解决了,多谢

    • arc
    • 03/04. 2015 11:13上午
    Using Google Chrome Google Chrome 41.0.2272.74 on Windows Windows NT

    谢谢你的教学,照上面作法做完后可以顺利连上VPN,不过部分网页无法开启,参考http://www.320dr.net/bencandy.php?fid=14&id=846 后发现应该是MTU的问题,
    修改/etc/ppp/ip-up,在“exit 0”之前的任意位置添加:“ifconfig $1 mtu 1500”
    得以解决

    • leo
    • 03/27. 2015 8:28下午
    Using Google Chrome Google Chrome 39.0.2171.95 on Windows Windows NT

    安装成功并连上了,但是上不了网!

    • 大黑
    • 04/29. 2015 5:17下午
    Using Google Chrome Google Chrome 42.0.2311.135 on Windows Windows 8

    标记下部分网页不能上 速度慢设定mtu值
    iptables -A FORWARD -s 192.168.240.0/24 -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j TCPMSS
    保存service iptables save
    重启service iptables restart

      • 大黑
      • 04/29. 2015 5:18下午
      Using Google Chrome Google Chrome 42.0.2311.135 on Windows Windows 8

      额标错
      iptables -A FORWARD -s 192.168.240.0/24 -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j TCPMSS --set-mss 1356

    • killyman
    • 07/26. 2015 5:33上午
    Using Mozilla Firefox Mozilla Firefox 31.0 on Linux Linux

    为什么我按你的,无法成功呢?

return top

%d 位部落客按了赞: