VPN与OpenVPN


VPN与OpenVPN

VPN(虚拟专用网络)概述

VPN(Virtual Private Network) 是一种通过公共网络(如互联网)构建安全、加密的专用通信通道的技术,实现远程设备与目标网络的安全连接。

通俗来讲,VPN 就是一个能让你在上网时“走小路、开隐身”的工具,主要解决两个问题:安全保护突破限制。下面用大白话解释它的原理和作用:

1. VPN 是什么?

VPN 全称是 “虚拟专用网络”(Virtual Private Network),你可以把它想象成一条“加密的网络隧道”。

比如你在家或公司上网,数据就像在普通公路上跑的货车,别人(比如运营商、黑客、网站)都能看到货车装了什么(你的浏览记录、聊天内容、账号密码等)。

而用了 VPN 后,你的数据会先装进一个“加密的集装箱”,通过一条专属隧道(VPN 服务器)传输,到达目的地后再拆箱。这样一来,路上的人只能看到集装箱,看不到里面的内容,也不知道你从哪儿来、要去哪儿。

2. VPN 能干什么?

① 保护隐私,防止信息泄露

  • 场景:在咖啡店、机场等公共 WiFi 上网时,黑客可能通过公共网络窃取你的信息(比如登录密码、银行卡号)。
  • VPN 作用:给数据加密,让黑客就算截获数据,也看不懂内容(就像拿到一个锁死的箱子,打不开)。

② 突破地域限制,访问“受限内容”

  • 场景:有些网站或服务(比如国外视频平台、国内的一些本地服务)会根据你的位置(IP 地址)限制访问。比如你在中国想访问美国的 Netflix,或者在美国想访问国内的视频网站。
  • VPN 作用:把你的真实 IP 地址隐藏起来,换成 VPN 服务器所在地区的 IP(比如美国、日本的 IP),让网站以为你在当地,从而解锁内容。

③ 访问公司内部网络(远程办公)

  • 场景:很多公司的内部系统(如文件服务器、办公平台)只能在公司内网访问,员工在家办公时,通过 VPN 连接到公司的私有网络,就像直接坐在公司电脑前一样。

3. VPN 怎么工作?分三步搞定

  1. 连接 VPN 服务器:你打开 VPN 软件,选择一个服务器(比如选“美国纽约”的服务器)。
  2. 数据加密传输:你的所有上网数据(比如打开网页、发消息)会先加密,通过这条“隧道”传到 VPN 服务器。
  3. 服务器转发请求:VPN 服务器用自己的 IP 地址代替你的真实 IP,去访问你要打开的网站,然后把网站内容再加密传回给你,最后解密显示在你的设备上。

4. VPN 的优缺点

优点

  • 安全:加密数据,防止被监控或窃取。
  • 自由:绕过地域限制,访问全球内容。
  • 匿名:隐藏真实 IP,减少被追踪的风险(但注意:不是完全匿名,VPN 服务商可能知道你的数据,所以要选靠谱的!)。

缺点

  • 速度变慢:数据绕路传输,可能导致网速变卡(尤其是免费 VPN,人多更慢)。
  • 隐私风险:有些免费 VPN 可能记录你的浏览数据,甚至卖数据赚钱;还有的国家法律要求 VPN 服务商必须留存用户日志。
  • 并非万能:不能突破所有限制(比如政府级的网络封锁),也不能完全避免恶意软件(还是要装杀毒软件)。

5. 什么时候该用 VPN?

  • 连公共 WiFi 时(保护密码和隐私)。
  • 想访问国外网站/服务时(比如看 Netflix、用 Google)。
  • 公司要求远程办公连内网时。
  • 不想让网站追踪你的真实位置时(比如隐藏 IP 地址)。

6. 注意!选 VPN 要避开这些坑

  • 别用免费 VPN:免费的往往限速、广告多,甚至偷数据(天下没有免费的隧道)。
  • 查“无日志政策”:选明确声明不记录用户数据的服务商(比如不记录你的浏览记录、连接时间)。
  • 看服务器数量和地区:服务器越多、覆盖地区越广,速度和解锁能力越强。

总结

VPN 就像一个“网络伪装者”+“安全保镖”:

  • 想偷偷看国外视频?它帮你伪装成当地人。
  • 怕信息被偷?它帮你把数据锁起来。

