随着远程办公和企业内网安全访问需求的日益增长,虚拟私人网络(VPN)已成为现代网络架构中不可或缺的一环,L2TP(Layer 2 Tunneling Protocol)结合IPsec加密机制,因其兼容性强、安全性高,在Linux服务器平台上广受欢迎,本文将以CentOS 7/8为例,详细介绍如何在该操作系统上部署并配置L2TP/IPsec VPN服务,帮助网络管理员快速搭建一个稳定、安全的远程接入通道。
确保你的CentOS系统已更新至最新版本,并具备基本的网络连通性,推荐使用CentOS Stream或CentOS 7(长期支持版本),因为它们对L2TP/IPsec的支持更为成熟,安装前请确认系统已开启防火墙规则放行UDP端口500(IKE)、4500(NAT-T)以及1701(L2TP控制协议),若使用firewalld,可通过以下命令临时开放:
firewall-cmd --add-port=500/udp --permanent firewall-cmd --add-port=4500/udp --permanent firewall-cmd --add-port=1701/udp --permanent firewall-cmd --reload
安装必要的软件包,L2TP/IPsec通常依赖于xl2tpd(用于L2TP隧道)和strongswan(IPsec实现),执行以下命令:
yum install -y xl2tpd strongswan
配置IPsec部分时,编辑/etc/strongswan/ipsec.conf文件,定义主配置块和连接策略:
config setup
charondebug="ike 1, knl 1, cfg 1"
uniqueids=yes
conn %default
ikelifetime=60m
keylife=20m
rekey=no
keyingtries=3
dpdaction=clear
dpddelay=30s
auto=add
conn l2tp-psk
right=%any
left=%any
leftsubnet=192.168.100.0/24
rightsourceip=192.168.100.0/24
authby=secret
pfs=yes
type=transport
auto=add
然后创建预共享密钥文件/etc/strongswan/ipsec.secrets,格式如下:
%any %any : PSK "your_strong_pre_shared_key_here"
注意:请将“your_strong_pre_shared_key_here”替换为高强度密码,建议包含大小写字母、数字和特殊字符。
接着配置L2TP守护进程xl2tpd,编辑/etc/xl2tpd/xl2tpd.conf,设置全局参数和连接模板:
[global] port = 1701 listen-addr = 0.0.0.0 [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,确保客户端能正确获取IP地址并启用DNS:
ipcp-accept-local ipcp-accept-remote ms-dns 8.8.8.8 ms-dns 8.8.4.4 noccp auth crtscts lock modem proxyarp lcp-echo-interval 30 lcp-echo-failure 4
用户账号需通过/etc/ppp/chap-secrets文件定义,
username * password *
完成所有配置后,重启服务并验证状态:
systemctl enable strongswan xl2tpd systemctl start strongswan xl2tpd systemctl status strongswan xl2tpd
你可以在Windows或iOS设备上使用L2TP/IPsec连接配置客户端,输入服务器IP地址、用户名和密码即可建立安全隧道,此方案不仅适用于小型企业办公场景,也可扩展用于多分支机构互联,是CentOS平台下可靠且经济的VPN解决方案。

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

