在当前远程办公和跨地域访问日益普遍的背景下,构建一个稳定、安全且易于管理的虚拟私有网络(VPN)服务变得至关重要,对于Linux爱好者或企业IT运维人员来说,Ubuntu作为一款开源、稳定且社区支持强大的操作系统,是搭建L2TP/IPsec VPN服务器的理想选择,本文将详细介绍如何在Ubuntu 20.04或22.04 LTS版本上部署并配置L2TP/IPsec类型的VPN服务器,确保数据传输的安全性与连接的可靠性。
第一步:准备工作
确保你有一台运行Ubuntu系统的物理机或云服务器(如阿里云、AWS EC2),具备公网IP地址,并已安装SSH访问权限,登录服务器后,执行以下命令更新系统包列表并安装必要软件:
sudo apt update && sudo apt upgrade -y sudo apt install strongswan xl2tpd -y
StrongSwan用于实现IPsec协议栈,而xl2tpd则负责L2TP隧道的建立,还需启用内核IP转发功能,编辑 /etc/sysctl.conf 文件,取消注释以下行:
net.ipv4.ip_forward=1
然后执行 sysctl -p 生效设置。
第二步:配置IPsec(StrongSwan)
编辑 /etc/ipsec.conf,添加如下内容:
config setup
charondebug="ike 1, knl 1, cfg 1"
uniqueids=no
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
keyexchange=ikev1
authby=secret
ike=aes256-sha1-modp1024!
esp=aes256-sha1!
conn l2tp-psk
rightsubnet=192.168.100.0/24
left=%any
leftid=@your-server-ip
right=%any
auto=add
type=transport
authby=secret
ike=aes256-sha1-modp1024!
esp=aes256-sha1!
接着创建预共享密钥文件 /etc/ipsec.secrets:
%any %any : PSK "your-secure-psk-here"
替换 your-secure-psk-here 为强密码,建议使用随机生成工具生成。
第三步:配置L2TP(xl2tpd)
编辑 /etc/xl2tpd/xl2tpd.conf如下:
[global]
port = 1701
[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 = l2tp-server
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
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
第四步:用户认证
创建用户账户(vpnuser)并为其设置密码:
sudo adduser vpnuser sudo passwd vpnuser
第五步:启动服务并防火墙配置
重启服务:
sudo systemctl restart strongswan 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、macOS、Android等)中配置L2TP/IPsec连接时,输入服务器IP、用户名和密码,即可建立安全通道。
通过以上步骤,你已在Ubuntu上成功搭建了一个基于L2TP/IPsec的VPN服务器,既满足了远程接入需求,又保障了数据传输的加密与完整性,此方案适合中小企业、开发者及家庭用户使用,具有高性价比和可扩展性。

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