但记住:它不是魔法,选对工具(付费、正规服务商)+ 合理使用(别用来干坏事),才能发挥最大作用~

核心原理

  1. 隧道技术:通过封装协议(如IPsec、OpenVPN、WireGuard等)将原始数据包裹在新的数据包中,在公共网络上建立一条逻辑“隧道”。
  2. 加密与认证:对传输数据进行加密(如AES、RSA),并通过身份验证(如证书、密码)确保只有授权用户可访问隧道。
  3. 网络地址转换(NAT):隐藏用户真实IP地址,实现匿名访问。

OpenVPN:开源高安全VPN解决方案

OpenVPN 是基于 OpenSSL库 开发的开源VPN软件,支持多种平台(Windows、Linux、macOS、Android、iOS等),因其高度可定制性和安全性,被企业和个人广泛使用。

工作模式

  • 客户端-服务器模式:用户通过客户端连接到OpenVPN服务器,访问目标网络(如企业内网)。
  • 点对点模式:直接在两台设备间建立隧道,无需中间服务器(需静态IP或穿透技术)。

VPN与OpenVPN的关系

  • VPN是技术概念,指通过加密隧道实现安全连接的一类技术;
  • OpenVPN是具体实现,是VPN技术的一种开源解决方案,以高安全性和灵活性著称。

选择VPN时,可根据需求权衡安全性、易用性和性能:

  • 个人轻度使用:可选易用的商业VPN服务(可能基于OpenVPN或WireGuard)。
  • 企业或技术用户:OpenVPN是首选,可自主搭建服务器,完全掌控数据安全。

OpenVPN环境搭建

1.环境准备

# 服务器环境

  • 操作系统:CentOS 7.9
  • 内网IP:192.168.8.100 (示例)
  • 公网IP:123.183.113.159 (示例)
  • 需要开放的端口:1194/udp (OpenVPN)

# 客户端环境

  • Windows 10/11
  • OpenVPN客户端软件

2.OpenVPN服务器配置

2.1安装必要软件包
[root@zuolaoshi ~]# yum install -y epel-release
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
……
Transaction test succeeded
Running transaction
  正在安装    : epel-release-7-11.noarch         1/1 
  验证中      : epel-release-7-11.noarch         1/1 

已安装:
  epel-release.noarch 0:7-11                         

完毕!
[root@zuolaoshi ~]# yum install -y openvpn easy-rsa
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink| 5.1 kB  00:00:00     
 * base: mirrors.aliyun.com
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
……
已安装:
  easy-rsa.noarch 0:3.0.8-1.el7                                    openvpn.x86_64 0:2.4.12-1.el7                                   

作为依赖被安装:
  pkcs11-helper.x86_64 0:1.11-3.el7                  

完毕!
2.2设置PKI(公钥基础设施)
[root@zuolaoshi ~]# mkdir -p /etc/openvpn/easy-rsa/keys
[root@zuolaoshi ~]# cp -r /usr/share/easy-rsa/3.0.8/* /etc/openvpn/easy-rsa/
[root@zuolaoshi ~]# cp -a /usr/share/doc/easy-rsa-3.0.8/vars.example  /etc/openvpn/easy-rsa/vars
[root@zuolaoshi ~]# cd /etc/openvpn/easy-rsa
[root@zuolaoshi easy-rsa]# 

编辑vars文件:

vim vars

修改以下内容:

[root@zuolaoshi easy-rsa]# grep ^[^#] vars
if [ -z "$EASYRSA_CALLER" ]; then
echo"You appear to be sourcing an Easy-RSA 'vars' file." >&2
echo"This is no longer necessary and is disallowed. See the section called" >&2
echo"'How to use this file' near the top comments for more details." >&2
return 1
fi
set_var EASYRSA_DN "cn_only"
set_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "Beijing"
set_var EASYRSA_REQ_CITY "Shanghai"
set_var EASYRSA_REQ_ORG "MOONRONG IT"
set_var EASYRSA_REQ_EMAIL "netskyman@163.com"
set_var EASYRSA_CA_EXPIRE 36500
set_var EASYRSA_CERT_EXPIRE 36500
set_var EASYRSA_NS_SUPPORT "yes"
2.3创建证书

