CentOS 7下搭建L2TP/IPsec VPN服务详解与配置实战
在企业网络环境中,远程访问内网资源是常见需求,L2TP(Layer 2 Tunneling Protocol)结合IPsec(Internet Protocol Security)是一种成熟、稳定且广泛支持的虚拟专用网络(VPN)协议组合,特别适用于Linux服务器环境,CentOS 7作为一款长期支持(LTS)的Linux发行版,因其稳定性与安全性,被广泛用于生产服务器部署,本文将详细介绍如何在CentOS 7系统上搭建L2TP/IPsec VPN服务,帮助管理员实现安全、高效的远程接入。
确保你的CentOS 7服务器具备公网IP地址,并开放必要的端口:UDP 500(IKE)、UDP 4500(NAT-T)、UDP 1701(L2TP),以及可选的TCP 22(SSH管理),若使用防火墙(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
接下来安装所需软件包,我们使用strongSwan作为IPsec后端,xl2tpd作为L2TP守护进程:
yum install -y strongswan xl2tpd
配置IPsec部分,编辑 /etc/strongswan/ipsec.conf 文件:
config setup
charondebug="ike 1, knl 1, cfg 1"
uniqueids=no
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
dpdaction=clear
dpddelay=30s
dpdtimeout=120s
conn l2tp-psk
auto=add
left=%any
leftid=@your-server-ip.com
leftsubnet=192.168.100.0/24
right=%any
rightid=%any
rightsubnet=192.168.100.0/24
authby=secret
type=transport
modecfgdns=8.8.8.8,8.8.4.4
modecfgbanner=Welcome to L2TP/IPsec on CentOS 7!
compress=yes
ike=aes256-sha2_512-modp2048!
esp=aes256-sha2_512!
然后设置预共享密钥(PSK),编辑 /etc/strongswan/ipsec.secrets:
@your-server-ip.com %any : PSK "your-strong-password"
重启IPsec服务并启用开机自启:
systemctl enable strongswan systemctl start strongswan
下一步配置L2TP,编辑 /etc/xl2tpd/xl2tpd.conf:
[global]
port = 1701
ipsec saref = yes
[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.l2tpd
length bit = yes
创建PPP选项文件 /etc/ppp/options.l2tpd:
+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
添加用户账号到 /etc/ppp/chap-secrets:
重启xl2tpd服务:
systemctl enable xl2tpd systemctl start xl2tpd
至此,L2TP/IPsec服务已部署完成,客户端可通过Windows、iOS或Android的原生VPN功能连接,输入服务器IP和预设用户名密码即可接入,此方案适合中小型企业远程办公场景,兼顾安全性与易用性,建议定期更新证书、加强日志监控,并考虑使用Fail2ban等工具防范暴力破解攻击。

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






