CentOS 7.2下搭建IPsec/L2TP VPN服务完整指南(适用于企业远程办公场景)

在当今数字化办公日益普及的背景下,企业员工经常需要通过安全通道远程访问内部网络资源,CentOS 7.2作为一款稳定、成熟的Linux发行版,常被用于服务器部署,本文将详细介绍如何在CentOS 7.2系统上搭建一个基于IPsec与L2TP协议的VPN服务,实现安全、可靠的远程接入。

准备工作
确保你已拥有以下条件:

  • 一台运行CentOS 7.2的服务器(建议配置至少2核CPU、4GB内存)
  • 公网IP地址(静态或动态均可,但需支持端口转发)
  • 熟悉基本Linux命令行操作
  • 安装了必要的开发工具和依赖包(如gcc、make等)

安装与配置StrongSwan(IPsec后端)
StrongSwan是目前主流的IPsec实现方案,支持IKEv1和IKEv2协议,首先更新系统并安装相关软件包:

sudo yum update -y
sudo yum install -y strongswan strongswan-plugins-eap-radius

编辑主配置文件 /etc/strongswan.conf,启用必要插件:

charon {
    load_modular = yes
    plugins {
        attr {
            default {
                # 启用属性插件
            }
        }
        eap {
            default {
                # EAP认证模块
            }
        }
    }
}

配置IPsec策略(ipsec.conf)
创建 /etc/ipsec.conf 文件,定义连接参数:

config setup
    charondebug="ike 1, knl 1, cfg 1"
    uniqueids=no
conn %default
    ikelifetime=60m
    keylife=20m
    rekeymargin=3m
    keyingtries=1
    keyexchange=ikev1
    left=%any
    leftid=@your-domain.com
    right=%any
    rightid=%any
    auto=add
    compress=yes
    ike=aes256-sha1-modp1024!
    esp=aes256-sha1!
conn l2tp-psk
    also=%default
    type=transport
    leftprotoport=17/1701
    rightprotoport=17/1701
    authby=secret
    dpdaction=restart
    aggressive=yes

设置预共享密钥(ipsec.secrets)
创建 /etc/ipsec.secrets 文件,添加身份验证信息:

配置L2TP服务(xl2tpd)
安装并配置L2TP守护进程:

sudo yum install -y xl2tpd

编辑 /etc/xl2tpd/xl2tpd.conf

[global]
listen-addr = your-server-ip
port = 1701
[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 = l2tpd
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

配置PPP选项(/etc/ppp/options.xl2tpd)
此文件控制用户拨号时的网络参数:

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)
添加允许连接的用户名密码:

# client     server   secret                 IP addresses
user1       l2tpd    password123            *

启动服务与防火墙规则
启动服务并设置开机自启:

sudo systemctl enable strongswan xl2tpd
sudo systemctl start strongswan xl2tpd

开放必要端口(使用firewalld):

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

测试与故障排查
客户端可通过Windows自带的“连接到工作区”功能或第三方L2TP/IPsec客户端连接,输入服务器公网IP、用户名和密码即可,若连接失败,请检查日志:

  • journalctl -u strongswan 查看IPsec状态
  • tail -f /var/log/messages 查看L2TP拨号过程

通过以上步骤,你就可以在CentOS 7.2上成功搭建一套企业级L2TP/IPsec VPN服务,满足远程办公、分支机构互联等场景需求,该方案具备高安全性、跨平台兼容性和良好的可扩展性,适合中小型IT团队快速部署。

IPsec预共享密钥  第1张

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