L2TP/IPSec一键安装脚本

技术 秋水逸冰 2543801浏览 1097评论

Great Wall

本脚本适用环境:
系统支持:CentOS6+,Debian7+,Ubuntu12+
内存要求:≥128M
更新日期:2017 年 05 月 28 日

关于本脚本:
名词解释如下
L2TP(Layer 2 Tunneling Protocol)
IPSec(Internet Protocol Security)
IKEv2 (Internet Key Exchange v2)
能实现 IPsec 的目前总体上有 openswan,libreswan,strongswan 这3种。
libreswan 是基于 openswan 的 fork,所以现在各个发行版基本已经看不到 openswan 的身影了。
当然也有使用 strongswan 的。

之所以要更新 L2TP 一键安装脚本,是因为随着各个 Linux 发行版不断推陈出新,原有的脚本已经不适应现在的需求。
本脚本通过编译安装最新版 libreswan 来实现 IPSec(CentOS7 下则是全部 yum 安装),yum 或 apt-get 来安装 xl2tpd,再根据各个发行版的使用方法不同,部署防火墙规则。

隆重介绍 Docker 版 L2TP/IPsec

由于此脚本已经很久不更新,因此已经不推荐使用该脚本来安装了。OK,下面隆重介绍一下 Docker 版 L2TP/IPsec。
使用 Docker 的好处就是对当前系统的环境没有破坏性,随时可以新建或者删除容器,而且方便快捷,实在是居家必备。
具体请查看《介绍几款 Docker 镜像》此文中的【L2TP/IPsec VPN Server Docker Image】小节。
以及该项目的 Docker Hub 页面,或 Github 页面

写在前面:

基于 OpenVZ 虚拟化技术的 VPS 需要开启TUN/TAP才能正常使用,购买 VPS 时请先咨询服务商是否支持开启 TUN/TAP。

OpenVZ 虚拟的 VPS 需要系统内核支持 IPSec 才行。也就是说,母服务器的内核如果不支持的话那就没办法,只能换 VPS。
因此,一般不建议在 OpenVZ 的 VPS 上安装本脚本。脚本如果检测到该 VPS 为 OpenVZ 架构,会出现警告提醒。

如何检测是否支持TUN模块?
执行命令:
cat /dev/net/tun
如果返回信息为:cat: /dev/net/tun: File descriptor in bad state 说明正常

如何检测是否支持ppp模块?
执行命令:
cat /dev/ppp
如果返回信息为:cat: /dev/ppp: No such device or address 说明正常
当然,脚本在安装时也会执行检查,如果不适用于安装,脚本会予以提示。

使用方法:

root 用户登录后,运行以下命令:

wget --no-check-certificate https://raw.githubusercontent.com/teddysun/across/master/l2tp.sh
chmod +x l2tp.sh
./l2tp.sh

执行后,会有如下交互界面

L2TP

Please input IP-Range:
(Default Range: 192.168.18):
输入本地IP段范围(本地电脑连接到VPS后给分配的一个本地IP地址),直接回车意味着输入默认值192.168.18

Please input PSK:
(Default PSK: teddysun.com):
PSK意为预共享密钥,即指定一个密钥将来在连接时需要用到,直接回车意味着输入默认值teddysun.com

Please input Username:
(Default Username: teddysun):
Username意为用户名,即第一个默认用户。直接回车意味着输入默认值teddysun

Please input teddysun’s password:
(Default Password: Q4SKhu2EXQ):
输入用户的密码,默认会随机生成一个10位包含大小写字母和数字的密码,当然你也可以指定密码。

ServerIP:your_server_main_IP
显示你的 VPS 的主 IP(如果是多 IP 的 VPS 也只显示一个)

Server Local IP:192.168.18.1
显示你的 VPS 的本地 IP(默认即可)

Client Remote IP Range:192.168.18.2-192.168.18.254
显示 IP 段范围

PSK:teddysun.com
显示 PSK

Press any key to start…or Press Ctrl+c to cancel
按下任意按键继续,如果想取消安装,请按Ctrl+c键