在当前目录位置:

[root@zuolaoshi easy-rsa]# pwd
/etc/openvpn/easy-rsa
[root@zuolaoshi easy-rsa]# ll
总用量 96
-rwxr-xr-x 1 root root 76946 4月   3 14:37 easyrsa
drwxr-xr-x 2 root root     6 4月   3 14:37 keys
-rw-r--r-- 1 root root  4616 4月   3 14:37 openssl-easyrsa.cnf
-rw-r--r-- 1 root root  8903 4月   3 14:50 vars
drwxr-xr-x 2 root root   122 4月   3 14:37 x509-types
[root@zuolaoshi easy-rsa]# 

1)初始化

在当前目录创建PKI目录,用于存储证书:

[root@zuolaoshi easy-rsa]# ./easyrsa init-pki

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki

2)创建根证书

提示设置密码,这里设置密码为12345678,用于ca对之后生成的server和client证书签名时使用,其他提示内容直接回车即可:

[root@zuolaoshi easy-rsa]# ./easyrsa build-ca

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

Enter New CA Key Passphrase: 12345678
Re-Enter New CA Key Passphrase: 12345678
Generating RSA private key, 2048 bit long modulus
..................................................................+++
............................................................................................+++
e is 65537 (0x10001)
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]:

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

然后可以查看证书过期详情:

[root@zuolaoshi easy-rsa]# openssl x509 -text -in pki/ca.crt -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            a5:5c:70:da:d2:6b:75:00
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: CN=Easy-RSA CA
        Validity
            Not Before: Apr  3 06:59:33 2025 GMT
            Not After : Mar 10 06:59:33 2125 GMT
        Subject: CN=Easy-RSA CA
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
……
         62:01:a1:d3:5d:ab:f4:4f:3f:31:d1:30:34:5b:f7:ef:fd:6f:
         6a:b9:16:88:50:83:75:ea:93:a1:0f:15:4d:9b:2b:11:8e:d6:
         f2:a6:27:18:9c:c9:55:af:17:08:24:b2:57:83:bc:38:75:4d:
         89:23:05:cd:71:d5:91:3f:35:b5:fd:65:4f:4b:8b:9d:48:f7:
         d8:dd:f4:fe
[root@zuolaoshi easy-rsa]# 

3)创建server端证书和私钥文件

nopass表示不加密私钥文件,提示内容直接回车即可

[root@zuolaoshi easy-rsa]# ./easyrsa gen-req server nopass

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Generating a 2048 bit RSA private key
.......................................+++
...........................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-28964.nOL8mm/tmp.0KmYFD'
-----
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]:

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

4)给server端证书签名

提示内容需要输入yes和创建ca根证书时候的密码

[root@zuolaoshi easy-rsa]# ./easyrsa sign server server

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017


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 36500 days:

subject=
    commonName                = server


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-29619.YZkC1k/tmp.fOkCYd
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: 12345678


Certificate created at: /etc/openvpn/easy-rsa/pki/issued/server.crt


[root@zuolaoshi easy-rsa]# 

5)创建Diffie-Hellman文件

密钥交换时的Diffie-Hellman算法

[root@zuolaoshi easy-rsa]# ./easyrsa gen-dh

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
....................................................................................................................+.......................................................................+..............+.........................+.+............................................................................................................++*++*

DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem


[root@zuolaoshi easy-rsa]# 

6)创建client端的证书和私钥文件

nopass表示不加密私钥文件,提示内容直接回车即可:

[root@zuolaoshi easy-rsa]# ./easyrsa gen-req client1 nopass

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017
Generating a 2048 bit RSA private key
.....+++
...........................................................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-29193.zP5HZ2/tmp.xPQ9RC'
-----
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]:

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

7)给client端证书签名

提示内容输入yes和创建ca根证书时候的密码:

[root@zuolaoshi easy-rsa]# ./easyrsa sign client client1

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017


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 36500 days:

subject=common
Name=client1


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-29799.zarTwR/tmp.erHpAW
Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: 12345678
……

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client1.crt
2.4检查配置

检查是否有ca根证书、客户端服务端证书、客户端服务端私钥:

