您的位置:首页 > 编程语言 > PHP开发

在Ubuntu上部署L2TP服务

2015-04-24 16:49 344 查看
现在流行的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=$GATEWAY
right=0.0.0.0
rightsubnet=0.0.0.0/0
auto=route


其中,$IP改成你主机以太网卡的IP,$GATEWAY改成该IP所在网段的网关地址,下同。对于下面的密钥文件,你需要将密钥引在引号中。
# /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中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu linode l2tp