CentOS 7下搭建IPsec/L2TP VPN服务详解——实现安全远程访问企业内网
在现代企业网络架构中,远程办公已成为常态,为了保障员工通过公共网络访问内部资源的安全性,部署一个稳定、可靠的虚拟私人网络(VPN)至关重要,本文将详细介绍如何在 CentOS 7 系统上搭建基于 IPsec 和 L2TP 协议的 VPN 服务,适用于中小型企业和个人用户,支持 Windows、macOS、iOS 和 Android 客户端连接。
确保你拥有一个运行 CentOS 7 的服务器(建议为最小化安装),并具备公网 IP 地址,登录服务器后,执行以下系统更新命令:
sudo yum update -y
安装必要的软件包,我们使用 xl2tpd 实现 L2TP 隧道,ipsec-tools 或 strongSwan 实现 IPsec 加密协议,这里以 ipsec-tools 为例:
sudo yum install -y xl2tpd ipsec-tools
配置 IPsec 主要涉及 /etc/ipsec.conf 和 /etc/ipsec.secrets 文件,编辑 /etc/ipsec.conf:
config setup
protostack=netkey
plutodebug=control
nat_traversal=yes
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12
conn l2tp-psk
authby=secret
pfs=no
type=transport
left=%defaultroute
leftprotoport=17/1701
right=%any
rightprotoport=17/1701
auto=add
然后编辑 /etc/ipsec.secrets,添加预共享密钥(PSK):
%any %any : PSK "your_strong_pre_shared_key"
重启 IPsec 服务并启用开机自启:
sudo systemctl restart ipsec sudo systemctl enable ipsec
下一步配置 L2TP 服务,编辑 /etc/xl2tpd/xl2tpd.conf:
[global]
listen-addr = your_server_public_ip
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.xl2tpd
length bit = yes
创建 /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
speed 115200
mtu 1400
mru 1400
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
配置用户认证信息,创建 /etc/ppp/chap-secrets 文件:
保存后,启动并启用 xl2tpd 服务:
sudo systemctl start xl2tpd sudo systemctl enable xl2tpd
完成上述步骤后,记得开放防火墙端口(UDP 500、UDP 4500、UDP 1701):
sudo firewall-cmd --permanent --add-port=500/udp sudo firewall-cmd --permanent --add-port=4500/udp sudo firewall-cmd --permanent --add-port=1701/udp sudo firewall-cmd --reload
至此,CentOS 7 上的 IPsec/L2TP VPN 已成功搭建,客户端可通过标准 L2TP/IPsec 连接方式接入,输入服务器 IP、用户名和密码即可建立加密隧道,该方案兼容性强、配置灵活,适合需要安全远程访问的企业环境,是低成本高可靠性的解决方案。

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

