在现代企业网络和远程办公场景中,构建一个稳定、安全的虚拟私人网络(VPN)至关重要,L2TP(Layer 2 Tunneling Protocol)结合IPsec加密协议,是目前广泛使用的远程接入解决方案之一,它不仅兼容主流操作系统(如Windows、macOS、Linux和移动设备),还能提供端到端的数据加密,确保用户在公共网络中也能安全地访问内部资源。
本文将详细讲解如何在Linux系统(以Ubuntu Server为例)上搭建一个完整的L2TP/IPsec VPN服务器,涵盖环境准备、软件安装、配置文件修改、防火墙设置及客户端连接测试等关键步骤。
你需要一台运行Ubuntu Server 20.04或更高版本的物理机或云服务器,并具备公网IP地址(建议使用静态IP),登录服务器后,执行以下命令更新系统并安装必要工具:
sudo apt update && sudo apt upgrade -y sudo apt install strongswan xl2tpd -y
StrongSwan用于实现IPsec协议栈,而xl2tpd则负责L2TP隧道的建立,编辑IPsec配置文件 /etc/ipsec.conf,添加如下内容:
config setup
plutodebug=control
protostack=netkey
dumpdir=/var/run/pluto/
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
keyexchange=ikev1
ike=aes256-sha1-modp1024!
esp=aes256-sha1!
conn l2tp-psk
auto=add
left=%any
leftid=@your.server.domain.com
right=%any
rightauth=pubkey
rightsourceip=192.168.100.0/24
leftsubnet=192.168.100.0/24
rightdns=8.8.8.8,8.8.4.4
authby=secret
pfs=yes
type=transport
dpddelay=30
dpdtimeout=120
dpdaction=clear
注意替换 your.server.domain.com 为你的服务器域名或IP地址,然后创建预共享密钥文件 /etc/ipsec.secrets:
%any %any : PSK "your_strong_pre_shared_key"
接着配置xl2tpd服务,在 /etc/xl2tpd/xl2tpd.conf 中加入:
[global]
port = 1701
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
require authentication = yes
[lns default]
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
require authentication = yes
refuse pap = yes
refuse chap = yes
require mschap-v2 = yes
name = L2TP-Server
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
创建PPP选项文件 /etc/ppp/options.xl2tpd,用于定义用户认证和DNS分配:
ipcp-accept-local
ipcp-accept-remote
ms-dns 8.8.8.8
ms-dns 8.8.4.4
noccp
noauth
proxyarp
mtu 1280
mru 1280
重启服务并启用开机自启:
sudo systemctl restart strongswan xl2tpd sudo systemctl enable strongswan xl2tpd
在防火墙中开放UDP 500(IKE)、UDP 4500(NAT-T)和UDP 1701(L2TP),并启用IP转发:
sudo ufw allow 500/udp sudo ufw allow 4500/udp sudo ufw allow 1701/udp echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sysctl -p
完成以上步骤后,你就可以使用Windows或Android/iOS设备配置L2TP/IPsec连接,输入服务器IP、用户名密码和预共享密钥即可成功接入,此方案适合中小型企业部署,兼顾安全性与易用性,是你构建私有网络的第一步!

VPN加速器|半仙VPN加速器-免费VPN梯子首选半仙VPN






