ContOS搭建PPTP服务器

环境ContOS 6

1. 更新yum

1
yum -y update

2. 判断ppp是否可用

1
2
cat /dev/ppp
# cat: /dev/ppp: No such device or address

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

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

3. 安裝 ppp 与 iptables

1
2
yum -y install ppp iptables
cp -rpf /etc/sysconfig/iptables /etc/sysconfig/iptables.pptpsave

4. 安装 pptp

1
2
3
4
5
6
7
8
9
# 32位
cd /usr/src
wget http://poptop.sourceforge.net/yum/stable/rhel6/i386/pptpd-1.4.0-1.el6.i686.rpm
rpm -ivh pptpd-1.4.0-1.el6.i686.rpm --force --nodeps

# 64位
cd /usr/src
wget http://poptop.sourceforge.net/yum/stable/rhel6/x86_64/pptpd-1.4.0-1.el6.x86_64.rpm # 64
rpm -ivh pptpd-1.4.0-1.el6.x86_64.rpm --force --nodeps

6. 允许数据包转发

1
2
3
4
5
vim /etc/sysctl.conf  

# 设置:
# Controls IP packet forwarding
net.ipv4.ip_forward = 1

7. 执行下面命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sysctl -p # 调整内核参数

# 追加pptp配置
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.240 与 eth0 改成符合你需求的设定)。

8. 加入 VPN 账号

1
2
3
vi /etc/ppp/chap-secrets

# 帐号名称 pptpd 密码 *

错误处理

807 错误

在 VPN 拨号时,如果出现807错误(计算机与VPN服务器之间的网络连接被中断),或是其它在验证登陆信息之前出现的错误。
如果确定不是自己电脑的问题,你可以编辑 Server 的 /etc/sysconfig/iptables,查看是否有这两条配置:

1
2
-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:

1
service iptables restart

734 错误

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

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

附:
一键安装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash

function installVPN(){
trueecho "begin to install VPN services";
true#check wether vps suppot ppp and tun
true
trueyum remove -y pptpd ppp
trueiptables --flush POSTROUTING --table nat
trueiptables --flush FORWARD
truerm -rf /etc/pptpd.conf
truerm -rf /etc/ppp
true
truearch=`uname -m`
true
truewget http://www.hi-vps.com/downloads/dkms-2.0.17.5-1.noarch.rpm
truewget http://wty.name/linux/sources/kernel_ppp_mppe-1.0.2-3dkms.noarch.rpm
truewget http://www.hi-vps.com/downloads/kernel_ppp_mppe-1.0.2-3dkms.noarch.rpm
truewget http://www.hi-vps.com/downloads/pptpd-1.3.4-2.el6.$arch.rpm
truewget http://www.hi-vps.com/downloads/ppp-2.4.5-17.0.rhel6.$arch.rpm


trueyum -y install make libpcap iptables gcc-c++ logrotate tar cpio perl pam tcp_wrappers
truerpm -ivh dkms-2.0.17.5-1.noarch.rpm
truerpm -ivh kernel_ppp_mppe-1.0.2-3dkms.noarch.rpm
truerpm -qa kernel_ppp_mppe
truerpm -Uvh ppp-2.4.5-17.0.rhel6.$arch.rpm
truerpm -ivh pptpd-1.3.4-2.el6.$arch.rpm

truemknod /dev/ppp c 108 0
trueecho 1 > /proc/sys/net/ipv4/ip_forward
trueecho "mknod /dev/ppp c 108 0" >> /etc/rc.local
trueecho "echo 1 > /proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
trueecho "localip 172.16.36.1" >> /etc/pptpd.conf
trueecho "remoteip 172.16.36.2-254" >> /etc/pptpd.conf
trueecho "ms-dns 8.8.8.8" >> /etc/ppp/options.pptpd
trueecho "ms-dns 8.8.4.4" >> /etc/ppp/options.pptpd

truepass=`openssl rand 6 -base64`
trueif [ "$1" != "" ]
truethen pass=$1
truefi

trueecho "vpn pptpd ${pass} *" >> /etc/ppp/chap-secrets

trueiptables -t nat -A POSTROUTING -s 172.16.36.0/24 -j SNAT --to-source `ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk 'NR==1 { print $1}'`
trueiptables -A FORWARD -p tcp --syn -s 172.16.36.0/24 -j TCPMSS --set-mss 1356
trueservice iptables save

truechkconfig iptables on
truechkconfig pptpd on

trueservice iptables start
trueservice pptpd start

trueecho "VPN service is installed, your VPN username is vpn, VPN password is ${pass}"
true
}

function repaireVPN(){
trueecho "begin to repaire VPN";
truemknod /dev/ppp c 108 0
trueservice iptables restart
trueservice pptpd start
}

function addVPNuser(){
trueecho "input user name:"
trueread username
trueecho "input password:"
trueread userpassword
trueecho "${username} pptpd ${userpassword} *" >> /etc/ppp/chap-secrets
trueservice iptables restart
trueservice pptpd start
}

echo "which do you want to?input the number."
echo "1. install VPN service"
echo "2. repaire VPN service"
echo "3. add VPN user"
read num

case "$num" in
[1] ) (installVPN);;
[2] ) (repaireVPN);;
[3] ) (addVPNuser);;
*) echo "nothing,exit";;
esac