ILD

使用openswan+xl2tpd搭建L2TP/IPSec VPN服务器
作者:Herbert Yuan 邮箱:yuanjp89@163.com
发布时间:2017-8-5 站点:Inside Linux Development

L2TP/IPSec VPN的兼容比较好,在Windows/Android/IOS上面都能支持。本文使用OpenSwan和xl2tpd在Ubuntu上搭建L2TP/IPSec VPN。IPSec认证方式为PSK,PPP认证方式为chap2,这种认证方式兼容性也比较好。


1 安装OpenSwan和xl2tpd

Ubuntu官方的IPSec包是strongSwan。OpenSwan需要自己编译安装。如果已经安装strongSwan,先卸载。

1
2
apt-get remove strongswan
apt autoremove

安装OpenSwan

1
2
3
4
5
wget  
tar xf openswan-latest.tar.gz -C ../src
cd ../src/openswan-2.6.50/
make programs
make install

安装xl2tpd

1
apt-get install xl2tpd

还需要安装ppp包,但是通常已经安装好了。


2 IPsec配置

需要配置3个文件。


/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
# /etc/ipsec.conf
version 2.0 # conforms to second version of ipsec.conf specification
 
config setup
        nat_traversal=yes
        virtual_private=%v4:10.0.0.0/8,%v4:25.0.0.0/8,%v4:!10.254.253.0/24
        protostack=netkey
        #protostack=mast  # used for SAref + MAST only
        interfaces="%defaultroute"
        oe=off
        #plutodebug=all
        #plutostderrlog=/var/log/openswan.log
 
conn l2tp-psk
        authby=secret
        pfs=no
        auto=add
        rekey=no
        # overlapip=yes   # for SAref + MAST
        # sareftrack=yes  # for SAref + MAST
        type=transport
        left=172.31.129.70
        leftprotoport=17/1701
        #
        # The remote user.
        #
        right=%any
        rightprotoport=17/%any
        rightsubnet=vhost:%priv,%no

上述配置中,需要将left的ip为wan口ip。


/etc/ipsec.secrets配置:

1
2
3
4
5
6
# /etc/ipsec.secrets
# This file holds shared secrets or RSA private keys for authentication.
 
# RSA private key for this host, authenticating it to any other host
# which knows the public part.
172.31.129.70 %any  0.0.0.0 : PSK '123456'

特别需要指出的是,PSK值123456必须使用单引号括起来。否则协商会失败。


/etc/sysctl.conf添加下述配置

1
2
3
4
5
6
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1

添加后,使用 sysctl -p更新。


可以使用ipsec verify命令进行检查,最后重启ipsec。

1
/etc/init.d/ipsec restart


3 l2tp配置

同样需要配置3个文件。


xl2tpd配置文件:/etc/xl2tpd/xl2tpd.conf

1
2
3
4
5
6
7
8
9
10
11
[lns default]
ip range = 10.0.0.100-10.0.0.109
local ip = 10.0.0.1
assign ip = yes
require chap = yes
refuse pap = yes
require authentication = yes
name = vpn 
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes


ppp配置文件:/etc/ppp/options.xl2tpd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ipcp-accept-local
ipcp-accept-remote
ms-dns  8.8.8.8
noccp
auth
crtscts
idle 1800
mtu 1200
mru 1200
nodefaultroute
debug
lock
proxyarp
connect-delay 5000
require-mschap-v2

require-mschap-v2这个选项,表示使用chap2身份验证,在windows拨号时,要选择对应的验证。


用户配置文件:/etc/ppp/chap-secrets 

1
2
3
# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
user * admin *


4 iptables配置

配置NAT转发

1
 iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE


5 Windows拨号

使用的是Windows10系统,拨号发现Windows不会先进行IPSec协商,需要删除下述注册表:

1
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Rasman\Parameters\ProhibitIpSec

如果服务器或者客户端在NAT后面,需要添加一条注册表项

1
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PolicyAgent

添加DWORD值 AssumeUDPEncapsulationContextOnSendRule,值为2。重启电脑。


新建VPN连接。

身份验证选项MS-CHAP v2,因为之前的PPP配置中设置的是chapv2。


在高级设置中,选择PSK。

进行连接,输入PPP用户名密码。正常应该连接成功。但是整个配置还是非常复杂的话,连接失败可进行抓包和开启服务器openswan等的log来进行排查。


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.