Secrets for authentication using CHAP

CentOS 6下搭建IPsec/L2TP VPN服务详解与配置实战

在企业网络环境中,远程访问内网资源是常见需求,尤其是在老旧系统仍在运行的场景中(如CentOS 6),如何安全、稳定地搭建一个兼容性强、易维护的虚拟私有网络(VPN)成为关键任务,本文将详细讲解如何在CentOS 6操作系统上部署IPsec/L2TP类型的VPN服务,适用于远程员工或分支机构接入内网的需求。

确保你已拥有一个CentOS 6服务器(推荐使用最小化安装版本),并具备root权限,该系统虽然已于2024年停止官方支持,但在某些遗留业务系统中仍广泛使用,因此掌握其基础网络配置尤为重要。

第一步:安装必要软件包
我们需要安装Openswan(用于IPsec)、xl2tpd(用于L2TP协议)和PPP(点对点协议)支持,执行以下命令:

yum install -y openswan xl2tpd ppp

第二步:配置IPsec(Openswan)
编辑 /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-NAT
    rightsubnet=vhost:%priv
    also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
    authby=secret
    pfs=no
    auto=add
    keyingtries=3
    rekey=no
    ike=aes256-sha1,modp1024
    phase2alg=aes256-sha1
    left=%defaultroute
    leftid=@your-server-ip
    right=%any
    rightprotoport=17/1701
    dpddelay=30
    dpdtimeout=120
    dpdaction=clear

接着配置共享密钥,编辑 /etc/ipsec.secrets

@your-server-ip %any : PSK "your-pre-shared-key-here"

注意:请将 your-server-ip 替换为你的服务器公网IP,your-pre-shared-key-here 替换为你自定义的强密码。

第三步:配置L2TP服务(xl2tpd)
编辑 /etc/xl2tpd/xl2tpd.conf

[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

require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4

第五步:添加用户账号
/etc/ppp/chap-secrets 中添加用户:


第六步:启动服务并配置防火墙
启动服务:

service ipsec start
service xl2tpd start
chkconfig ipsec on
chkconfig xl2tpd on

开启IP转发和iptables规则(假设eth0为外网接口):

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A INPUT -i eth0 -p udp --dport 500 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --dport 4500 -j ACCEPT
iptables -A INPUT -i eth0 -p udp --dport 1701 -j ACCEPT
iptables -A FORWARD -i ppp+ -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o ppp+ -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE

客户端可使用Windows自带的“连接到工作网络”功能,选择L2TP/IPsec类型,并输入服务器IP及预共享密钥即可建立连接。

尽管CentOS 6已不再受支持,但其稳定的内核和成熟的服务生态仍可用于特定场景下的轻量级VPN部署,本方案通过IPsec加密数据通道、L2TP封装PPP会话,兼顾安全性与兼容性,适合中小型企业快速构建远程办公环境,建议后续逐步迁移至CentOS Stream或Ubuntu等现代发行版以获得持续更新和安全补丁。

Secrets for authentication using CHAP

半仙加速器-海外加速器 | VPN加速器 | VPN翻墙加速器 | VPN梯子 | VPN外网加速