随着远程办公和云环境的普及,构建一个稳定、安全的虚拟私有网络(VPN)已成为企业与个人用户的重要需求,本文将以 CentOS 7.3(基于 Linux 7.3 内核)为平台,详细介绍如何部署两种主流的开源 VPN 解决方案:IPsec + L2TP 和 OpenVPN,帮助你在本地或云服务器上快速搭建可信赖的远程访问通道。
首先说明前提条件:
你需要一台运行 CentOS 7.3 的物理机或虚拟机(推荐使用最小化安装),具备公网 IP 地址,并确保防火墙(firewalld 或 iptables)允许相关端口开放,常用端口包括:
- IPsec: UDP 500(IKE)、UDP 4500(NAT-T)
- L2TP: UDP 1701
- OpenVPN: TCP/UDP 1194
IPsec + L2TP(适合移动设备和 Windows 用户)
-
安装必要软件包:
yum install -y xl2tpd ipsec-tools
-
配置 StrongSwan(IPsec 后端): 编辑
/etc/ipsec.conf文件,添加如下内容:config setup plutodebug=control protostack=netkey uniqueids=yes
conn %default keylife=20m rekey=yes ike=aes256-sha1-modp1024! esp=aes256-sha1! dpdaction=clear dpddelay=30s
conn l2tp-psk auto=add left=%any leftid=@your.domain.com leftauth=pubkey right=%any rightauth=secret rightauth2=pubkey rightdns=8.8.8.8 also=l2tp
3. 设置预共享密钥(PSK):
在 `/etc/ipsec.secrets` 中添加:
%any %any : PSK "your_strong_pre_shared_key"
4. 配置 L2TP 服务(`/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
5. 创建用户认证文件(`/etc/ppp/chap-secrets`):
username password
6. 启动服务并配置开机自启:
```bash
systemctl enable ipsec xl2tpd
systemctl start ipsec xl2tpd
在客户端(如 Windows、iOS、Android)连接时,选择 L2TP/IPsec 类型,输入服务器地址、用户名和密码即可。
OpenVPN(适合高级用户和跨平台兼容性)
-
安装 OpenVPN:
yum install -y openvpn easy-rsa
-
生成证书和密钥(使用 easy-rsa 工具):
make-cadir /etc/openvpn/easy-rsa cd /etc/openvpn/easy-rsa ./vars ./clean-all ./build-ca ./build-key-server server ./build-key client1 ./build-dh
-
配置 OpenVPN 主文件(
/etc/openvpn/server.conf):port 1194 proto udp dev tun ca /etc/openvpn/easy-rsa/pki/ca.crt cert /etc/openvpn/easy-rsa/pki/issued/server.crt key /etc/openvpn/easy-rsa/pki/private/server.key dh /etc/openvpn/easy-rsa/pki/dh.pem server 10.8.0.0 255.255.255.0 push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 8.8.8.8" keepalive 10 120 comp-lzo user nobody group nobody persist-key persist-tun status /var/log/openvpn-status.log verb 3 -
启动 OpenVPN 并设置防火墙:
systemctl enable openvpn@server systemctl start openvpn@server firewall-cmd --add-port=1194/udp --permanent firewall-cmd --reload
客户端只需导入 .ovpn 配置文件即可连接。
CentOS 7.3 搭建 VPN 服务不仅成本低、安全性高,而且社区支持丰富,IPsec + L2TP 更适合企业级用户,而 OpenVPN 则更适合个性化定制和多平台部署,建议根据实际需求选择合适方案,并定期更新证书与补丁以保障网络安全。

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






