OpenVPN 是一个功能齐全的开源传输层安全 (TLS) VPN 解决方案,可适应各种配置。 在本教程中,您将在 Ubuntu 22.04 服务器上设置 OpenVPN,然后将其配置为可从客户端计算机访问。
1.安装 OpenVPN 和 Easy-RSA
sudo apt update
sudo apt install openvpn easy-rsa -y
2.配置 CA 证书与密钥
Easy-RSA 是一个用于创建和管理 PKI (公钥基础设施)的工具集。2.1.创建 Easy-RSA 工作目录
make-cadir ~/openvpn-ca
cd ~/openvpn-ca
2.2.初始化 PKI
./easyrsa init-pki
2.3.构建 CA(输入密码用于保护私钥)
./easyrsa build-ca nopass
3.生成服务器端密钥和证书
./easyrsa gen-req server nopass
./easyrsa sign-req server server
会提示你确认签署请求。
4.生成 Diffie-Hellman 参数和 TLS 密钥
./easyrsa gen-dh
openvpn --genkey secret ta.key
5.生成客户端证书(以 client1 为例)
./easyrsa gen-req client1 nopass
./easyrsa sign-req client client1
6.配置 OpenVPN 服务器
6.1.复制所需文件
sudo cp pki/ca.crt pki/issued/server.crt pki/private/server.key pki/dh.pem ta.key /etc/openvpn/
6.2.创建配置文件
sudo vim /etc/openvpn/server.conf
6.3.服务配置示例
以下是我的配置示例
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key # This file should be kept secret
dh dh.pem
topology subnet
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 114.114.114.114"
keepalive 10 120
tls-auth ta.key 0 # This file is secret
cipher AES-256-CBC
persist-key
persist-tun
status /var/log/openvpn/openvpn-status.log
verb 3
explicit-exit-notify 1
7.启动openvpn服务
sudo systemctl enable openvpn@server
sudo systemctl start openvpn@server
sudo systemctl status openvpn@server
8.开启 IP 转发和防火墙设置
8.1.开启内核转发
sudo vim /etc/sysctl.conf
net.ipv4.ip_forward=1 #添加或者取消配置注释
退出执行命令使其生效
sudo sysctl -p
8.2.设置 UFW 防火墙
sudo ufw allow 1194/udp
sudo ufw allow OpenSSH
sudo vim /etc/ufw/before.rules
# 允许 VPN 流量通过 UFW
sudo ufw allow in on tun0
sudo ufw allow out on tun0
# 允许组播流量(可选)
sudo ufw allow proto udp to 224.0.0.0/24
在文件顶部添加:
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE
COMMIT
8.3.启用 UFW 转发
sudo vim /etc/default/ufw
#找到:
DEFAULT_FORWARD_POLICY="DROP"
#改为:
DEFAULT_FORWARD_POLICY="ACCEPT"
8.4.重启 UFW 使其生效
sudo ufw disable
sudo ufw enable
9.配置客户端
以下是一个客户端配置示例client
dev tun
dev-type tun
remote 服务端ip 1443 udp
nobind
persist-tun
cipher AES-128-CBC
data-ciphers AES-256-CBC:AES-256-GCM
tun-mtu 1500
auth SHA1
verb 2
mute 3
push-peer-info
ping 10
ping-restart 60
hand-window 70
server-poll-timeout 4
reneg-sec 2592000
sndbuf 393216
rcvbuf 393216
remote-cert-tls server
key-direction 1
<ca>
# 内容来自 ca.crt
</ca>
<cert>
# 内容来自 client1.crt
</cert>
<key>
# 内容来自 client1.key
</key>
<tls-auth>
# 内容来自 ta.key
</tls-auth>
10.客户端证书吊销
使用revoke吊销证书
./easyrsa revoke client1
#生成新的吊销列表
/easyrsa gen-crl
#将生成的吊销列表文件复制到openvpn服务目录
sudo cp pki/crl.pem /etc/openvpn/server/
然后重启openvpn
sudo systemctl restart openvpn-server@server
查看吊销证书列表
openssl crl -in /etc/openvpn/server/crl.pem -noout -text
参考来源:https://bruceboron.com/2025/05/15/2025/05/setup_openvpn_in_ubuntu/
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/1088
相关推荐
- ubuntu22.04编译安装postgresql17.5
- ubuntu使用deb包安装指定版本内核
- ubuntu修改grub引导切换到指定内核的方法
- ubuntu使用nvbandwidth测试单节点gpu带宽性能
- ubuntu24.04LTS添加apt源
- ubuntu下使用qperf工具测试RDMA网络带宽和延迟
- ubuntu22.04关闭自动更新和禁止unattended-upgrades服务开机启动
- ubuntu22.04使用nccl-tests进行单机多卡通信测试
- ubuntu22.04编译安装hwloc/libevent/ucx/openpmix/openmpi
- ubuntu安装openvpn并配置连接
评论列表