一、VPN介绍
虚拟专用网络
二、VPN使用场景






三、OpenVPN部署
实现功能:

01.服务器准备
服务器准备Kylin V10SP3(至少两个网卡)
1、上传VPN.tar至10.0.0.7的家目录下
2、解压VPN.tar.gz
3、执行vpn.sh脚本
4、回车
5、Windows解压openvpn-install-2.4.12-I601-Win10包openvpn客户端
6、将下载到桌面的四个文件放到Windows的C:Program FilesOpenVPNconfig目录下
7、运行openvpn客户端
02.客户端配置
模拟SSH连接WEB02 172.16.1.8
需要将网关修改为172.16.1.7
[root@web02 ~]#ip route del 0/0 via 10.0.0.2
[root@web02 ~]#route -n
[root@web02 ~]#ip route add 0/0 via 172.16.1.7
[root@web02 ~]#route -n

四、证书问题:生成新证书
使用 Easy-RSA 生成证书是搭建安全可靠的 OpenVPN 服务最关键的一步。它会帮你创建一套完整的公钥基础设施(PKI),包括一个根证书颁发机构(CA)、服务器证书和客户端证书。
全程概览:生成证书的核心步骤
- 安装并准备 Easy-RSA
- 创建根证书颁发机构 (CA)
- 生成服务器证书
- 生成 Diffie-Hellman (DH) 参数(增强安全性)
- 为每个客户端生成证书
01.安装与初始化 Easy-RSA
首先,需要在服务器(通常是Linux系统)上安装并初始化 Easy-RSA 工具,以建立一个干净的工作环境。
1.安装 easy-rsa
[root@nfs ~]# yum -y install easy-rsa
2.创建一个专门的工作目录(例如 /etc/openvpn/easy-rsa)
[root@nfs ~]# mkdir -p /etc/openvpn/easy-rsa
3.将所有 Easy-RSA 模板文件复制到该目录
[root@nfs ~]# cp -r /usr/share/easy-rsa/3.0.8/* /etc/openvpn/easy-rsa/
4.进入工作目录
[root@nfs easy-rsa]# ll
总用量 84
-rwxr-xr-x 1 root root 76946 6月 16 14:37 easyrsa
-rw-r--r-- 1 root root 4616 6月 16 14:37 openssl-easyrsa.cnf
drwxr-xr-x 2 root root 122 6月 16 14:37 x509-types
5.初始化 PKI (公钥基础设施) 目录
[root@nfs easy-rsa]# ./easyrsa init-pki
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki
[root@nfs easy-rsa]# ll
总用量 84
-rwxr-xr-x 1 root root 76946 6月 16 14:37 easyrsa
-rw-r--r-- 1 root root 4616 6月 16 14:37 openssl-easyrsa.cnf
drwx------ 4 root root 87 6月 16 14:39 pki #初始化后生成的目录
drwxr-xr-x 2 root root 122 6月 16 14:37 x509-types
[root@nfs easy-rsa]# ll pki/
总用量 16
-rw------- 1 root root 4616 6月 16 14:39 openssl-easyrsa.cnf
drwx------ 2 root root 6 6月 16 14:39 private
drwx------ 2 root root 6 6月 16 14:39 reqs
-rw------- 1 root root 4640 6月 16 14:39 safessl-easyrsa.cnf
02.创建根证书颁发机构 (CA)
CA 是整个PKI体系的信任锚点,它将用来签署所有服务器和客户端的证书,其私钥必须妥善保管。
[root@nfs easy-rsa]# ./easyrsa build-ca
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
Enter New CA Key Passphrase: #oldboy123.com
Re-Enter New CA Key Passphrase: #oldboy123.com
Generating RSA private key, 2048 bit long modulus (2 primes)
..............+++++
................+++++
e is 65537 (0x010001)
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:oldboy #oldboy
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/easy-rsa/pki/ca.crt
在执行命令时,系统会提示:
- 设置一个保护 CA 私钥的密码 (PEM pass phrase)
- 为 CA 设置一个通用名 (Common Name),例如
OpenVPN-CA
根证书 (
ca.crt) 和私钥 (ca.key) 会被保存在pki目录下
03.生成服务器证书
服务器证书是颁发给 OpenVPN 服务端的“身份证”。
1.生成服务器证书和私钥 (–nopass 表示不加密私钥)
[root@nfs easy-rsa]# ./easyrsa gen-req server nopass
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
Generating a RSA private key
.................................+++++
.........................+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-55928.UoQ7vt/tmp.nzKo5X'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [server]:oldboy #oldboy
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/server.req
key: /etc/openvpn/easy-rsa/pki/private/server.key
2.使用 CA 的私钥签署服务器证书请求
[root@nfs easy-rsa]# ./easyrsa sign-req server server
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.
Request subject, to be signed as a server certificate for 825 days:
subject=
commonName = oldboy
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes #输入yes
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-55954.mYBIM2/tmp.oRL8lm
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: #oldboy123.com
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'oldboy'
Certificate is to be certified until Sep 18 08:10:25 2028 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /etc/openvpn/easy-rsa/pki/issued/server.crt
生成的服务器证书 (
server.crt) 和私钥 (server.key) 会分别存放在pki/issued和pki/private目录下。
04.生成 Diffie-Hellman (DH) 参数
DH参数用于增强OpenVPN连接的密钥交换安全性。
# 生成 DH 参数文件 (默认生成2048位密钥,可能需要几分钟)
[root@nfs easy-rsa]# ./easyrsa gen-dh
生成的 DH 参数文件 (
dh.pem) 位于pki目录下。
05.为每位客户端生成证书
为每个需要连接的设备(如笔记本电脑、手机)生成一个证书和私钥。
1.生成客户端证书和私钥 (将 "client1" 替换为用户名)
[root@nfs easy-rsa]# ./easyrsa gen-req client1 nopass
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
Generating a RSA private key
...................+++++
..+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-56221.yiJqcV/tmp.4NRBB9'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [client1]:oldboy #oldboy
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/client1.req
key: /etc/openvpn/easy-rsa/pki/private/client1.key
2.使用 CA 的私钥签署客户端证书请求
[root@nfs easy-rsa]# ./easyrsa sign-req client client1
Using SSL: openssl OpenSSL 1.1.1f 31 Mar 2020
You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.
Request subject, to be signed as a client certificate for 825 days:
subject=
commonName = oldboy
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes #yes
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-56247.BVGFhX/tmp.bkxZ6V
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: #oldboy123.com
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'oldboy'
Certificate is to be certified until Sep 18 09:14:49 2028 GMT (825 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client1.crt
为方便管理,可以为每个客户端分别命名,如
client2,client3,client-laptop等。证书和私钥同样存放在pki/issued和pki/private目录下。
06.整理与分发证书文件
1.为服务器整理所需文件
- 根证书:
/etc/openvpn/easy-rsa/pki/ca.crt - 服务器证书:
/etc/openvpn/easy-rsa/pki/issued/server.crt - 服务器私钥:
/etc/openvpn/easy-rsa/pki/private/server.key - DH参数文件:
/etc/openvpn/easy-rsa/pki/dh.pem
将这些文件复制到 OpenVPN 的配置目录(如 /opt/skey/)。
1.创建存放目录
[root@nfs ~]# mkdir -p /opt/skey
2.复制所需文件到存放目录
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/ca.crt /opt/skey/
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/issued/server.crt /opt/skey/
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/private/server.key /opt/skey/
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/dh.pem /opt/skey/
3.查看
[root@nfs ~]# ll /opt/skey/
总用量 20
-rw------- 1 root root 1184 6月 16 17:19 ca.crt
-rw------- 1 root root 424 6月 16 17:20 dh.pem
-rw------- 1 root root 4586 6月 16 17:19 server.crt
-rw------- 1 root root 1704 6月 16 17:19 server.key
4.修改文件权限为600(默认600就不用改了)
[root@nfs ~]# chmod 600 /opt/skey/*
2.为客户端整理所需文件
对于每个客户端(如 client1),需要安全地分发以下三个文件:
- 根证书:
/etc/openvpn/easy-rsa/pki/ca.crt - 客户端证书:
/etc/openvpn/easy-rsa/pki/issued/client1.crt - 客户端私钥:
/etc/openvpn/easy-rsa/pki/private/client1.key
将这些文件复制到一个目录下(如/opt/ckey/)
1.创建存放目录
[root@nfs ~]# mkdir -p /opt/ckey
2.复制所需文件到存放目录
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/ca.crt /opt/ckey
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/issued/client1.crt /opt/ckey
[root@nfs ~]# cp /etc/openvpn/easy-rsa/pki/private/client1.key /opt/ckey
3.查看
[root@nfs ~]# ll /opt/ckey/
总用量 16
-rw------- 1 root root 1184 6月 16 17:26 ca.crt
-rw------- 1 root root 4470 6月 16 17:27 client1.crt
-rw------- 1 root root 1704 6月 16 17:27 client1.key
07.配置OpenVPN服务
1.服务端配置
将上面整理好的文件路径,正确填写到 OpenVPN 服务端配置文件(如 server.conf)
1.安装OpenVPN
[root@nfs ~]# yum -y install openvpn
2.配置OpenVPN server.conf # 默认没有手动创建
[root@nfs openvpn]# cat server.conf
port 1194 #端口
proto udp #协议
dev tun #采用路由隧道模式
ca /opt/skey/ca.crt #ca证书的位置
cert /opt/skey/server.crt #服务端公钥的位置
key /opt/skey/server.key #服务端私钥的位置
dh /opt/skey/dh.pem #证书校验算法
server 10.8.0.0 255.255.255.0 #给客户端分配的地址池
push "route 172.16.1.0 255.255.255.0" #允许客户端访问的内网网段
ifconfig-pool-persist ipp.txt #地址池记录文件位置,未来让openvpn客户端固定ip地址使用的
keepalive 10 120 #存活时间,10秒ping一次,120秒如果未收到响应则视为短线
max-clients 100 #最多允许100个客户端连接
status openvpn-status.log #日志位置,记录openvpn状态
log /var/log/openvpn.log #openvpn日志记录位置
verb 3 #openvpn版本
client-to-client #允许客户端与客户端之间通信
persist-key #通过keepalive检测超时后,重新启动VPN,不重新读取
persist-tun #检测超时后,重新启动VPN,一直保持tun是linkup的,否则网络>
会先linkdown然后再linkup
duplicate-cn #客户端密钥(证书和私钥)是否可以重复
comp-lzo #启动lzo数据压缩格式
注意:复制时需要删掉注释
3.开启内核转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
4.启动服务
[root@nfs openvpn]# systemctl start openvpn@server
[root@nfs openvpn]# netstat -tnulp|grep openvpn
udp 0 0 0.0.0.0:1194 0.0.0.0:* 57739/openvpn
2.客户端配置
客户端配置文件(如 client.ovpn)中,也需要正确指定 ca.crt, client1.crt, client1.key 的路径。
写配置文件client.ovpn
[root@nfs openvpn]# cd /opt/ckey/
[root@nfs ckey]# vim client.ovpn
client
dev tun
proto udp
remote 10.0.0.31 1194 #注意此处更改为openvpn服务端代码
resolv-retry infinite
nobind
ca ca.crt
cert client.crt
key client.key
verb 3
persist-key
comp-lzo
08.测试OpenVPN服务
1.客户端软件安装
官网下载exe软件双击下一步下一步即可
2.下载客户端证书
[root@nfs ~]# sz /opt/ckey/*

3.客户端证书移动到C:Program FilesOpenVPNconfig目录下
1.先删除原有的证书
2.再将下载到桌面的四个文件放到Windows的C:Program FilesOpenVPNconfig目录下

4.启动OpenVPN
双击OpenVPN图标-》任务栏右键图标-》点击连接
5.测试
1.ping 10.8.0.1
[c:~]$ ping 10.8.0.1
正在 Ping 10.8.0.1 具有 32 字节的数据:
来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 10.8.0.1 的回复: 字节=32 时间=1ms TTL=64
来自 10.8.0.1 的回复: 字节=32 时间=1ms TTL=64
来自 10.8.0.1 的回复: 字节=32 时间=1ms TTL=64
10.8.0.1 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 1ms,平均 = 0ms
2.ping 172.16.1.8
[c:~]$ ping 172.16.1.8
正在 Ping 172.16.1.8 具有 32 字节的数据:
请求超时。
注意:172.16.1.8网关需要指向31,数据包需要有去有回
修改172.16.1.8网关指向31后再测试-成功
[c:~]$ ping 172.16.1.8
正在 Ping 172.16.1.8 具有 32 字节的数据:
来自 172.16.1.8 的回复: 字节=32 时间<1ms TTL=63
来自 172.16.1.8 的回复: 字节=32 时间<1ms TTL=63
来自 172.16.1.8 的回复: 字节=32 时间=1ms TTL=63
来自 172.16.1.8 的回复: 字节=32 时间=2ms TTL=63
172.16.1.8 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 2ms,平均 = 0ms
五、写成自动化脚本
01.写脚本
1.准备好需要的文件
[root@nfs ~]# mkdir vpn
[root@nfs ~]# cd vpn/
[root@nfs vpn]# cp -r /opt/skey/ /opt/ckey/ /etc/openvpn/server.conf .
[root@nfs vpn]# ll
总用量 4
drwxr-xr-x 2 root root 77 6月 16 20:28 ckey
-rw-r--r-- 1 root root 899 6月 16 20:28 server.conf
drwxr-xr-x 2 root root 70 6月 16 20:28 skey
2.写脚本
vim vpn.sh
#1.配置epel仓库
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
#2.安装OpenVPN
yum -y install openvpn
#3.复制文件到对应目录
cp server.conf /etc/openvpn
cp -r skey /opt/
cp -r ckey /opt/
#4.开启内容转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
#5.启动服务端
systemctl start openvpn@server
#6.下载客户端证书
sz /opt/ckey/*
3.打包
[root@nfs ~]# tar -zcvf vpn.tar.gz vpn
4.下载
[root@nfs ~]# sz vpn.tar.gz
02.测试
#服务端执行
1.将31服务器恢复快照
2.上传vpn.tar.gz并解压
[root@nfs ~]# ll vpn.tar.gz
-rw-r--r-- 1 root root 8916 6月 16 20:52 vpn.tar.gz
[root@nfs ~]# tar -xf vpn.tar.gz
3.进入脚本目录执行脚本
[root@nfs ~]# cd vpn/
[root@nfs vpn]# sh vpn.sh
#客户端执行
1.将客户端证书放到OpenVPN配置目录中
2.启动OpenVPN连接
#测试
[c:~]$ ping 10.8.0.1
正在 Ping 10.8.0.1 具有 32 字节的数据:
来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
来自 10.8.0.1 的回复: 字节=32 时间=1ms TTL=64
来自 10.8.0.1 的回复: 字节=32 时间=2ms TTL=64
10.8.0.1 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 2ms,平均 = 0ms
[c:~]$ ping 172.16.1.8
正在 Ping 172.16.1.8 具有 32 字节的数据:
来自 172.16.1.8 的回复: 字节=32 时间<1ms TTL=63
来自 172.16.1.8 的回复: 字节=32 时间=1ms TTL=63
来自 172.16.1.8 的回复: 字节=32 时间=2ms TTL=63
来自 172.16.1.8 的回复: 字节=32 时间=4ms TTL=63
172.16.1.8 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 4ms,平均 = 1ms