在Ubuntu上部署L2TP服务
现在,lesca向您提供OpenVPN、L2TP和PPTP的快速部署工具OVM,请读者转至《OVM:在Ubuntu上快速部署OpenVPN, L2TP, PPTP服务》。如果想手动安装L2TP,请继续阅读本文:
一、简介
目前流行的L2TP服务都是通过IPSec加密的,正式的叫法应该是L2TP over IPSec。因此,要获得通用性较强的L2TP服务,就需要有IPSec支持。
IPSec提供加密、验证服务,客户端连接服务器后首先请求IPSec验证,这通常是用PSK进行验证,验证通过后,交由L2TP处理。
L2TP负责对数据进行封包,并启动pppd建立通讯链路。pppd负责用户名、密码的验证。
以上就是建立一个L2TP连接的大致过程,但是其中会用到多种相关的协议或者端口:
- 协议:UDP,端口500,IKE(Internet Key Exchange) – 解决对称密钥交换问题
- 协议:UDP,端口4500,IPsec NAT-Traversal
- 协议:UDP,端口1701,L2TP监听
- 协议:ESP,协议号50,Encapsulated Security Payload
- 协议:AH,协议号51,Authentication Header
ESP和AH协议用于提供验证、数据完整性以及加密通信等特性。它们可以加密整个数据包,也可以仅加密上层协议头,这取决于IPSec所启用的模式。
IPSec工作在隧道模式(tunnel)时,加密整个数据报;传输模式(transport)时,仅加密IP数据报的头部。
我们配置的IPSec仅向L2TP提供支持,因此工作在传输模式。
ESP和AH协议不使用端口号,因此使用NAT的设备必须支持NAT穿透,它将ESP和AH封装在UDP中进行传输。
二、安装软件包
现在流行的ipsec来自openswan,l2tp来自xl2tpd。本文将以这两个软件包进行讲解如何配置。当提及openswan的时候就是指ipsec,当说到xl2tpd的时候就是指l2tp。希望读者能够明白。
1.搭建环境
- Ubuntu 10.04
- Kernel 2.6.32
- Openswan 2.6.37
- xl2tpd-1.2.5
/!\重要:笔者一开始在Ubuntu 11.04上进行实验,Kernel 3.0,IPSec版本2.6.23。这里有几个严重问题:
- openswan尚未对3.0内核提供良好支持,某些情况下用iPhone或者win7客户端连接会导致内核奔溃!
- 2.6.23版本的openswan尚有诸多BUG,即使在Kernel 2.6的环境下,也无法保证100%连接成功。
因此,请至少使用openswan 2.6.37的版本,当前最新版本是2.6.38。源代码可在这里下载:http://download.openswan.org/openswan/,之后需要通过编译完成安装。对于openswan的安装我们将会提供两种不同的方法。
2.通过添加软件库安装openswan
apt-get install python-software-properties add-apt-repository ppa:openswan/ppa apt-get update apt-get install openswan
如果出现Do you want to create a RSA public/private keypair for this host?。我们选择No,这种模式的IPSec通过证书进行验证,而非PSK。
3.通过编译源代码包安装openswan
获取最新软件包:
wget http://download.openswan.org/openswan/openswan-2.6.38.tar.gz tar -xzf openswan-2.6.38.tar.gz
编译不同的版本可能会依赖不同的库文件,因此如果你正在编译更高版本的openswan,请务必先阅读README文件。这里我们安装如下库文件:
apt-get install libgmp3-dev flex bison
安装这些库会占用大量空间,而且在服务器上编译二进制文件通常不是明智之举!但是有时候服务器架构会与你的测试机完全不同,因此可能你仍然需要在服务器上做这些工作。接下来我们开始编译:
make programs install
如果一切顺利,编译工作完成后配置文件及服务会自动安装到合适的地方,也就是和通过apt-get安装没什么两样了。
4.安装xl2tpd
Ubuntu中xl2tpd的版本目前没有发现严重问题。因此安装起来也比较方便:
apt-get install xl2tpd
三、开始配置
配置工作分为三部分:IPSec配置、L2TP配置、PPP配置
1.IPSec配置
# /etc/ipsec.conf
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
# we cannot rekey for %any, let client rekey
rekey=no
# Apple iOS doesn't send delete notify so we need dead peer detection
# to detect vanishing clients
dpddelay=10
dpdtimeout=90
dpdaction=clear
# Set ikelifetime and keylife to same defaults windows has
ikelifetime=8h
keylife=1h
# l2tp-over-ipsec is transport mode
type=transport
#
left=$IP
#
# For updated Windows 2000/XP clients,
# to support old clients as well, use leftprotoport=17/%any
leftprotoport=17/1701
#
# The remote user.
#
right=%any
# Using the magic port of "%any" means "any one single port". This is
# a work around required for Apple OSX clients that use a randomly
# high port.
rightprotoport=17/%any
conn passthrough-for-non-l2tp
type=passthrough
left=$IP
leftnexthop=%defaultroute
right=0.0.0.0
rightsubnet=0.0.0.0/0
auto=route
其中,$IP改成你主机以太网卡的IP,%defaultroute是魔法参数,指默认网关,下同。对于下面的密钥文件,你需要将密钥引在引号中。
# /etc/ipsec.secrets $IP %any: PSK "Your Preshared Key Here"
最后我们需要修改某些网络策略,让ipsec正常运行:
for each in /proc/sys/net/ipv4/conf/*
do
echo 0 > $each/accept_redirects
echo 0 > $each/send_redirects
done
2.L2TP配置
# /etc/xl2tpd/xl2tpd.conf [global] ipsec saref = yes [lns default] ip range = 10.10.20.100-10.10.20.254 local ip = 10.10.20.1 require chap = yes refuse pap = yes require authentication = yes ppp debug = yes pppoptfile = /etc/ppp/options.xl2tpd length bit = yes
3.PPP配置
# /etc/ppp/options.xl2tpd refuse-mschap-v2 refuse-mschap ms-dns 8.8.8.8 ms-dns 8.8.4.4 asyncmap 0 auth lock hide-password local #debug name l2tpd proxyarp lcp-echo-interval 30 lcp-echo-failure 4 mtu 1404 mru 1404
5.启用转发
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
对于OpenVZ的主机,可能不支持MASQUERADE,此时需要使用SNAT:
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source $IP
其中$IP就是你主机的eth0接口的IP地址。
四、检查ipsec是否工作正常
我们通过执行ipsec verify命令进行验证ipsec服务是否工作正常:
错误1:pluto is running [FAILED]
Checking your system to see if IPsec got installed and started correctly: Version check and ipsec on-path [OK] Linux Openswan U2.6.38/K(no kernel code presently loaded) Checking for IPsec support in kernel [FAILED] SAref kernel support [N/A] Checking that pluto is running [FAILED] whack: Pluto is not running (no "/var/run/pluto/pluto.ctl") Checking for 'ip' command [OK] Checking /bin/sh is not /bin/dash [WARNING] Checking for 'iptables' command [OK] Opportunistic Encryption Support [DISABLED]
这是因为你还没有启动ipsec服务!
/etc/init.d/ipsec start
错误2:NETKEY: Testing XFRM related proc values [FAILED]
Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path [OK]
Linux Openswan U2.6.38/K2.6.32-38-generic-pae (netkey)
Checking for IPsec support in kernel [OK]
SAref kernel support [N/A]
NETKEY: Testing XFRM related proc values [FAILED]
Please disable /proc/sys/net/ipv4/conf/*/send_redirects
or NETKEY will cause the sending of bogus ICMP redirects!
[FAILED]
Please disable /proc/sys/net/ipv4/conf/*/accept_redirects
or NETKEY will accept bogus ICMP redirects!
[OK]
Checking that pluto is running [OK]
Pluto listening for IKE on udp 500 [OK]
Pluto listening for NAT-T on udp 4500 [OK]
Checking for 'ip' command [OK]
Checking /bin/sh is not /bin/dash [WARNING]
Checking for 'iptables' command [OK]
Opportunistic Encryption Support [DISABLED]
你可能忘记修改网络策略:
for each in /proc/sys/net/ipv4/conf/*
do
echo 0 > $each/accept_redirects
echo 0 > $each/send_redirects
done
错误3:Pluto listening for IKE on udp 500 [FAILED]
Pluto listening for IKE on udp 500 [FAILED] Cannot execute command "lsof -i UDP:500": No such file or directory Pluto listening for NAT-T on udp 4500 [FAILED] Cannot execute command "lsof -i UDP:4500": No such file or directory
那么你需要安装lsof:
apt-get install lsof
五、测试与调试
1.启用测试用户
编辑/etc/ppp/chap-secrets文件,加入如下行:
# Secrets for authentication using CHAP # client server secret IP addresses guest l2tpd password *
该行创建一个用户,用户名为guest,密码为password(明文)
server字段与options.xl2tpd配置文件中的name属性对应。还可以星号(*)代替,表示不限调用服务。
IP address表示来源IP,这里星号(*)表示接受所有连接。
对来源IP的过滤应该由iptables防火墙来完成。读者可以查阅本博客关于iptables的相关文章进行设置。
2.连接
现在就可以尝试连接,如果连接失败,需要通过查看日志信息排错。
3.查看日志
ipsec日志记录在/var/log/auth中,如果发现该行:
STATE_MAIN_R3: sent MR3, ISAKMP SA established
则表示IPSec认证或连接没有问题,可以检查其他日志。但是有时候即使出现established,也会在稍后的行中出现错误信息,这是需要用户留意的地方。
xl2tpd和pppd的日志记录在/var/log/syslog中。
References:
[1] The official IPsec Howto for Linux
[2] Configure L2TP/IPSec VPN on Ubuntu
[3] Secure IPsec/L2TP VPN for on the road android devices
[4] Setting Up an IPSec L2TP VPN server on Ubuntu for Windows clients
[5] Creating VPNs with IPsec and SSL/TLS
[6] ipsec安装好后运行ipsec verify命令发现错误的解决方案
[7] 在Ubuntu上安装L2TP/IPsec VPN服务器
[8] VPN相关端口和协议
[9] IPsec HOWTO – Theory
版权声明
本文出自 Lesca 技术宅,转载时请注明出处及相应链接。
本文永久链接: https://lesca.me/archives/how-to-setup-l2tp-over-ipsec-on-ubuntu.html


Qoo記
2013-02-24 at 21:02
我在12.10跟著你的教程
请问make programs install的直接在terminal输入?
顺著做出现了make: *** no rule to make target ‘programs’ . Stop
mark
2014-07-09 at 12:37
我在AWS上创建了一个,android手机可以连接了,但是win7和ipad都无法连接,也不知道什么问题。