[root@zuolaoshi easy-rsa]# tree
.
├── easyrsa                   # 管理命令
├── keys
├── openssl-easyrsa.cnf
├── pki
│   ├── ca.crt                # ca根证书,服务端与客户端都需要用
│   ├── certs_by_serial
│   │   ├── 3282D2B7F15060106A611496E77ED0D5.pem
│   │   ├── A8B95F1B3A095E363E1B3AC57788103A.pem
│   │   └── E8AD78EDEA4FBCC213D2CB75FA92BA6D.pem
│   ├── dh.pem                # 认证算法 服务端
│   ├── index.txt
│   ├── index.txt.attr
│   ├── index.txt.attr.old
│   ├── index.txt.old
│   ├── issued
│   │   ├── client1.crt   # 客户端证书
│   │   └── server.crt    # 服务端证书
│   ├── openssl-easyrsa.cnf
│   ├── private
│   │   ├── ca.key
│   │   ├── client1.key   # 客户端私钥
│   │   └── server.key    # 服务端私钥
│   ├── renewed
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── reqs
│   │   ├── client1.req      
│   │   └── server.req       
│   ├── revoked
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── safessl-easyrsa.cnf
│   ├── serial
│   └── serial.old
├── vars
└── x509-types
    ├── ca
    ├── client
    ├── code-signing
    ├── COMMON
    ├── email
    ├── kdc
    ├── server
    └── serverClient

15 directories, 27 files

3.配置OpenVPN

[root@zuolaoshi openvpn]# vim /etc/openvpn/server.conf
[root@zuolaoshi openvpn]# cat /etc/openvpn/server.conf
port 1194      #端口,生产建议修改
proto udp      #协议
dev tun        #采用路由隧道模式
ca /etc/openvpn/easy-rsa/pki/ca.crt                   #ca证书的位置
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 "route 192.168.8.0 255.255.255.0"              #允许客户端访问的内网网段,注意这里要和实际环境符合
ifconfig-pool-persist ipp.txt                         #地址池记录文件位置,未来让openvpn客户端固定ip地址使用的
keepalive 10 120                                      #存活时间,10秒ping一次,120秒如果未收到响应则视为断线
max-clients 5  #最多允许5个客户端连接
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数据压缩格式

4.启用IP转发和配置防火墙

[root@zuolaoshi openvpn]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@zuolaoshi openvpn]# sysctl -p
net.ipv4.ip_forward = 1

配置防火墙:

测试时建议关闭防火墙

[root@zuolaoshi openvpn]# firewall-cmd --permanent --add-port=1194/udp
success
[root@zuolaoshi openvpn]# firewall-cmd --permanent --add-masquerade
success
[root@zuolaoshi openvpn]# firewall-cmd --reload
success

5.启动OpenVPN服务

[root@zuolaoshi openvpn]# systemctl start openvpn@server

查看tun0:

[root@zuolaoshi openvpn]# ip a s tun0
6: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::96c4:50f4:34ca:aa5c/64 scope link flags 800 
       valid_lft forever preferred_lft forever
[root@zuolaoshi openvpn]# 

6.OpenVPN客户端配置(window)

6.1下载OpenVPN
https://openvpn.net/community-downloads/

这里从这里下:

https://juejin.cn/post/7419666298941718537

图片

6.2安装及配置客户端

下载客户端证书

将ca根证书、client.key、client.crt放入config目录

在服务端,下载证书:

[root@zuolaoshi easy-rsa]# pwd
/etc/openvpn/easy-rsa
[root@zuolaoshi easy-rsa]# sz pki/ca.crt 
[root@zuolaoshi easy-rsa]# sz pki/private/client1.key 
[root@zuolaoshi easy-rsa]# sz pki/issued/client1.crt

图片

创建客户端配置文件 记事本创建将内容保存道myvpn.ovpn文件中

client                      #模式,客户端
dev tun
proto udp
remote 123.183.113.159 1194  #此处的IP和端口需在防火墙上做端口映射对应的内网IP
resolv-retry infinite
nobind                      #不绑定本地特定端口
ca ca.crt
cert client1.crt
key client1.key
verb 3
persist-key
comp-lzo

7.连接测试

open-vpn客户端安装傻瓜式点点即可

1、打开VPN,选择FILE块

VPN

2、将配置文件拖入

3、点击连接