为什么选择 Pritunl?
在企业级 VPN 解决方案中,Pritunl 是一个值得关注的开源选择。它基于 OpenVPN 和 WireGuard,提供完整的 SELinux 支持、多平台部署能力和企业级安全特性。
核心优势:
- 完全开源,免费版本即可满足基本需求
- 支持 AWS、Google Cloud、Azure 等主流云平台
- 内置 SELinux 策略,安全性极高
- 支持集群部署,可扩展性强
- 提供 Web 管理界面,操作简便
适用场景:
- 中小企业远程办公
- 多分支机构互联
- 开发测试环境隔离
- 云上资源安全访问
系统要求
操作系统推荐
Pritunl 推荐使用以下 Linux 发行版:
| 发行版 | 推荐程度 | 说明 |
|---|
|——–|———-|——|
| AlmaLinux 9 | ⭐⭐⭐⭐⭐ | 首选,完整 SELinux 支持 |
|---|---|---|
| Rocky Linux 9 | ⭐⭐⭐⭐ | 良好兼容性 |
| RHEL 9 | ⭐⭐⭐⭐ | 企业级支持 |
| Ubuntu 24.04 | ⭐⭐⭐ | 可用,但官方不保证长期支持 |
| Amazon Linux 2023 | ⭐⭐⭐⭐ | AWS 环境专用 |
硬件要求
MongoDB 数据库:
- 推荐高内存实例
- 建议内存:4GB+(小型部署),8GB+(中型部署)
- 存储:SSD,至少 20GB
Pritunl 服务器:
- 推荐高 CPU、高主频实例
- 建议 CPU:2核+(小型),4核+(中型)
- 内存:2GB+(小型),4GB+(中型)
- 网络:需要公网 IP
实例类型推荐(AWS)
| 场景 | MongoDB 实例 | Pritunl 实例 |
|---|
|——|————–|————–|
| 小型(<100用户) | t3.medium | t3.medium |
|---|---|---|
| 中型(100-500用户) | r5.large | c5.xlarge |
| 大型(>500用户) | r5.xlarge | c5.2xlarge |
性能建议:每月每并发连接投入 $0.50-$1.00 的服务器成本。
安装指南
1. AlmaLinux 安装(推荐)
1.1 系统准备
`bash
# 更新系统
sudo dnf -y update
# 关闭防火墙(Pritunl 自带防火墙管理)
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service
# 移除 iptables-services(避免冲突)
sudo dnf -y remove iptables-services
`
1.2 添加 MongoDB 仓库
`bash
sudo tee /etc/yum.repos.d/mongodb-org.repo << EOF
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOF
`
1.3 添加 Pritunl 仓库
`bash
sudo tee /etc/yum.repos.d/pritunl.repo << EOF
[pritunl]
name=Pritunl Repository
baseurl=https://repo.pritunl.com/stable/yum/almalinux/9/
gpgcheck=1
enabled=1
gpgkey=https://raw.githubusercontent.com/pritunl/pgp/master/pritunl_repo_pub.asc
EOF
`
1.4 安装软件包
`bash
sudo dnf -y install pritunl pritunl-openvpn wireguard-tools mongodb-org
`
1.5 启动服务
`bash
sudo systemctl enable mongod pritunl
sudo systemctl start mongod pritunl
`
1.6 验证 SELinux
检查 Pritunl 启动日志,确认 SELinux 上下文正确:
`bash
sudo journalctl -u pritunl -f
`
应该看到类似以下日志:
`
[pritunl0][INFO] Starting server
selinux_context = “system_u:system_r:pritunl_t:s0”
`
如果是 none 或 unconfined,说明 SELinux 策略未正常工作。
2. Ubuntu 24.04 安装
2.1 添加仓库密钥
`bash
sudo apt –assume-yes install gnupg
# MongoDB 密钥
curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg –dearmor –yes
# OpenVPN 密钥
curl -fsSL https://swupdate.openvpn.net/repos/repo-public.gpg | sudo gpg -o /usr/share/keyrings/openvpn-repo.gpg –dearmor –yes
# Pritunl 密钥
curl -fsSL https://raw.githubusercontent.com/pritunl/pgp/master/pritunl_repo_pub.asc | sudo gpg -o /usr/share/keyrings/pritunl.gpg –dearmor –yes
`
2.2 添加软件源
`bash
# MongoDB 源
sudo tee /etc/apt/sources.list.d/mongodb-org.list << EOF
deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse
EOF
# OpenVPN 源
sudo tee /etc/apt/sources.list.d/openvpn.list << EOF
deb [ signed-by=/usr/share/keyrings/openvpn-repo.gpg ] https://build.openvpn.net/debian/openvpn/stable noble main
EOF
# Pritunl 源
sudo tee /etc/apt/sources.list.d/pritunl.list << EOF
deb [ signed-by=/usr/share/keyrings/pritunl.gpg ] https://repo.pritunl.com/stable/apt noble main
EOF
`
2.3 安装软件包
`bash
sudo apt update
sudo apt –assume-yes install pritunl openvpn mongodb-org wireguard wireguard-tools
`
2.4 启动服务
`bash
sudo systemctl start pritunl mongod
sudo systemctl enable pritunl mongod
`
2.5 关闭 UFW 防火墙
`bash
sudo ufw disable
`
3. AWS Amazon Linux 安装
3.1 SSH 连接服务器
`bash
ssh -i your-key.pem ec2-user@your-instance-ip
`
3.2 添加仓库
`bash
sudo tee /etc/yum.repos.d/mongodb-org.repo << EOF
[mongodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOF
sudo tee /etc/yum.repos.d/pritunl.repo << EOF
[pritunl]
name=Pritunl Repository
baseurl=https://repo.pritunl.com/stable/yum/amazonlinux/2023/
gpgcheck=1
enabled=1
gpgkey=https://raw.githubusercontent.com/pritunl/pgp/master/pritunl_repo_pub.asc
EOF
`
3.3 安装并启动
`bash
sudo dnf -y update
sudo dnf -y install pritunl pritunl-openvpn wireguard-tools mongodb-org
sudo systemctl enable mongod pritunl
sudo systemctl start mongod pritunl
`
注意:AWS 上的 Amazon Linux 不支持 SELinux,安全性略低于 AlmaLinux。
初始配置
1. 数据库设置
首次启动时,访问服务器的 443 端口进行配置:
`
https://your-server-ip
`
1.1 获取设置密钥
`bash
sudo pritunl setup-key
`
将返回的密钥复制到 Web 界面。
1.2 配置 MongoDB URI
默认情况下,MongoDB URI 已填充本地数据库地址。如果是单机部署,保持默认即可。
集群部署:所有主机连接同一个 MongoDB 数据库。MongoDB URI 格式:
`
mongodb://username:password@host1:27017,host2:27017/database?authSource=admin
`
1.3 登录系统
- 默认用户名:
pritunl - 获取默认密码:
`bash
sudo pritunl default-password
`
登录后系统会提示修改用户名和密码。
2. 基础安全配置
2.1 增加文件限制
防止高负载时连接问题:
`bash
sudo sh -c ‘echo “* hard nofile 64000” >> /etc/security/limits.conf’
sudo sh -c ‘echo “* soft nofile 64000” >> /etc/security/limits.conf’
sudo sh -c ‘echo “root hard nofile 64000” >> /etc/security/limits.conf’
sudo sh -c ‘echo “root soft nofile 64000” >> /etc/security/limits.conf’
`
验证当前文件限制:
`bash
sudo lsof -p $(pgrep pritunl-web) | wc -l
`
2.2 配置 LetsEncrypt 证书
免费 SSL 证书,自动续订:
前置条件:
- 域名 DNS 指向服务器 IP
- 端口 80 公开开放
配置步骤:
- 在 Web 控制台的 Settings 中设置 LetsEncrypt Domain
- 系统自动验证域名所有权
- 自动生成证书并重启 Web 服务器
- 证书每 80 天自动续订
2.3 修改 Web 控制台端口
默认端口 443,可修改:
`bash
# 修改端口
pritunl set app.server_port 8443
# 禁用 HTTP 重定向(端口 80)
pritunl set app.redirect_server false
`
3. 网络配置
3.1 AWS 路由配置
在 EC2 控制台:
- 进入 Network Interfaces
- 选择实例的网络接口
- 禁用 Source/Dest Check
3.2 Google Cloud 路由配置
创建实例时:
- 在网络选项中启用 IP Forwarding
创建第一个 VPN 服务器
1. 创建组织
- 登录 Web 控制台
- 点击 “Organizations”
- 点击 “Add Organization”
- 输入组织名称(如 “Engineering”)
2. 创建用户
- 进入组织
- 点击 “Add User”
- 输入用户名
- 点击 “Add”
3. 创建服务器
- 点击 “Servers”
- 点击 “Add Server”
- 配置服务器参数:
| 参数 | 推荐值 | 说明 |
|---|
|——|——–|——|
| Name | vpn-server | 服务器名称 |
|---|---|---|
| Port | 1194 | VPN 端口(可自定义) |
| Protocol | udp | 推荐 UDP,性能更好 |
| DNS | 8.8.8.8, 8.8.4.4 | Google DNS |
| Virtual Network | 10.0.0.0/24 | VPN 客户端网段 |
- 点击 “Add”
4. 关联组织到服务器
- 在服务器配置中点击 “Organizations”
- 选择要关联的组织
- 点击 “Attach”
5. 启动服务器
点击服务器状态按钮,启动 VPN 服务。
客户端连接
1. 下载客户端
访问 https://pritunl.com/download 下载对应平台的客户端。
支持平台:
- Windows
- macOS
- Linux
- iOS
- Android
2. 导入配置
方法一:URI 导入
- 在 Web 控制台复制用户的配置文件 URI
- 在客户端中选择 “Import”
- 粘贴 URI
方法二:文件导入
- 下载 .ovpn 配置文件
- 在客户端中导入文件
方法三:邮件发送
- 在用户设置中配置邮箱
- 点击 “Email Profile”
- 用户收到配置链接
3. 连接 VPN
- 在客户端中选择服务器
- 点击 “Connect”
- 输入 PIN 码(如果配置了两步验证)
- 连接成功
验证部署
1. 检查连接状态
`bash
# 查看 Pritunl 状态
sudo systemctl status pritunl
# 查看 MongoDB 状态
sudo systemctl status mongod
# 查看 VPN 连接日志
sudo journalctl -u pritunl -f
`
2. 测试网络连通性
连接 VPN 后:
- 访问 https://ifconfig.me 验证出口 IP
- 测试访问内网资源
- 检查 DNS 解析
3. 监控连接数
在 Web 控制台的 Servers 页面查看:
- 在线用户数
- 连接时长
- 流量统计
常见问题
Q1: OpenVPN 认证错误
问题:较新版本的 OpenVPN 客户端使用编码格式发送密码,导致认证失败。
解决:更新 Pritunl 到最新版本:
`bash
sudo dnf update pritunl
`
Q2: SELinux 未生效
问题:启动日志显示 SELinux 上下文为 none 或 unconfined。
解决:
- 确认使用 AlmaLinux/Rocky Linux/RHEL
- 检查 SELinux 状态:
`bash
sestatus
`
- 如果 SELinux 被禁用,编辑
/etc/selinux/config:
`
SELINUX=enforcing
`
- 重启系统
Q3: 无法访问 Web 控制台
问题:浏览器无法打开 https://server-ip
解决:
- 检查服务器防火墙:
`bash
sudo iptables -L -n
`
- 确保端口 443 开放
- 检查 Pritunl 服务状态:
`bash
sudo systemctl status pritunl
`
- 查看日志:
`bash
sudo journalctl -u pritunl -n 100
`
Q4: MongoDB 连接失败
问题:Pritunl 无法连接到 MongoDB
解决:
- 检查 MongoDB 状态:
`bash
sudo systemctl status mongod
`
- 查看 MongoDB 日志:
`bash
sudo journalctl -u mongod -n 100
`
- 检查 MongoDB 配置文件:
`bash
cat /etc/mongod.conf
`
- 确保 MongoDB 监听正确地址
最佳实践
1. 安全加固
- 修改默认端口
- 启用两步验证
- 配置防火墙规则
- 定期更新软件
2. 性能优化
- 选择合适的实例类型
- 配置 DNS 缓存
- 启用 WireGuard(性能更好)
- 调整 MTU 参数
3. 高可用部署
- MongoDB 副本集
- 多 Pritunl 节点
- 负载均衡器
- 自动备份
4. 监控告警
- 集成 InfluxDB + Grafana
- 设置连接数告警
- 监控服务器资源
- 日志集中收集
下一篇预告
第二篇:安全功能详解
将详细介绍:
- SELinux 策略配置
- 动态防火墙规则
- 设备认证机制
- 两步验证配置
- 安全加固最佳实践
相关资源:
- 官方文档:https://docs.pritunl.com/kb/vpn
- GitHub:https://github.com/pritunl
- 社区支持:https://pritunl.com/community
*本文基于 Pritunl 官方文档整理,更多技术细节请访问官方文档。*
发表回复