安装完成后,脚本会执行 ipsec verify 命令并提示如下:

If there are no [FAILED] above, then you can connect to your
L2TP VPN Server with the default Username/Password is below:

ServerIP:your_server_IP
PSK:your PSK
Username:your usename
Password:your password

If you want to modify user settings, please use command(s):
l2tp -a (Add a user)
l2tp -d (Delete a user)
l2tp -l (List all users)
l2tp -m (Modify a user password)
Welcome to visit https://teddysun.com/448.html
Enjoy it!

如果你要想对用户进行操作,可以使用如下命令:
l2tp -a 新增用户
l2tp -d 删除用户
l2tp -m 修改现有的用户的密码
l2tp -l 列出所有用户名和密码
l2tp -h 列出帮助信息

其他事项:

1、脚本在安装完成后,已自动启动进程,并加入了开机自启动。
2、脚本会改写 iptables 或 firewalld 的规则。
3、脚本安装时,会即时将安装日志写到 /root/l2tp.log 文件里,如果你安装失败,可以通过此文件来寻找错误信息。

使用命令:

ipsec status (查看 IPSec 运行状态)
ipsec verify (查看 IPSec 检查结果)
/etc/init.d/ipsec start|stop|restart|status (CentOS6 下使用)
/etc/init.d/xl2tpd start|stop|restart (CentOS6 下使用)
systemctl start|stop|restart|status ipsec (CentOS7 下使用)
systemctl start|stop|restart xl2tpd (CentOS7 下使用)
service ipsec start|stop|restart|status (Debian/Ubuntu 下使用)
service xl2tpd start|stop|restart (Debian/Ubuntu 下使用)

更新日志

2017 年 05 月 28 日:
升级 libreswan 到版本 3.20。
修正 libreswan 的若干配置问题。
修正 xl2tpd 的端口监听配置问题。
修正在 CentOS 6 对 libevent2 的依赖问题,改为 yum 安装 libevent2-devel。
测试表明,在内网环境的 VPS 里(如AWS, IDCF,GCE,腾讯云,阿里云等)也可以正常使用了。

2017 年 02 月 25 日:
升级 libreswan 到版本 3.19。

2016 年 09 月 12 日:
修正了在 CentOS 6 下 libevent2 依赖的问题;
新增了一个 -m 选项,用以修改现有用户的密码。

2016 年 08 月 13 日:
修正 Debian 8 下的 sd-daemon.h: No such file or directory 问题,是由于缺少依赖包 libsystemd-daemon-dev 导致的。

2016 年 08 月 05 日:
升级 libreswan 到版本 3.18。

2016 年 06 月 10 日:
脚本在安装完成后,新增了几个命令,便于操作用户
l2tp -a 新增用户
l2tp -d 删除用户
l2tp -l 列出所有用户
l2tp -h 列出帮助信息

2016 年 04 月 25 日:
4、在 Vultr 的 Debian 7的系统模板下安装时,软件包 libcurl4-nss-dev 会出现依赖错误。如下所示:

The following packages have unmet dependencies:
libcurl4-nss-dev : Depends: libldap2-dev but it is not going to be installed
Depends: librtmp-dev but it is not going to be installed

而 libldap2-dev 和 librtmp-dev 又依赖了其他几种软件包。总之最后的依赖关系如下:

libldap2-dev : Depends: libldap-2.4-2 (= 2.4.31-2+deb7u1) but 2.4.31+really2.4.40+dfsg-1+deb8u1~bpo70+1 is to be installed
librtmp-dev : Depends: libgnutls-dev but it is not going to be installed
libgnutls-dev : Depends: libp11-kit-dev (>= 0.4) but it is not going to be installed
libp11-kit-dev : Depends: libp11-kit0 (= 0.12-3) but 0.20.7-1~bpo70+1 is to be installed

那么解决办法就是把最底层的依赖包 libp11-kit0 先卸载掉,然后再安装 libcurl4-nss-dev 即可。

