随着远程办公和跨地域网络连接需求的不断增长,虚拟私人网络(VPN)已成为企业及个人用户保障网络安全的重要工具,L2TP(Layer 2 Tunneling Protocol)结合IPsec(Internet Protocol Security)是一种广泛采用的加密隧道协议,它既支持数据加密又兼容多种操作系统(如Windows、Linux、iOS、Android),本文将详细介绍如何在CentOS 7/8/9系统上部署并配置一个安全可靠的L2TP/IPsec VPN服务器,涵盖安装、配置文件修改、防火墙设置以及故障排查等关键步骤。
确保你的CentOS服务器具备公网IP地址,并已安装基础开发环境,执行以下命令更新系统并安装必要软件包:
sudo yum update -y sudo yum install -y xl2tpd iptables-services libreswan openssh-server
接下来配置IPsec,编辑 /etc/ipsec.conf 文件,添加如下内容:
config setup
protostack=netkey
plutostderrlog=/var/log/pluto.log
nat_traversal=yes
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=3
keyexchange=ike
authby=secret
conn l2tp-psk
rightsubnet=vhost:%priv
auto=add
left=%defaultroute
leftid=@your-server-ip
right=%any
rightauth=pubkey
rightauthordomains=%any
type=transport
modecfgdns=8.8.8.8,8.8.4.4
also=l2tp-psk
然后编辑 /etc/ipsec.secrets,添加预共享密钥(PSK):
%any %any : PSK "your-strong-psk-here"
重启IPsec服务并启用自动启动:
sudo systemctl enable ipsec sudo systemctl start ipsec
接着配置L2TP守护进程(xl2tpd),编辑 /etc/xl2tpd/xl2tpd.conf:
[global]
port = 1701
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
debug tunnel = yes
debug network = 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 = l2tpserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
创建PPP选项文件 /etc/ppp/options.xl2tpd,用于控制用户认证和IP分配:
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
配置用户账号,在 /etc/ppp/chap-secrets 中添加用户名和密码(格式为:用户名 密码 IP地址):
user1 * password1 *
开启IP转发并配置iptables规则以允许流量通过:
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf sysctl -p iptables -A INPUT -p udp --dport 500 -j ACCEPT iptables -A INPUT -p udp --dport 4500 -j ACCEPT iptables -A INPUT -p udp --dport 1701 -j ACCEPT iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE service iptables save
完成以上步骤后,启动xl2tpd服务:
systemctl enable xl2tpd systemctl start xl2tpd
测试连接时,客户端需使用“L2TP/IPsec”类型,输入服务器IP、预共享密钥及用户名密码,若出现连接失败,请检查日志文件(如 /var/log/messages 和 /var/log/pluto.log),确认IPsec协商是否成功。
此方案已在多个生产环境中稳定运行,适用于中小型企业或家庭用户,建议定期更新系统补丁、更换PSK、限制访问IP段,以进一步提升安全性。

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






