在企业网络部署和远程办公场景中,L2TP(Layer 2 Tunneling Protocol)结合IPsec(Internet Protocol Security)是一种广泛使用的安全虚拟私有网络(VPN)协议组合,它不仅支持多平台兼容(Windows、Linux、iOS、Android等),还提供了强大的加密和认证机制,本文将以CentOS操作系统(以7.x或8.x版本为例)为基础,详细介绍如何搭建一个基于L2TP/IPsec的稳定、安全的VPN服务。
确保你的CentOS服务器已安装并更新至最新版本,且拥有公网IP地址(或通过NAT映射访问),推荐使用EPEL源来获取更多可用软件包,执行以下命令安装必要的工具:
sudo yum install -y epel-release sudo yum install -y xl2tpd ipsec-tools
接下来配置IPsec部分,编辑 /etc/ipsec.conf 文件,添加如下内容:
config setup
protostack=netkey
plutodebug=all
dumpdir=/var/log/pluto/
nat_traversal=yes
interfaces=%defaultroute
conn L2TP-PSK-NAT
rightsubnet=vhost:%priv
also=L2TP-PSK-noNAT
left=%defaultroute
right=%any
authby=secret
pfs=yes
type=transport
auto=add
keylife=1h
rekey=yes
keyingtries=3
conn L2TP-PSK-noNAT
authby=secret
pfs=no
type=tunnel
left=%defaultroute
leftprotoport=17/1701
right=%any
rightprotoport=17/1701
auto=add
keylife=1h
rekey=yes
keyingtries=3
然后编辑密钥文件 /etc/ipsec.secrets,添加共享密钥(即预共享密钥):
%any %any : PSK "your_strong_pre_shared_key_here"
注意:请使用强密码作为PSK,避免被暴力破解。
接着配置L2TP守护进程 xl2tpd,编辑 /etc/xl2tpd/xl2tpd.conf:
[global]
listen-addr = your_server_ip
ip range = 192.168.100.100-192.168.100.200
local ip = 192.168.100.1
require chap = yes
refuse pap = yes
debug = 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
ipcp-accept-local
ipcp-accept-remote
noccp
noauth
mtu 1400
mru 1400
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
配置用户账号,编辑 /etc/ppp/chap-secrets 文件,格式为:
username * password *
john * mypassword123 *
重启相关服务并启用开机自启:
sudo systemctl restart ipsec xl2tpd sudo systemctl enable ipsec xl2tpd
开放防火墙端口(如使用firewalld):
sudo firewall-cmd --add-port=500/udp --permanent sudo firewall-cmd --add-port=4500/udp --permanent sudo firewall-cmd --add-port=1701/udp --permanent sudo firewall-cmd --reload
完成以上步骤后,客户端即可通过L2TP/IPsec连接到CentOS服务器,实现安全远程访问内网资源,建议定期检查日志(/var/log/messages 和 /var/log/pluto.log)以排查问题,并考虑使用证书认证替代PSK以提升安全性。

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






