检测是否支持
执行如下命令
1
|
cat /dev/net/tun
|
返回信息应该为
1
|
cat: /dev/net/tun: File descriptor in bad state
|
再执行如下命令
1
|
cat /dev/ppp
|
返回信息应该为
1
|
cat: /dev/ppp: No such device or address
|
如果返回信息不同 那么请联系你的主机商开通TUN和PPP
CentOS手动编译
安装openswan
首先来安装openswan 如果之前装过 建议先删除 删除方法如下
1
|
yum remove openswan
|
然后去openswan网站下载最新的源代码 在那之前先安装一些需要的依赖包
1
|
yum install make gcc gmp–devel bison flex
|
下载完openswan以后 解压缩openswan 进入openswan的目录 编译 安装
1
2
3
|
tar zxvf openswan–xxx.bz
cd openswan
make programs install
|
编辑ipsec.conf
然后编辑 etc/ipsec.conf 这文件对格式要求很严格 缩进问题都会导致出错 所以这个下面的这个代码仅供参考 如果直接复制出现问题 请编辑原来的配置文件 修改成这样子
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
|
version 2.0 # confirms to second version of ipsec.conf specification
config setup
nat_traversal=yes
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v6:fd00::/8,%v6:fe80::/10
oe=off
protostack=netkey
conn L2TP–PSK–NAT
rightsubnet=vhost:%priv
also=L2TP–PSK–noNAT
conn L2TP–PSK–noNAT
authby=secret
pfs=no
auto=add
keyingtries=3
rekey=no
# Apple iOS doesn’t send delete notify so we need dead peer detection
# to detect vanishing clients
dpddelay=10
dpdtimeout=10
dpdaction=clear
ikelifetime=8h
keylife=1h
type=transport
left=修改成你的服务器的IP
leftid=修改成你的服务器的IP
leftprotoport=17/1701
right=%any
rightid=%any
rightprotoport=17/%any
|
设置预共享密钥(PSK)
1
|
vim /etc/ipsec.secrets
|
上面的文件应该要自己建立,内容编辑成这样
SERVER-IP:服务器的IP地址
SharedKey:你自己设置的PSK
1
|
SERVER–IP %any: PSK “SharedKey”
|
设置完毕以后ipsec就搞定了
安装依赖(PPP)
1
|
yum install libpcap–devel ppp
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
wget http://downloads.sourceforge.net/project/rp-l2tp/rp-l2tp/0.4/rp-l2tp-0.4.tar.gz
tar zxvf rp–l2tp–0.4.tar.gz
cd rp–l2tp–0.4
./configure
make
cp handlers/l2tp–control /usr/local/sbin/
mkdir /var/run/xl2tpd/
ln –s /usr/local/sbin/l2tp–control /var/run/xl2tpd/l2tp–control
cd xl2tpd
make install
mkdir /etc/xl2tpd
vim /etc/xl2tpd/xl2tpd.conf
|
编辑成下面这个样子 设置以下你想分配给客户端的地址 以及虚拟服务器的IP
1
2
3
4
5
6
7
8
9
10
|
[lns default]
ip range = 192.168.1.128–192.168.1.254
local ip = 192.168.1.99
require chap = yes
refuse pap = yes
require authentication = yes
name = LinuxVPNserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
|
修改xl2tp配置
1
|
vim /etc/ppp/options.xl2tpd
|
编辑成这样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
require–mschap–v2
ms–dns 8.8.8.8
ms–dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide–password
modem
debug
name l2tpd
proxyarp
lcp–echo–interval 30
lcp–echo–failure 4
mtu 1400
noccp
connect–delay 5000
|
上面的noccp用来解决IOS连接问题
添加账号密码
1
|
vim /etc/ppp/chap–secrets
|
格式如下
1
|
账号 l2tp 密码 *
|
账号密码用英文和数字 *可以改为指定使用上面IP段中的IP地址
修改iptable规则
1
|
iptables –I INPUT –p udp –m multiport —dport 1701,4500,500 –j ACCEPT
|
然后给客户端设置NAT伪装 (偷懒下 直接在接口上做伪装)
1
|
iptables –t nat –A POSTROUTING –o eth0 –j MASQUERADE
|
开启IP转发
1
|
vim /etc/sysctl.conf
|
修改以下条目
1
2
3
|
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.tcp_syncookies = 0
|
添加以下条目
1
2
3
4
5
6
|
# Settings for OpenSwan IPSec implementation
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.core.xfrm_larval_drop = 1
|
运行一下命令让配置生效
1
|
sysctl –p
|
编辑xl2tp
1
|
vim /etc/init.d/xl2tpd
|
编辑成这样
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#!/bin/sh
#
# xl2tpd This shell script takes care of starting and stopping l2tpd.
#
# chkconfig: – 80 30
# description: Layer 2 Tunnelling Protocol Daemon (RFC 2661)
#
# processname: xl2tpd
# config: /etc/xl2tpd/xl2tpd.conf
# pidfile: /var/run/xl2tpd.pid
#Servicename
SERVICE=xl2tpd
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
if [ ${NETWORKING} = “no” ]
then
exit 0
fi
[ –x /usr/local/sbin/$SERVICE ] || exit 0
RETVAL=0
start() {
echo –n “Starting $SERVICE: “
if [ ! –d /var/run/xl2tpd ]
then
mkdir /var/run/xl2tpd
fi
daemon /usr/local/sbin/$SERVICE
RETVAL=$?
[ $RETVAL –eq 0 ] && touch /var/lock/subsys/$SERVICE
echo “”
return $RETVAL
}
stop() {
echo –n “Stopping $SERVICE: “
killproc $SERVICE
RETVAL=$?
echo
[ $RETVAL –eq 0 ] && rm –f /var/lock/subsys/$SERVICE
return $RETVAL
}
restart() {
stop
start
}
# See how we were called.
case “$1” in
start)
start
;;
stop)
stop
;;
status)
status $SERVICE
RETVAL=$?
;;
restart|reload)
restart
;;
condrestart)
[ –f /var/lock/subsys/$SERVICE ] && restart || :
;;
*)
echo “Usage: $SERVICE {start|stop|status|restart|reload|condrestart}”
exit 1
esac
|
给它加上执行权限
1
|
chmod +x /etc/init.d/xl2tpd
|
到这里就全部配置完成了 重启下几个服务
1
2
3
4
|
service iptables save
service iptables restart
service ipsec restart
service xl2tpd restart
|
xl2tp加入开机启动
1
|
chkconfig —add xl2tpd
|
下面是一键安装版本
一键安装方法
下载脚本
CentOS Fedora或Redhat系统
1
|
wget http://lamp.teddysun.com/files/l2tp.sh
|
Ubuntu系统
1
|
wget http://lamp.teddysun.com/files/l2tp_ubuntu.sh
|
赋予执行权限
1
|
chmod +x *.sh
|
运行脚本
CentOS Fedora或Redhat系统
1
|
./l2tp.sh
|
Ubuntu系统
1
|
./l2tp_ubuntu.sh
|
执行后会要求输入一些信息
「Please input IP-Range:」表示输入IP段 默认为10.1.2 你可以输入10.0.0等自定义IP段
「Please input PSK:」表示输入PSK预共享密钥 自己输入一个即可 默认为vpn
确认无误后再次按回车开始安装 安装完成后会显示你的服务器IP 默认添加的账户密码和预共享密钥(PSK) 修改添加账户密码方法跟上面的一样
安装完如下图说明成功 可以使用了
错误及解决方法
这里提供几个可能出现的错误及解决方法
Pluto错误
如果出现以下类似错误
1
2
|
Checking that pluto is running [FAILED]
whack: Pluto is not running (no “/var/run/pluto/pluto.ctl”)
|
按照上面修改IPSec.conf的方法 把第一行的version 2.0加上去
iptable错误
1
2
3
4
|
iptables: Setting chains to policy ACCEPT: security raw nat[FAILED]filter
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
|
这是Linode官方在iptables里加了一个security的规则链 但是centos不支持 解决方法如下
修改iptables文件
1
|
vim /etc/init.d/iptables
|
找到如下case段 在raw后面加上security)段如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
for i in $tables; do
echo –n “$i “
case “$i” in
raw)
$IPTABLES –t raw –P PREROUTING $policy \
&& $IPTABLES –t raw –P OUTPUT $policy \
|| let ret+=1
;;
security)
$IPTABLES –t filter –P INPUT $policy \
&& $IPTABLES –t filter –P OUTPUT $policy \
&& $IPTABLES –t filter –P FORWARD $policy \
|| let ret+=1
;;
filter)
$IPTABLES –t filter –P INPUT $policy \
&& $IPTABLES –t filter –P OUTPUT $policy \
&& $IPTABLES –t filter –P FORWARD $policy \
|| let ret+=1
;;
|
运行如下命令重启iptables
1
|
service iptables restart
|
看到如下所示表示成功
1
2
3
4
|
iptables: Setting chains to policy ACCEPT: security raw nat[ OK ]filter
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
|
客户端设置
配置好以后看看如何使用
这里以WIN7为例 其他系统可以自行参考设置
1、打开网络和共享中心 点击设置新的连接和网络
2、选择连接到工作区并点击下一步
3、点击创建新连接并下一步
4、选择使用我的Internet连接
5、Internet地址填入你的服务器IP地址 目标名称随意 用来自己识别 填完后点击下一步
6、填入你设置的用户名和密码 勾选记住此密码 如果你想每次连接都要求输入密码可以不勾选 并点击创建 然后关闭
7、右键点击你刚才创建的链接 点击属性
8、切换到安全选项卡设置VPN类型为IPSec
9、点击高级设置 选择使用预共享密钥作身份验证 并输入你刚才设置的预共享密钥(PSK) 一路确定设置完成 可以连接使用了