L2TP/IPSec一键安装脚本

技术 秋水逸冰 2478868浏览 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. Libreswan 3.18 3.19 在Debian stretch下编译一直有报错,大量的头文件引用路径不对。推荐使用这个包,在Debian 9下可用, https://packages.debian.org/zh-cn/experimental/amd64/libreswan/download 安装VPN后,在Debian 9下还无法使用,问题排查中
    Debian.cn7年前(2017-01-18)回复
  2. state.o: In function `delete_state': /root/l2tp/libreswan-3.18/programs/pluto/state.c:823: undefined reference to `event_free' server.o: In function `init_event_base': /root/l2tp/libreswan-3.18/programs/pluto/server.c:592: undefined reference to `evthread_make_base_notifiable' server.o: In function `pluto_event_new': /root/l2tp/libreswan-3.18/programs/pluto/server.c:442: undefined reference to `event_new' server.o: In function `pluto_event_free': /root/l2tp/libreswan-3.18/programs/pluto/server.c:598: undefined reference to `event_free' /root/l2tp/libreswan-3.18/programs/pluto/server.c:599: undefined reference to `event_free' /root/l2tp/libreswan-3.18/programs/pluto/server.c:600: undefined reference to `event_free' /root/l2tp/libreswan-3.18/programs/pluto/server.c:601: undefined reference to `event_free' timer.o: In function `delete_pluto_event': /root/l2tp/libreswan-3.18/programs/pluto/timer.c:832: undefined reference to `event_free' ikev2_parent.o:/root/l2tp/libreswan-3.18/programs/pluto/ikev2_parent.c:2773: more undefined references to `event_free' follow /usr/local/lib/libevent_pthreads.so: undefined reference to `evthread_set_condition_callbacks' /usr/local/lib/libevent_pthreads.so: undefined reference to `event_mm_malloc_' /usr/local/lib/libevent_pthreads.so: undefined reference to `event_mm_free_' /usr/local/lib/libevent_pthreads.so: undefined reference to `evthread_set_id_callback' /usr/local/lib/libevent_pthreads.so: undefined reference to `evthread_set_lock_callbacks' collect2: ld 返回 1 make[3]: *** [pluto] 错误 1 make[3]: Leaving directory `/root/l2tp/libreswan-3.18/OBJ.linux.i386/programs/pluto' make[2]: *** [local-base] 错误 2 make[2]: Leaving directory `/root/l2tp/libreswan-3.18/programs/pluto' make[1]: *** [recursive-all] 错误 2 make[1]: Leaving directory `/root/l2tp/libreswan-3.18/programs' make: *** [recursive-all] 错误 2 libreswan-3.18 install failed. 啥情况这是
    jcciam7年前(2017-01-17)回复
  3. 请问如何卸载,脚本是否包含了卸载?
    大超7年前(2017-01-14)回复
  4. 有卸载脚本吗?哥
    Eric7年前(2017-01-14)回复
  5. 同腾讯云vps,xl2tpd启动不了啊 换了好几个系统了 从centos 到debian到ubuntu。 [root@VM_23_67_centos ~]# systemctl status xl2tpd * xl2tpd.service - Level 2 Tunnel Protocol Daemon (L2TP) Loaded: loaded (/usr/lib/systemd/system/xl2tpd.service; enabled; vendor preset: disabled) Active: failed (Result: start-limit) since Fri 2017-01-13 14:32:52 CST; 52s ago Process: 22703 ExecStart=/usr/sbin/xl2tpd -D (code=exited, status=1/FAILURE) Process: 22701 ExecStartPre=/sbin/modprobe -q l2tp_ppp (code=exited, status=0/SUCCESS) Main PID: 22703 (code=exited, status=1/FAILURE) Jan 13 14:32:52 VM_23_67_centos systemd[1]: xl2tpd.service: main process exited, code=exited, status=1/FAILURE Jan 13 14:32:52 VM_23_67_centos systemd[1]: Unit xl2tpd.service entered failed state. Jan 13 14:32:52 VM_23_67_centos systemd[1]: xl2tpd.service failed. Jan 13 14:32:52 VM_23_67_centos systemd[1]: xl2tpd.service holdoff time over, scheduling restart. Jan 13 14:32:52 VM_23_67_centos systemd[1]: start request repeated too quickly for xl2tpd.service Jan 13 14:32:52 VM_23_67_centos systemd[1]: Failed to start Level 2 Tunnel Protocol Daemon (L2TP). Jan 13 14:32:52 VM_23_67_centos systemd[1]: Unit xl2tpd.service entered failed state. Jan 13 14:32:52 VM_23_67_centos systemd[1]: xl2tpd.service failed.
    Allegretto7年前(2017-01-13)回复
  6. 你好,我目前主機的網路配置是: eth0 : 211.20.196.x eth1 : 192.168.1.x eth2 : 192.168.2.x 裝好l2tp的腳本後,我在機器內ping得到eth2的GW:192.168.2.1 由於eth2是一台AP,GW就等於是他的web後台 但是我用其他機器連上vpn之後卻ping不到也開不了192.168.2.1的web, 請問我是不是有什麼設置沒做? 謝謝
    sk20017年前(2017-01-12)回复
  7. Centos6 直接脚本安装,客户端能连接成功,但是升级内核(elrepo kernel-ml)准备加入bbr的,结果升级完内核,vpn校验完账号就619关闭连接了。 重装服务器,先升级内核,再安装l2tp脚本,还是连接失败619. 应该是服务器因为升级内核导致的吧,内核到4.9.1了,是啥情况呢,能补救吗?确实想用新内核的bbr算法。
    tiantian7年前(2017-01-12)回复
    • 因为内核升级以后crtscts参数调用失败造成了连接失败,因为连接时端口打不开,用日志跟踪一下就能看到 Jan 21 13:15:52 VPN-SS-LA pppd[31762]: Plugin pppol2tp.so loaded. Jan 21 13:15:52 VPN-SS-LA pppd[31762]: In file /etc/ppp/options.xl2tpd: unrecognized option 'crtscts' Jan 21 13:15:52 VPN-SS-LA xl2tpd[31222]: child_handler : pppd exited for call 1 with code 2 Jan 21 13:15:52 VPN-SS-LA xl2tpd[31222]: call_close: Call 11227 to 115.215.20.1 disconnected Jan 21 13:15:52 VPN-SS-LA xl2tpd[31222]: write_packet: tty is not open yet. 目前在网上没有找到解决办法
      xeeleer7年前(2017-01-21)回复
      • 删除配置文件中的 'crtscts' 跟 'lock' 字段即可
        暗夜星辰7年前(2017-03-04)回复
  8. 忘记说我的一个搬瓦工VPS,还有一个是阿里云的,另一个是AWS的,都不行,作者能帮忙试下?
    SVIP7年前(2017-01-11)回复
  9. 我三个centos6.5的系统都提示安装成功,但是手机就是连接不上,安卓IOS都是,用命令检测状态,也是正常,重启也是如此,外部扫描端口1702.1723都是closed状态,麻烦作者,这是什麽情况
    SVIP7年前(2017-01-11)回复
  10. 请问这个安装是否一定需要2个网卡呢?
    pppkq7年前(2017-01-11)回复
  11. 必须要打开 iptables 才能用 L2TP 吗?
    LisonFan7年前(2017-01-11)回复
  12. 十分感谢!系统为阿里云 CentOS 6.8,PC、ISO和安卓6.0以下的设备可用,安卓6.0连接失败。请问有没有解决方法呢?
    老脸一张7年前(2017-01-09)回复
  13. ../../../mk/depend.mk:28: recipe for target 'plutomain.o' failed make[3]: *** [plutomain.o] Error 1 make[3]: Leaving directory '/root/l2tp/libreswan-3.18/OBJ.linux.x86_64/programs/pluto' Makefile:409: recipe for target 'local-base' failed make[2]: *** [local-base] Error 2 make[2]: Leaving directory '/root/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 '/root/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. 这是为啥呢?看你前面的评论,几个依赖都安装了,还是报错,ubuntu 16
    小月点刀7年前(2017-01-07)回复
  14. 你好,我的是腾讯云的CentOS7.1的系统,安装无异常,但是电脑和手机都连不上VPN,就是把sha2-truncbug=yes这个加上也不可以,大神可不可以给指条路?
    _LINXIONG7年前(2017-01-04)回复
    • 试试debian系统啊.
      ninivii7年前(2017-01-04)回复
      • 也不可以,xl2tpd服务启动不了啊
        _LINXIONG7年前(2017-01-08)回复
  15. root@9940:~# ipsec verify /usr/local/sbin/ipsec: 389: exec: /usr/local/libexec/ipsec/verify: not found
    webfury7年前(2017-01-02)回复