手把手教你搭建L2TP/IPsec VPN服务器:安全远程访问的完整指南

在现代企业网络中,远程办公已成为常态,为了确保员工能够安全、稳定地访问内网资源,虚拟私人网络(VPN)成为不可或缺的技术手段,L2TP(Layer 2 Tunneling Protocol)结合IPsec加密技术,因其兼容性强、安全性高、配置成熟,被广泛用于构建企业级远程接入解决方案,本文将详细介绍如何在Linux服务器(以Ubuntu为例)上搭建一个L2TP/IPsec VPN服务,帮助你实现安全可靠的远程访问。

第一步:环境准备
你需要一台公网IP的Linux服务器(推荐Ubuntu 20.04或更高版本),并确保防火墙已开放必要的端口:UDP 500(IKE)、UDP 4500(NAT-T)、UDP 1701(L2TP),以及TCP 22(SSH管理),建议使用云服务商如阿里云、腾讯云或AWS部署,便于获取固定公网IP。

第二步:安装必要软件包
通过SSH登录服务器后,执行以下命令安装所需工具:

sudo apt update
sudo apt install strongswan xl2tpd -y

StrongSwan是IPsec协议栈,xl2tpd负责L2TP隧道建立。

第三步:配置IPsec(StrongSwan)
编辑 /etc/ipsec.conf 文件,添加如下内容:

config setup
    plutostderrlog = /var/log/pluto.log
    protostack=netkey
    strictcrlpolicy=yes
conn %default
    ikelifetime=60m
    keylife=20m
    rekeymargin=3m
    keyingtries=3
    keyexchange=ike
    authby=secret
    ike=aes256-sha1-modp1024!
    esp=aes256-sha1!
conn l2tp-psk
    left=%any
    leftid=@yourdomain.com
    right=%any
    rightsubnet=192.168.100.0/24
    auto=add
    type=transport
    authby=secret
    dpddelay=10
    dpdtimeout=20
    dpdaction=restart

注意:leftid应替换为你的域名或IP地址;rightsubnet是你内部网络段,例如192.168.100.0/24。

第四步:设置预共享密钥
编辑 /etc/ipsec.secrets 文件:

@yourdomain.com : PSK "your_strong_pre_shared_key"

your_strong_pre_shared_key 替换为你自定义的强密码(建议含大小写字母、数字、符号)。

第五步:配置L2TP(xl2tpd)
编辑 /etc/xl2tpd/xl2tpd.conf

[lns default]
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
require chap = yes
refuse pap = yes
require authentication = yes
name = l2tpd
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

第六步:创建PPP选项文件
编辑 /etc/ppp/options.xl2tpd

require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4

第七步:添加用户账号
编辑 /etc/ppp/chap-secrets


此处的 usernamepassword 是客户端连接时使用的凭证。

第八步:启动服务与防火墙配置
重启服务:

sudo systemctl restart strongswan
sudo systemctl restart xl2tpd
sudo systemctl enable strongswan xl2tpd

开放防火墙规则(以UFW为例):

sudo ufw allow 500/udp
sudo ufw allow 4500/udp
sudo ufw allow 1701/udp
sudo ufw reload

第九步:测试连接
在Windows或移动设备上,新建L2TP/IPsec连接,输入服务器IP、用户名和密码即可,若成功,你将获得内网IP地址,可访问公司内部系统。

至此,一个功能完整的L2TP/IPsec VPN服务器已部署完成,此方案兼顾安全性与易用性,适合中小型企业快速落地远程办公需求,记住定期更新证书、强化密码策略,并监控日志防止未授权访问。

Secrets for authentication using CHAP  第1张

半仙加速器-海外加速器|VPN加速器|vpn翻墙加速器|VPN梯子|VPN外网加速