apt-get -y remove libp11-kit0
apt-get -y --no-install-recommends install libcurl4-nss-dev wget

然后再运行脚本安装即可。

2016 年 04 月 22 日:
修复了在 Ubuntu 16.04 下因为默认缺少 python 命令而导致 ipsec verify 等命令不能用的问题。

2016 年 04 月 19 日:
修复了在 Debian 7 下因为 libnss3 和 libnspr4 的版本过低而导致编译 libreswan 失败的问题。

2016 年 04 月 18 日:
目前在 Debian 7 上测试的结果,因为 libnss3 和 libnspr4 的版本过低而导致编译 libreswan 失败。临时解决办法是 dpkg 安装 libnss3_3.17.2 和 libnspr4_4.10.7 的 deb 包后重试。

参考链接:
https://libreswan.org/wiki/3.14_on_Debian_Wheezy
https://github.com/libreswan/libreswan

转载请注明:秋水逸冰 » L2TP/IPSec一键安装脚本

发表我的评论
取消评论

请输入正确答案后提交评论 *超出时限。 请再次填写验证码。

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1097)

  1. 嘤嘤嘤不支持ipv6
    Tizen Ye8年前(2016-08-13)回复
    • 实际上通过改配置文件是能支持 IPv6 的,可是我没有测试环境。 为确保可用性,就没配置 IPv6
      秋水逸冰8年前(2016-08-14)回复
  2. 成功!好激动,特来感谢博主
    大猩猩8年前(2016-08-13)回复
  3. 老大,碰到个很奇怪的问题,linode的vps,用了两个版本的系统都安装成功,一个是centos6.5一个是centos7,但是iphone的ios在centos6.5上可以连接上,centos7连接不上。请问怎么解决这个问题??急,谢谢。
    yujam8年前(2016-08-13)回复
    • CentOS 7 上的也许是 firewalld 没有执行到位。 firewall-cmd --list-all 执行这个看看,有没有 xl2tpd ? 如果没有的话,执行: firewall-cmd --permanent --add-service=xl2tpd firewall-cmd --reload
      秋水逸冰8年前(2016-08-14)回复
      • public (default) interfaces: sources: services: dhcpv6-client ipsec ssh xl2tpd ports: masquerade: yes forward-ports: icmp-blocks: rich rules: 这样算有xl2tpd吗??
        yujam8年前(2016-08-14)回复
        • 老大,按照你的方法试了,还是连接不上,提示:L2TP-VPN服务器未响应。请重试连接。如果问题继续存在,请验证您的设置并联系管理员。 然后我又ipsec verify: Verifying installed system and configuration files Version check and ipsec on-path [OK] Libreswan 3.15 (netkey) on 4.6.3-x86_64-linode70 Checking for IPsec support in kernel [OK] NETKEY: Testing XFRM related proc values ICMP default/send_redirects [OK] ICMP default/accept_redirects [OK] XFRM larval drop [OK] Pluto ipsec.conf syntax [OK] Hardware random device [N/A] Two or more interfaces found, checking IP forwarding [OK] Checking rp_filter [OK] Checking that pluto is running [OK] Pluto listening for IKE on udp 500 [OK] Pluto listening for IKE/NAT-T on udp 4500 [OK] Pluto ipsec.secret syntax [OK] Checking 'ip' command [OK] Checking 'iptables' command [OK] Checking 'prelink' command does not interfere with FIPSChecking for obsolete ipsec.conf options [OK] Opportunistic Encryption [DISABLED] 一切都是好的,就是连接不上。请老大再帮我看看
          yujam8年前(2016-08-14)回复
          • 那只能再看看进程有没有启动了。 ps -ef | grep -v grep | grep xl2tpd ps -ef | grep -v grep | grep ipsec 看看这 2 个进程是否都存在。
            秋水逸冰8年前(2016-08-14)回复
        • 那说明防火墙 firewalld 设置是 OK 的。
          秋水逸冰8年前(2016-08-14)回复
  4. 麻烦大神帮我看看是怎么回事,xl2tpd启动失败,日志地址:http://115.159.209.78:88/res/l2tp.log 我是在局域网搭建
    小怪兽8年前(2016-08-12)回复
    • xl2tpd 是 yum 安装的,并非编译安装的。 xl2tpd 启动失败的情况我也遇到过,是系统的问题。
      秋水逸冰8年前(2016-08-13)回复
  5. breswan-3.18/ports/linux/include -I/root/l2tp/libreswan-3.18/ports/linux/include \ -MMD -MF ./alg_info.d \ -o ./alg_info.o \ -c /root/l2tp/libreswan-3.18/lib/libswan/alg_info.c In file included from /root/l2tp/libreswan-3.18/lib/libswan/alg_info.c:34:0: /root/l2tp/libreswan-3.18/include/constants.h:107:45: fatal error: prcpucfg.h: No such file or directory compilation terminated. make[3]: *** [alg_info.o] Error 1 make[3]: Leaving directory `/root/l2tp/libreswan-3.18/OBJ.linux.x86_64/lib/libswan' make[2]: *** [local-base] Error 2 make[2]: Leaving directory `/root/l2tp/libreswan-3.18/lib/libswan' make[1]: *** [recursive-all] Error 2 make[1]: Leaving directory `/root/l2tp/libreswan-3.18/lib' make: *** [recursive-all] Error 2 libreswan-3.18 install failed. 8月5号的版本. OS:Debian 7 x64 Server:vultr 非常感谢博主
    jessezhang8年前(2016-08-11)回复
    • 换 Debian 8 即可。 Debian 7 下 libnss3 和 libnspr4 版本过低,需手动安装 deb 包。有时候会不成功。
      秋水逸冰8年前(2016-08-12)回复
      • 我是debian8 之前debian7安装一次性成功。 后来换debian8, libnss版本也达到要求 但libreswan-3.18 install failed. compilation terminated. ../../../mk/depend.mk:28: recipe for target 'plutomain.o' failed make[3]: *** [plutomain.o] Error 1 make[3]: Leaving directory '/home/mxy/l2tp/libreswan-3.18/OBJ.linux.i386/programs/pluto' Makefile:409: recipe for target 'local-base' failed make[2]: *** [local-base] Error 2 make[2]: Leaving directory '/home/mxy/l2tp/libreswan-3.18/programs/pluto' ../mk/targets.mk:82: recipe for target 'recursive-all' failed make[1]: *** [recursive-all] Error 2 make[1]: Leaving directory '/home/mxy/l2tp/libreswan-3.18/programs' mk/targets.mk:82: recipe for target 'recursive-all' failed make: *** [recursive-all] Error 2 libreswan-3.18 install failed.
        红黑红红黑8年前(2016-08-13)回复
        • Debian 8下该问题已经修复。缺少依赖包 libsystemd-daemon-dev 造成的问题。 请下载最新代码重新安装。
          秋水逸冰8年前(2016-08-13)回复
          • Ubuntu16.04 同樣情況發生, In file included from /root/l2tp/libreswan-3.19/include/lswlog.h:40:0, from /root/l2tp/libreswan-3.19/lib/libswan/alg_info.c:23: /root/l2tp/libreswan-3.19/include/constants.h:107:45: fatal error: prcpucfg.h: No such file or directory #include /* from nspr4 devel */ ^ compilation terminated. ../../../mk/depend.mk:28: recipe for target 'alg_info.o' failed make[3]: *** [alg_info.o] Error 1 make[3]: Leaving directory '/root/l2tp/libreswan-3.19/OBJ.linux.x86_64/lib/libswan' Makefile:98: recipe for target 'local-base' failed make[2]: *** [local-base] Error 2 make[2]: Leaving directory '/root/l2tp/libreswan-3.19/lib/libswan' ../mk/targets.mk:82: recipe for target 'recursive-all' failed make[1]: *** [recursive-all] Error 2 make[1]: Leaving directory '/root/l2tp/libreswan-3.19/lib' mk/targets.mk:82: recipe for target 'recursive-all' failed make: *** [recursive-all] Error 2 libreswan-3.19 install failed.
            阿莫7年前(2017-04-20)回复
            • 打錯版本,是16.10
              阿莫7年前(2017-04-20)回复
  6. 以前一键角本成功,现在不行了。
    kala8年前(2016-08-11)回复
    • 这个 L2TP 的安装对 VPS 环境比较挑剔,有的能安装成功,有的就是不行。 发你的安装日志 /root/l2tp.log 到我邮箱的话,我可以帮你分析一下原因。
      秋水逸冰8年前(2016-08-12)回复
      • 谢谢回复。 以前安装的是pptp,但苹果手机从ios10之后取消对pptp的支持,而我这个vpn只是给用苹果手机的朋友,所以就把pptp直接删除了。安卓或是pc机上直接用ss更安心
        kala8年前(2016-08-12)回复
  7. 感谢如此好用的脚本~有个小建议 因为android 6.0的手机加密模式都是AES_256-HMAC_SHA2_256 所以脚本中升级后的手机都有可能连接不上。。 就在/etc/ipsec.conf 修改以下两行配置,在行尾加入“aes256-sha2_256”,以此增加对sha2的支持 ike=3des-sha1,aes-sha1,aes256-sha1,aes256-sha2_256 phase2alg=3des-sha1,aes-sha1,aes256-sha1,aes256-sha2_256 新增一行配置 sha2-truncbug=yes
    游客8年前(2016-08-10)回复
  8. 为什么没有写要安装什么啊?我ipsec都没安装也没有提醒啊,xl2tpd也没有安装,怎么不说呢
    Nozz8年前(2016-08-08)回复
  9. 赞!装上就能用了,非常感谢!
    Mike8年前(2016-08-08)回复
  10. 更新后能用了,太感谢
    火火8年前(2016-08-07)回复
  11. 我在遨游的vps上安装失败,日志见:http://132.pw/l2tp.log 麻烦有空的时候帮忙看下,谢谢。
    scse8年前(2016-08-05)回复
  12. 如何卸载?安装失败
    jerry8年前(2016-08-04)回复
    • 暂不支持卸载。 安装失败也不会影响其他程序。
      秋水逸冰8年前(2016-08-04)回复
  13. 您好,我使用您的一键包安装了l2tp,现在是手机和电脑可以连上,但上不了网,不管国内的还国外的。有一个错误:Two or more interfaces found, checking IP forwarding 但cat /proc/sys/net/ipv4/ip_forward 的返回结果是1 现在服务器重启直接连不上了 日志文件已发送到你邮箱 帮忙看看 谢谢
    yandong318年前(2016-08-04)回复
  14. 博主,我第一次安装是可以正常使用的,后来服务器重启后,xl2tpd启动就一直失败,重新跑一遍脚本后还是一样,启动失败。ipsec verify的时候pluto监听端口是失败的。ipsec status 030 ignoring message from whack with bad magic 2003331864; should be 1869114154; Mismatched versions of userland tools.
    cheevar8年前(2016-08-02)回复
    • xl2tpd 启动失败有什么错误信息返回吗?
      秋水逸冰8年前(2016-08-02)回复
      • 第一忘记了,重装后启动只显示failed。建议在脚本里面添加一个卸载的选项,不知道我直接重装是否妥当。下面是我log的地址,麻烦帮我看一下 https://github.com/Qiway/hello-world
        cheevar8年前(2016-08-03)回复
        • 从日志来看,是 xl2tpd 启动失败,而 xl2tpd 是通过 yum 来安装的。我想你需要具体分析一下,为什么会启动失败。启动命令:/etc/init.d/xl2tpd start
          秋水逸冰8年前(2016-08-03)回复
  15. 请问这个L2tp程式是否与锐速不兼容?我发现连接L2tp后会导致全线速度变慢,只有退出vpn,重启锐速后速度才会恢复
    Light8年前(2016-07-28)回复