Linux: 企业级 VPN 服务 OpenVPN
- TAGS: Linux
本章内容
- OpenVPN 简介
- OpenVPN 部署
- OpenVPN 故障排错
- OpenVPN 高级功能
- 扩展知识
OpenVPN简介
VPN 介绍
专用网:专用网就是在两个网络(例如,北京和广州)之间架设一条专用线路, 但是它并不需要真正地去铺设光缆之类的物理线路。虽然没有亲自去铺设,但是 需要向电信运营商申请租用专线,在这条专用的线路上只传输自己的信息,所以 安全稳定,同时也费用高昂
VPN:Virtual Private Network,虚拟私有网络,又称为虚拟专用网络,用于在 不安全的线路上安全的传输数据。
OpenVPN
OpenVPN:一个实现VPN的开源软件,OpenVPN 是一个健壮的、高度灵活的 VPN守 护进程。它支持SSL/TLS 安全、Ethernet bridging、经由代理的 TCP 或 UDP隧 道和 NAT。另外,它也支持动态 IP地址以及DHCP,可伸缩性足以支持数百或数 千用户的使用场景,同时可移植至大多数主流操作系统平台上。
GitHub地址:https://github.com/OpenVPN/openvpn
OpenVPN 示意图
OpenVPN 部署
准备 OpenVPN 部署环境
环境1: 阿里云OpenVPN 实战环境
准备阿里云网络实验环境
1 阿里云创建专有网络 指定城市和可用区 网段名称net1和地址段172.16.0.0/12 交换机名称 net1-sw1 可用区A IPv4地址段172.30.0.0/24 阿里云设置安全组规则允许所有ip能连接1194端口 安全组开放22端口 2 创建openvpn服务器有公网IP的实例1个 3 创建局域网的服务器无公网IP的实例2个 4 重设所有实例密码 5 修改安全组打开 1194/tcp/udp端口
准备完成的实例环境
俩台内网web服务器可以不设置网关
防火墙规则配置
环境2: 局域网 OpenVPN 实战环境
共四台主机 1 openvpn server: CentOS 8.2 eth0:10.0.0.8/24 NAT模式,模拟公网IP eth1:172.30.0.1/24 仅主机模式,私网IP 2 内网主机两台 第一台主机 eth0:172.30.0.100/24 仅主机模式,私网IP,无需网关 第二台主机 eth0:172.30.0.200/24 仅主机模式,私网IP,无需网关 3 Windows 客户端 Windows 10
安装OpenVPN软件包
查看版本
查看官网的OpenVPN的版本
访问官网:https://openvpn.net , 点击 “Communtiy” 查看社区版本。
在不同OS上查看OpenVPN版本
CentOS系统上的EPEL源OpenVPN版本比Ubuntu的仓库中版本更新,以下选择在 CentOS8上部署OpenVPN
范例: CentOS 查看OpenVPN版本
[root@openvpn-server ~]#yum list openvpn openvpn.x86_64 2.4.10-1.el8 epel [root@openvpn-server ~]#yum list easy-rsa easy-rsa.noarch 3.0.8-1.el8 epel
范例: Ubuntu 查看OpenVPN版本
[root@ubuntu1804 ~]#apt list openvpn openvpn/bionic-proposed 2.4.4-2ubuntu1.4 amd64 #查看仓库中软件包有哪些版本可以安装 apt list --all-versions openvpn apt-cache madison openvpn apt-cache madison easy-rsa
安装OpenVPN-软件包方式
Radhat 系
#OpenVPN服务器端 yum install -y openvpn #证书管理工具,签发服务端和客户端证书 yum install -y easy-rsa #查看包中相关文件 rpm -ql openvpn /etc/openvpn/client # 客户端配置文件 /etc/openvpn/server # server配置文件 /usr/lib/systemd/system/[email protected] /usr/lib/systemd/system/[email protected] # server启动脚本 /usr/sbin/openvpn /usr/share/doc/openvpn/sample/sample-config-files/client.conf # 模板,客户端配置 /usr/share/doc/openvpn/sample/sample-config-files/server.conf # 模板,服务端配置 /var/lib/openvpn rpm -ql easy-rsa /usr/share/doc/easy-rsa/vars.example # 变量配置文件,用于配置证书有效性 /usr/share/easy-rsa/3 /usr/share/easy-rsa/3.0.8/easyrsa # 签发证书的程序 #生成服务器配置文件 #cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf /etc/openvpn/server/ #准备证书签发相关文件 mkdir -p /etc/openvpn/server/easy-rsa/ cp -r /usr/share/easy-rsa/3/* /etc/openvpn/server/easy-rsa/ #准备签发证书相关变量的配置文件 cp /usr/share/doc/easy-rsa/vars.example /etc/openvpn/server/easy-rsa/vars # 文件名vars #建议修改给CA和OpenVPN服务器颁发的证书的有效期,可适当加长 vim /etc/openvpn/server/easy-rsa/vars #CA的证书有效期默为为10年,可以适当延长,比如:100年 #set_var EASYRSA_CA_EXPIRE 3650 set_var EASYRSA_CA_EXPIRE 36500 #服务器证书默为为825天,可适当加长,比如:3650天 #set_var EASYRSA_CERT_EXPIRE 825 set_var EASYRSA_CERT_EXPIRE 3650 root@rocky10:~# tree /etc/openvpn/server/easy-rsa/ /etc/openvpn/server/easy-rsa/ ├── easyrsa ├── openssl-easyrsa.cnf ├── vars └── x509-types ├── ca ├── client ├── code-signing ├── COMMON ├── email ├── kdc ├── server └── serverClient 2 directories, 11 files
Debian 系
apt install -y openvpn easy-rsa #查看包中相关文件 dpkg -L openvpn dpkg -L easy-rsa /usr/share/easy-rsa/easyrsa #准备相关配置文件 mkdir -p /etc/openvpn/server/easy-rsa/ cp -r /usr/share/easy-rsa/* /etc/openvpn/server/easy-rsa cd /etc/openvpn/server/easy-rsa/ cp vars.example vars
安装OpenVPN-二进制包安装
EASY_RSA_VERSION=3.2.7 apt install -y openvpn #yum install -y openvpn easy_rsa_url="https://github.com/OpenVPN/easy-rsa/releases/download/v${EASY_RSA_VERSION}/EasyRSA-${EASY_RSA_VERSION}.tgz" mkdir -p /etc/openvpn/server/easy-rsa/ { wget -qO- "$easy_rsa_url" 2>/dev/null || curl -sL "$easy_rsa_url" ; } | tar xz -C /etc/openvpn/server/easy-rsa/ --strip-components 1 chown -R root:root /etc/openvpn/server/easy-rsa/ #cd /etc/openvpn/server/easy-rsa/ #cp vars.example vars
准备 CA 签发机构环境和 TLS key
初始化 PKI 和 CA 签发机构环境
脚本easyrsa帮助用法
cd /etc/openvpn/server/easy-rsa file ./easyrsa ./easyrsa: POSIX shell script, ASCII text executable ./easyrsa help USAGE: easyrsa [options] COMMAND [command-options] A list of commands is shown below. To get detailed usage and help for a command, run: ./easyrsa help COMMAND For a listing of options that can be supplied before the command, use: ./easyrsa help options Here is the list of commands available with a short syntax reminder. Use the 'help' command above to get full usage details. init-pki # 初始化数据,在当前目录下生成pki目录及相关文件,同时会读取当前目录的vars文件 build-ca [ cmd-opts ] # 创建CA证书 不使用密码添加 nopass 参数 gen-dh # 创建Diifie-Hellman密钥 gen-req <filename_base> [ cmd-opts ] # 生成证书 sign-req <type> <filename_base> # 颁发证书 build-client-full <filename_base> [ cmd-opts ] build-server-full <filename_base> [ cmd-opts ] revoke <filename_base> [cmd-opts] renew <filename_base> [cmd-opts] build-serverClient-full <filename_base> [ cmd-opts ] gen-crl update-db show-req <filename_base> [ cmd-opts ] show-cert <filename_base> [ cmd-opts ] show-ca [ cmd-opts ] import-req <request_file_path> <short_basename> # 导入证书请求文件 export-p7 <filename_base> [ cmd-opts ] export-p8 <filename_base> [ cmd-opts ] export-p12 <filename_base> [ cmd-opts ] set-rsa-pass <filename_base> [ cmd-opts ] set-ec-pass <filename_base> [ cmd-opts ] upgrade <type>
初始化PKI生成PKI相关目录和文件
cd /etc/openvpn/server/easy-rsa #初始化数据,在当前目录下生成pki目录及相关文件,同时会读取当前目录的vars文件 ./easyrsa --batch init-pki root@rocky10:/etc/openvpn/server/easy-rsa# tree -F ./ ├── easyrsa* ├── openssl-easyrsa.cnf ├── pki/ │ ├── issued/ │ ├── private/ # 私钥存储位置 │ ├── reqs/ # 请求文件存储位置 │ └── vars.example ├── vars └── x509-types/ ├── ca ├── client ├── code-signing ├── COMMON ├── email ├── kdc ├── server └── serverClient
创建CA机构
cd /etc/openvpn/server/easy-rsa ./easyrsa build-ca nopass # 创建CA证书,不带密码,一路回车 。加 --batch 不需要回车 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/server/easy-rsa/pki/ca.crt #生成自签名的证书文件 root@rocky10:/etc/openvpn/server/easy-rsa# tree -F ./ ├── easyrsa* ├── openssl-easyrsa.cnf ├── pki/ │ ├── ca.crt # 生成自签名的证书文件 │ ├── certs_by_serial/ │ ├── index.txt │ ├── issued/ │ ├── private/ │ │ └── ca.key # 生成私钥文件 │ ├── reqs/ │ ├── revoked/ │ │ ├── certs_by_serial/ │ │ └── private_by_serial/ │ ├── serial │ └── vars.example ├── vars.example └── x509-types/ ├── ca ├── client ├── code-signing ├── COMMON ├── email ├── kdc ├── server └── serverClient #查看一下生成CA相关的文件 [root@openvpn-server 3]#cat pki/serial 01 [root@openvpn-server 3]#ll pki/index.txt -rw------- 1 root root 0 Jan 26 12:17 pki/index.txt [root@openvpn-server 3]#ll pki/ca.crt pki/private/ca.key -rw------- 1 root root 1204 Jan 26 12:17 pki/ca.crt -rw------- 1 root root 1679 Jan 26 12:17 pki/private/ca.key [root@openvpn-server 3]#cat pki/ca.crt [root@openvpn-server 3]#openssl x509 -in pki/ca.crt -noout -text
启用安全增强功能
EasyRSA 认为一份"完整"的 OpenVPN 内联文件应该包含 <tls-crypt>(或 <tls-auth>)段。而这个 TLS key 在 EasyRSA 里是单独的一个文件:
- pki/private/easyrsa-tls.key
两种 key 的生成方式
./easyrsa gen-tls-auth-key # 生成 TLS-AUTH key ./easyrsa gen-tls-crypt-key # 生成 TLS-CRYPT-V1 key(EasyRSA 标注为 Preferred)内部调用 openvpn --genkey tls-crypt # 两条命令都写到同一个文件 pki/private/easyrsa-tls.key,全 PKI 只允许存在一个 # 底层其实就是调 OpenVPN: openvpn --genkey tls-auth <file> openvpn --genkey tls-crypt <file> #然后 EasyRSA 在文件最前面插一行标签: # Easy-RSA TLS-CRYPT Key ← 或 # Easy-RSA TLS-AUTH Key # # 2048 bit OpenVPN static key # -----BEGIN OpenVPN Static key V1-----
作用区别
- 两者都是在 TLS 握手之外再套一层 HMAC 预共享密钥,目的是让没有这个 key 的人连 TLS 握手都发不进来:
| tls-auth | tls-crypt | |
|---|---|---|
| 做什么 | 对控制通道包签名(HMAC 认证) | 对控制通道包签名 + 加密 |
| 控制包可见性 | 明文可见(能看出是 OpenVPN、能看到证书 CN 等握手信息) | 密文,DPI 更难识别 |
| 配置额外要求 | 必须配 key-direction(服务端 0 / 客户端 1) | 不需要 key-direction |
| 挡住什么 | 端口扫描、未授权握手、控制通道 DoS、部分 TLS 漏洞利用 | 同上,外加隐藏握手元数据 |
范例
cd /etc/openvpn/server/easy-rsa/ # 生成 OpenVPN tls‑crypt 的静态密钥文件(tls-crypt.key)客户端和服务端共用这一把密钥,对握手数据包做加密 + 认证 ./easyrsa gen-tls-crypt-key root@rocky10:/etc/openvpn/server/easy-rsa# tree pki pki ├── ca.crt ├── certs_by_serial ├── index.txt ├── issued ├── private │ ├── ca.key │ └── easyrsa-tls.key ├── reqs ├── revoked │ ├── certs_by_serial │ └── private_by_serial ├── serial └── vars.example
上面是 V1 版本的。还有 V2 版本
v1 和 v2 的核心区别
- v1:全网共用一把 PSK。服务端和所有客户端拿的是同一个文件。任何一个客户端泄露,这层防护对所有人同时失效,而且换 key 要同步更新全部客户端。
- v2:每个客户端一把独立的 key。服务端只持有一把 server key(就是你这条命令生成的),它的作用是解包客户端 key,而不是直接参与加密。
机制上:客户端 key 文件里装着两样东西——该客户端自己的密钥材料,以及用 server key 加密封装过的同一份材料(WKc,wrapped client key)。握手时客户端把 WKc 发过去,服务端用 server key 解开拿到这个客户端的专属密钥,再用它校验/解密控制通道。
所以 server key 本身永远不下发给客户端,这是和 v1 最本质的差别。
v2 版本完整流程
# 1. 服务端 key(全服务器一把,只在服务端保存) openvpn --genkey tls-crypt-v2-server /etc/openvpn/server/tls-crypt-v2.key # 2. 每个客户端派生一把(需要用 server key 来签发) openvpn --tls-crypt-v2 /etc/openvpn/server/tls-crypt-v2.key \ --genkey tls-crypt-v2-client /tmp/client-x-tc.key #配置: # server.conf tls-crypt-v2 /etc/openvpn/server/tls-crypt-v2.key # client-x.ovpn <tls-crypt-v2> ...客户端自己那份 846 字节的内容... </tls-crypt-v2>
- 小规模实验 / 少量客户端:用 v1。
./easyrsa gen-tls-crypt-key一条命令,EasyRSA 全流程托管,inline 自动生成 - 客户端数量多、流动性大、或者客户端设备不完全可控:用 v2。per-client key 的隔离价值这时才体现出来,
创建Diifie-Hellman密钥
Diffie-Hellman 算法
Diffie-Hellman 密钥交换方法,由惠特菲尔德·迪菲(Bailey Whitfield Diffie)、马丁·赫尔曼(Martin Edward Hellman)于1976年发表。它是一种安全协议,让双方在完全没有对方任何预先信息的条件下通过不安全信道建立起一个密钥,这个密钥一般作为“对称加密”的密钥而被双方在后续数据传输中使用。DH数学原理是base离散对数问题。做类似功能的还有非对称加密类算法,如:RSA。其应用非常广泛,在SSH、VPN、Https等都有应用。
wiki参考链接: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
创建Diffie-Hellman密钥
cd /etc/openvpn/server/easy-rsa #创建Diffie-Hellman密钥 #方法1 ./easyrsa gen-dh Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa-server/3.0.8/vars Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 2020 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/server/easy-rsa/pki/dh.pem #查看生成的文件 ll pki/dh.pem cat pki/dh.pem #方法2 openssl dhparam -out /root/dh2048.pem 2048
推荐做法:OpenVPN 2.7 上直接不要 DH 文件,改成 server.conf
dh none
2.4 起密钥交换默认走 ECDHE,dh.pem 这条路径只在对端强制用传统 DH 套件时才有意义,而 2.7 的默认 data-ciphers / TLS 配置根本走不到那里。省掉一个要分发和轮换的文件。
创建证书和证书吊销文件
创建服务端证书申请
#创建服务器证书申请文件,其中server是文件前缀 cd /etc/openvpn/server/easy-rsa ./easyrsa gen-req server nopass # 生成服务端证书,不使用密码 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]: 接受Common Name,可以使用默认 Keypair and certificate request completed. Your files are: req: /etc/openvpn/server/easy-rsa/pki/reqs/server.req #生成的申请文件 key: /etc/openvpn/server/easy-rsa/pki/private/server.key #生成的私钥文件
签发服务端证书
#将上面server.req的申请,颁发server类型的证书 cd /etc/openvpn/server/easy-rsa ./easyrsa sign server server #签发服务端证书 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 3650 days: #可以看到vars文件指定的有效期 subject= commonName = server Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes #输入yes回车 Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'server' Certificate is to be certified until Jan 24 04:31:32 2031 GMT (3650 days) Write out database with 1 new entries Data Base Updated Certificate created at: /etc/openvpn/server/easy-rsa/pki/issued/server.crt
# 也可以一次生成 #生成名为 server 的服务器端完整证书(包含私钥、证书请求及签名后的证书),有效期 3650 天,且私钥不加密(无密码保护) ./easyrsa --batch --days=3650 build-server-full server nopass
验证结果
root@rocky10:/etc/openvpn/server/easy-rsa# tree pki pki ├── ca.crt ├── certs_by_serial │ └── C8025EA9176D4AAFF783B3372686C22B.pem #服务器证书文件 ├── dh.pem ├── index.txt ├── index.txt.attr ├── index.txt.old ├── inline │ └── private │ ├── README.inline.private │ └── server.inline ├── issued │ └── server.crt #服务器证书文件 ├── private │ ├── ca.key │ ├── easyrsa-tls.key │ └── server.key ├── reqs │ └── server.req ├── revoked │ ├── certs_by_serial │ └── private_by_serial ├── serial ├── serial.old └── vars.example diff pki/certs_by_serial/C8025EA9176D4AAFF783B3372686C22B.pem pki/issued/server.crt ll !* -rw------- 1 root root 4511 Nov 5 01:50 pki/certs_by_serial/C8025EA9176D4AAFF783B3372686C22B.pem -rw------- 1 root root 4511 Nov 5 01:50 pki/issued/server.crt #证书相关文件 root@rocky10:/etc/openvpn/server/easy-rsa# cat pki/serial C8025EA9176D4AAFF783B3372686C22C root@rocky10:/etc/openvpn/server/easy-rsa# cat pki/index.txt V 361101175001Z C8025EA9176D4AAFF783B3372686C22B unknown /CN=server root@rocky10:/etc/openvpn/server/easy-rsa# cat pki/serial.old c8025ea9176d4aaff783b3372686c22b
准备客户端证书环境
上面服务端证书配置完成,下面是配置客户端证书。方便管理才分开位置,不分也可以的。
这里就不分了。想分可以使用下面命令。
EASY_RSA_VERSION=3.2.7 easy_rsa_url="https://github.com/OpenVPN/easy-rsa/releases/download/v${EASY_RSA_VERSION}/EasyRSA-${EASY_RSA_VERSION}.tgz" mkdir -p /etc/openvpn/server/easy-rsa-client/ { wget -qO- "$easy_rsa_url" 2>/dev/null || curl -sL "$easy_rsa_url" ; } | tar xz -C /etc/openvpn/server/easy-rsa-client/ --strip-components 1 chown -R root:root /etc/openvpn/server/easy-rsa-client/ cd /etc/openvpn/server/easy-rsa-client/ #初始化数据,在当前目录下生成pki目录及相关文件,同时会读取当前目录的vars文件 ./easyrsa init-pki
创建客户端证书申请
cd /etc/openvpn/server/easy-rsa #生成客户端用户的证书申请 client=client-x ./easyrsa gen-req "$client" nopass # 不想交互可以加 --batch Common Name (eg: your user, host, or server name) [client-x]: #接受默认回车 Notice ------ Private-Key and Public-Certificate-Request files created. Your files are: req: /etc/openvpn/server/easy-rsa/pki/reqs/client-x.req #申请证书文件 key: /etc/openvpn/server/easy-rsa/pki/private/client-x.key #私钥文件 #生成两个新文件 root@rocky10:/etc/openvpn/server/easy-rsa# tree pki pki ├── ca.crt ├── certs_by_serial │ └── C8025EA9176D4AAFF783B3372686C22B.pem ├── dh.pem ├── index.txt ├── index.txt.attr ├── index.txt.old ├── inline │ └── private │ ├── README.inline.private │ └── server.inline ├── issued │ └── server.crt ├── private │ ├── ca.key │ ├── client-x.key # 私钥文件 │ └── server.key ├── reqs │ ├── client-x.req # 申请证书文件 │ └── server.req ├── revoked │ ├── certs_by_serial │ └── private_by_serial ├── serial ├── serial.old └── vars.example
签发客户端证书
cd /etc/openvpn/server/easy-rsa #如果请求文件在另的目录,可以使用下面方法导入客户端证书请求文件到CA的工作目录。这里不需要做 #./easyrsa import-req /etc/openvpn/server/easy-rsa-client/pki/reqs/client-x.req client-x # 签发客户端证书。建议修改给客户端颁发证书的有效期,可适当减少,比如:180天 client=client-x ./easyrsa --days=180 sign client "$client" Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa-server/3.0.8/vars Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 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 180 days: subject= commonName = client-x Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes #输入yes后回车 Certificate created at: /etc/openvpn/easy-rsa-server/3/pki/issued/client-x.crt #证书文件 root@rocky10:/etc/openvpn/server/easy-rsa# tree pki pki ├── ca.crt ├── certs_by_serial │ ├── C8025EA9176D4AAFF783B3372686C22B.pem │ └── D3BD2AA1331D4512559EC3124F679CB9.pem ├── dh.pem ├── index.txt ├── index.txt.attr ├── index.txt.attr.old ├── index.txt.old ├── inline │ └── private │ ├── client-x.inline │ ├── README.inline.private │ └── server.inline ├── issued │ ├── client-x.crt #生成客户端证书 │ └── server.crt ├── private │ ├── ca.key │ ├── client-x.key │ ├── easyrsa-tls.key │ └── server.key ├── reqs │ ├── client-x.req │ └── server.req ├── revoked │ ├── certs_by_serial │ └── private_by_serial ├── serial ├── serial.old └── vars.example root@rocky10:/etc/openvpn/server/easy-rsa# cat pki/index.txt V 361101175001Z C8025EA9176D4AAFF783B3372686C22B unknown /CN=server V 270503192302Z D3BD2AA1331D4512559EC3124F679CB9 unknown /CN=client-x root@rocky10:/etc/openvpn/server/easy-rsa# ll pki/issued/ total 16 -rw------- 1 root root 4499 Nov 5 03:23 client-x.crt -rw------- 1 root root 4511 Nov 5 01:50 server.crt root@rocky10:/etc/openvpn/server/easy-rsa# ll pki/certs_by_serial/ total 16 -rw------- 1 root root 4511 Nov 5 01:50 C8025EA9176D4AAFF783B3372686C22B.pem -rw------- 1 root root 4499 Nov 5 03:23 D3BD2AA1331D4512559EC3124F679CB9.pem
如果需要颁发的客户端证书较多,可以使用下面脚本实现客户端证书的批量颁发
客户端证书自动颁发脚本
cat <<\eof> openvpn-user-crt.sh
#!/bin/bash
read -p "请输入用户的姓名拼音(如:${NAME}): " client
cd /etc/openvpn/server/easy-rsa
./easyrsa --batch --days=3650 build-client-full "$client" nopass # 生成客户端完整证书套件,私钥无密码
eof
生成证书吊销列表文件
cd /etc/openvpn/server/easy-rsa #每次吊销证书后都需要更新证书吊销列表文件,并且需要重启OpenVPN服务 ./easyrsa --batch --days=3650 gen-crl # 更新证书吊销列表文件 Notice ------ An updated CRL DER copy has been created: /etc/openvpn/server/easy-rsa/pki/crl.der An updated CRL has been created: /etc/openvpn/server/easy-rsa/pki/crl.pem IMPORTANT: When the CRL expires, an OpenVPN Server which uses a CRL will reject ALL new connections, until the CRL is replaced. root@rocky10:/etc/openvpn/server/easy-rsa# cat pki/crl.pem -----BEGIN X509 CRL----- xxx -----END X509 CRL-----
将证书相关文件复制到服务器相应的目录
cd /etc/openvpn/server/easy-rsa cp pki/ca.crt pki/private/ca.key pki/issued/server.crt pki/private/server.key pki/crl.pem /etc/openvpn/server cp pki/private/easyrsa-tls.key /etc/openvpn/server/tc.key # 前面生成的 tls‑crypt 共享密钥,重命名 # CRL(证书吊销列表)在每次客户端连接时都会被重新读取,但此时 OpenVPN 进程通常已经降权为 nobody 用户 group_name=nogroup # debian 系系统 #group_name="nobody" # redhat chown nobody:"$group_name" /etc/openvpn/server/crl.pem # 降权为 nobody 的 OpenVPN 进程要读取 CRL,能够访问目录内的条目 chmod o+x /etc/openvpn/server/
准备 OpenVPN 服务器配置文件
服务器端配置文件说明
#server.conf文件中以#或;开头的行都为注释 [root@centos8 ~]#grep -Ev "^#|^$" /etc/openvpn/server.conf ;local a.b.c.d #本机监听IP,默认注释监听本机所有IP,也可local 0.0.0.0 port 1194 #端口 ;proto tcp #协议,生产推荐使用TCP proto udp #默认协议,速度快,数据包不做校验 ;dev tap #创建一个以太网隧道,以太网使用tap,一个tap设备允许完整的以太网帧通过Openvpn隧道, 可提供非ip协议的支持,比如IPX协议和AppleTalk协议,tap等同于一个以太网设备,它操作第二层数据包如以太网数据帧。 dev tun #创建一个路由IP隧道,生产推存使用tun.互联网使用tun,一个tun设备大多时候, 被用于基于IP协议的通讯。tun模拟了网络层设备,操作第三层数据包比如IP数据封包。 ;dev-node MyTap #TAP-Win32适配器。非windows不需要配置 ca ca.crt #ca证书文件 cert server.crt #服务器证书文件 key server.key #服务器私钥文件 dh dh2048.pem #dh参数文件,如果dh none代表不需要dh文件 ;topology subnet #通知子网拓扑,不需要配置 server 10.8.0.0 255.255.255.0 #客户端连接后分配IP的地址池,服务器默认会占用第一个IP10.8.0.1将做为客户端的网关 ifconfig-pool-persist ipp.txt #为客户端分配固定IP,不需要配置,建议注释。如有就在文件中写固定ip ;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100 #配置网桥模式,不需要配置,建议注释 ;server-bridge ;push "route 192.168.10.0 255.255.255.0" #给客户端生成的到达服务器后面网段的静态路由,下一跳为openvpn服务器的10.8.0.1。windows输入route PRINT ;push "route 192.168.20.0 255.255.255.0" #推送路由信息到客户端,以允许客户端能够连接到服务器背后的其它私有子网。要提前考虑多机房地址不冲突 ;client-config-dir ccd #为指定的客户端添加路由,此路由通常是客户端后面的内网网段而不是服务端的,也不需要设置,无法保证客户端网络不冲突 ;route 192.168.40.128 255.255.255.248 #同上,不需要设置 ;client-config-dir ccd ;route 10.9.0.0 255.255.255.252 ;learn-address ./script #运行外部脚本,创建不同组的iptables规则,无需配置 ;push "redirect-gateway def1 bypass-dhcp" #启用后,客户端所有流量都将通过VPN服务器,因此生产一般无需配置此项 ;push "dhcp-option DNS 208.67.222.222" #推送DNS服务器,不需要配置 ;push "dhcp-option DNS 208.67.220.220" ;client-to-client #允许不同的client直接通信,不安全,生产环境一般无需要配置 ;duplicate-cn #多个用户共用一个证书,一般用于测试环境,生产环境都是一个用户一个证书,无需开启 keepalive 10 120 #设置服务端检测的间隔和超时时间,默认为每10秒ping一次,如果 120秒没有回应则认为对方已经down tls-auth ta.key 0 #访止DoS等攻击的安全增强配置,可以使用以下命令来生成:openvpn --genkey --secret ta.key 服务器和每个客户端都需要拥有该密钥的一个拷贝。第二个参数在服务器端应该为’0’,在客户端应该为’1’ cipher AES-256-CBC #加密算法 ;compress lz4-v2 #启用Openvpn2.4.X新版压缩算法 ;push "compress lz4-v2" #推送客户端使用新版压缩算法,和下面的comp-lzo不要同时使用 ;comp-lzo #旧户端兼容的压缩配置,需要客户端配置开启压缩,openvpn2.4.X等新版可以不用开启 ;max-clients 100 #最大客户端数 ;user nobody #运行openvpn服务的用户和组 ;group nobody persist-key #重启VPN服务时默认会重新读取key文件,开启此配置后保留使用第一次的key文件,生产环境无需开启 persist-tun #启用此配置后,当重启vpn服务时,一直保持tun或者tap设备是up的,否则会先down然后再up,生产环境无需开启 status openvpn-status.log #openVPN状态记录文件,每分钟会记录一次 ;log openvpn.log #第一种日志记录方式,并指定日志路径,log会在openvpn启动的时候清空日志文件,不建议使用 ;log-append openvpn.log #第二种日志记录方式,并指定日志路径,重启openvpn后在之前的日志后面追加新的日志,生产环境建议使用 verb 3 #设置日志级别,0-9,级别越高记录的内容越详细,0 表示静默运行,只记录致命错误,4 表示合理的常规用法, 5 和 6 可以帮助调试连接错误。9 表示极度冗余,输出非常详细的日志信息 ;mute 20 #相同类别的信息只有前20条会输出到日志文件中 explicit-exit-notify 1 #通知客户端,在服务端重启后自动重新连接,仅能用于udp模式,tcp模式不需要配置即可实现断开重新连接, 且开启此项后tcp配置后将导致openvpn服务无法启动,所以tcp时必须不能开启此项 ;crl-verify /etc/openvpn/easy-rsa-server/3/pki/crl.pem # 第一次吊销证时需要编辑配置文件调用吊销证书的文件
修改服务器端配置文件
client="client" port="11194" protocol="udp" local_ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | sed -n "1"p) vpn_subnet="10.8.0.0" group_name=nogroup # debian 系系统 #group_name="nobody" # redhat # 生成服务端配置 server.conf echo "local ${local_ip} port $port proto $protocol dev tun ca ca.crt cert server.crt key server.key dh none auth SHA512 tls-crypt tc.key topology subnet server $vpn_subnet 255.255.255.0 #push "dhcp-option DNS 1.1.1.1" #push "redirect-gateway def1 bypass-dhcp" #push "route 10.0.0.101 255.255.255.255" ifconfig-pool-persist ipp.txt client-config-dir ccd keepalive 10 120 user nobody group $group_name persist-tun data-ciphers AES-256-GCM:CHACHA20-POLY1305 remote-cert-tls client verb 3 crl-verify crl.pem status /var/log/openvpn/status.log " > /etc/openvpn/server/server.conf if [[ "$protocol" = "udp" ]]; then echo "explicit-exit-notify" >> /etc/openvpn/server/server.conf fi mkdir -p /etc/openvpn/server/ccd mkdir -p /var/log/openvpn
#ipp.txt 是 持久化地址池(ifconfig-pool-persist),作用是让客户端在重连、甚至服务端重启之后,拿到和上次相同的 VPN IP ifconfig-pool-persist 默认 600 秒才刷一次盘。要想立刻看到效果,把刷新间隔调短: ifconfig-pool-persist ipp.txt 60 #客户端连上后,OpenVPN 会把「证书 CN → 分到的 IP」写进 ipp.txt,一行一条: client-x,10.8.0.2 client-y,10.8.0.3 #下次这个 CN 再连,优先把同一个 IP 发回去。 #它是「尽量」而不是「保证」。 如果重连时那个 IP 已经被别人占了,OpenVPN 会另外分一个,不会报错。真要固定 IP 得用: client-config-dir /etc/openvpn/server/ccd # /etc/openvpn/server/ccd/client-x 内容: ifconfig-push 10.8.20.50 255.255.255.0 #CCD 是硬性指定,优先级高于地址池。
准备iptables规则和内核参数
#在服务器开启 ip_forward 转发功能 echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-openvpn-forward.conf sysctl --system # Enable without waiting for a reboot or service restart echo 1 > /proc/sys/net/ipv4/ip_forward #添加SNAT规则 # yum install -y iptables cat <<\EOF> /etc/openvpn/server/ovpn-rules.sh #!/bin/sh set -eu CHAIN=OVPN_FWD NET=10.8.0.0/24 WAN=eth0 PORT=11194 IPT="iptables -w 5" local_ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | sed -n "1"p) add() { if $IPT -nL "$CHAIN" >/dev/null 2>&1; then echo "chain $CHAIN already exists" >&2; exit 1 fi cleanup() { "$0" del >/dev/null 2>&1 || true; } trap cleanup EXIT HUP INT TERM $IPT -N "$CHAIN" $IPT -I INPUT 1 -i "$WAN" -p udp --dport "$PORT" -j ACCEPT $IPT -I INPUT 1 -i tun+ -s "$NET" -j ACCEPT $IPT -I FORWARD 1 -o tun+ -d "$NET" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT $IPT -I FORWARD 1 -i tun+ -s "$NET" -j "$CHAIN" # 策略区:要做「只能上公网」就取消下面几行注释 # $IPT -A "$CHAIN" -d 10.0.0.0/8 -j REJECT # $IPT -A "$CHAIN" -d 172.16.0.0/12 -j REJECT # $IPT -A "$CHAIN" -d 192.168.0.0/16 -j REJECT # $IPT -A "$CHAIN" -d 100.64.0.0/10 -j REJECT # $IPT -A "$CHAIN" -d 127.0.0.0/8 -j REJECT # $IPT -A "$CHAIN" -d 169.254.0.0/16 -j REJECT $IPT -A "$CHAIN" -j ACCEPT $IPT -t nat -A POSTROUTING -s "$NET" ! -d "$NET" -o "$WAN" -j SNAT --to ${local_ip} trap - EXIT HUP INT TERM } del() { $IPT -t nat -D POSTROUTING -s "$NET" ! -d "$NET" -o "$WAN" -j SNAT --to ${local_ip} 2>/dev/null || true $IPT -D FORWARD -i tun+ -s "$NET" -j "$CHAIN" 2>/dev/null || true $IPT -D FORWARD -o tun+ -d "$NET" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT 2>/dev/null || true $IPT -D INPUT -i tun+ -s "$NET" -j ACCEPT 2>/dev/null || true $IPT -D INPUT -i "$WAN" -p udp --dport "$PORT" -j ACCEPT 2>/dev/null || true $IPT -F "$CHAIN" 2>/dev/null || true $IPT -X "$CHAIN" 2>/dev/null || true } case "${1:-}" in add) add ;; del) del ;; *) echo "usage: $0 add|del" >&2; exit 2 ;; esac EOF cat <<\EOF> /etc/systemd/system/openvpn-iptables.service [Unit] Description=OpenVPN iptables rules Before=network-online.target Wants=network-online.target #[email protected] [Service] Type=oneshot RemainAfterExit=yes ExecStart=/etc/openvpn/server/ovpn-rules.sh add ExecStop=-/etc/openvpn/server/ovpn-rules.sh del [Install] WantedBy=multi-user.target EOF chmod +x /etc/openvpn/server/ovpn-rules.sh systemctl daemon-reload systemctl enable openvpn-iptables systemctl start openvpn-iptables echo "iptables -t nat -APOSTROUTING -s 10.8.0.0/24 -j MASQUERADE" >>/etc/rc.d/rc.local # 地址伪装 echo "iptables -A INPUT -i eth0 -p tcp --dport 1194 -j ACCEPT" >>/etc/rc.d/rc.local # 允许访问1194 echo "iptables -A INPUT -m state --stat ESTABLISHED,RELATED -j ACCEPT" >>/etc/rc.d/rc.local # 响应连接允许 chmod +x /etc/rc.d/rc.local /etc/rc.d/rc.local
启动OpenVPN服务
启动 OpenVPN 服务
systemctl enable --now [email protected] systemctl restart [email protected] # 验证转发链路。看计数器有没有涨 iptables -t nat -nvL POSTROUTING # SNAT 的 pkts 应该开始涨 iptables -nvL OVPN_FWD # 同上 cat /var/log/openvpn/status.log cat /etc/openvpn/server/ipp.txt # 应该出现 client-x,10.8.0.2
查看服务状态
systemctl status [email protected] #注意端口号 ss -nulp journalctl -u [email protected] root@rocky10:/etc/openvpn/server# ip a 5: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq state UNKNOWN group default qlen 500 link/none inet 10.8.0.1/24 brd 10.8.0.255 scope global tun0 root@rocky10:/etc/openvpn/server# ip route default via 10.0.0.2 dev eth0 proto static metric 100 10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.201 metric 100 10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.1
准备OpenVPN客户端配置文件
客户端默认范例配置文件说明
[root@openvpn-server ~]#grep -Ev "^(#|;)|^$" /usr/share/doc/openvpn/sample/sample-config-files/client.conf client #声明自己是个客户端 dev tun #接口类型,必须和服务端保持一致 proto udp #协议类型,必须和服务端保持一致 remote my-server-1 1194 #server端的ip和端口,可以写域名但是需要可以解析成IP resolv-retry infinite #如果是写的server端的域名,那么就始终解析,如果域名发生变化,会重新连接到新的域名对应的IP nobind #本机不绑定监听端口,客户端是随机打开端口连接到服务端的1194 persist-key persist-tun ca ca.crt cert client.crt key client.key remote-cert-tls server #指定采用服务器证书校验方式 tls-auth ta.key 1 cipher AES-256-CBC verb 3
生成客户端用户的通用配置文件
#生成客户端文件,文件后缀必须为.ovpn #grep -Ev "^(#|;)|^$" /usr/share/doc/openvpn/sample/sample-config-files/client.conf >/etc/openvpn/client/client-x/client.ovpn #修改配置文件,内容如下 port="11194" protocol="udp" get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "http://ip1.dynupdate.no-ip.com/" || curl -m 10 -4Ls "http://ip1.dynupdate.no-ip.com/")") public_ip="$get_public_ip" cat <<EOF>/etc/openvpn/server/client-common.txt client dev tun proto $protocol remote ${public_ip} $port resolv-retry infinite nobind persist-tun explicit-exit-notify remote-cert-tls server verify-x509-name server name auth SHA512 auth-nocache data-ciphers AES-256-GCM:CHACHA20-POLY1305 tls-client tls-version-min 1.2 tls-ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256 ignore-unknown-option block-outside-dns setenv opt block-outside-dns verb 3 EOF
生成客户端用户的配置文件
# 新加客户端 script_dir=/root/ client="client-2" cd /etc/openvpn/server/easy-rsa/ ./easyrsa --batch --days=3650 build-client-full "$client" nopass # 写客户端配置文件 grep -vh '^#' /etc/openvpn/server/client-common.txt /etc/openvpn/server/easy-rsa/pki/inline/private/"$client".inline > "$script_dir"/"$client".ovpn
OpenVPN 客户端
Windows 安装 OpenVPN 客户端
客户端软件:
附件:
- OpenVPN客户端配置文件。
连接方式:
- 导入 .ovpn 配置文件到OpenVPN客户端。
- 从MFA管理员处获得OTP验证码。
- 点击连接并输入OTP验证码即可连接。
Windows客户端配置准备
放置到windows客户端的 C:\Program Files\OpenVPN\config 目录下
开打OpenVPN GUI,绿色之后就表示连接正常
客户端验证通信
# 观察OpenVPN服务器日志 root@rocky10:/etc/openvpn/server/easy-rsa# journalctl -u [email protected] -f Nov 05 07:22:44 rocky10.2 openvpn[6936]: udp4:10.0.0.1:58682 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 2048 bits RSA, signature: RSA-SHA256, peer temporary key: 253 bits X25519, peer signing digest/type: rsa_pss_rsae_sha256 RSASSA-PSS, key agreement: x25519 Nov 05 07:22:44 rocky10.2 openvpn[6936]: udp4:10.0.0.1:58682 [client-2] Peer Connection Initiated with [AF_INET]10.0.0.1:58682 Nov 05 07:22:44 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 MULTI_sva: pool returned IPv4=10.8.0.2, IPv6=(Not enabled) Nov 05 07:22:44 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 MULTI: Learn: 10.8.0.2 -> client-2/udp4:10.0.0.1:58682 Nov 05 07:22:44 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 MULTI: primary virtual IP for client-2/udp4:10.0.0.1:58682: 10.8.0.2 Nov 05 07:22:44 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 SENT CONTROL [client-2]: 'PUSH_REPLY,route 10.0.0.101 255.255.255.255,route-gateway 10.8.0.1,topology subnet,ping 10,ping-restart 120,ifconfig 10.8.0.2 255.255.255.0,peer-id 0,cipher AES-256-GCM,protocol-flags cc-exit tls-ekm dyn-tls-crypt aead-epoch,tun-mtu 1500' (status=1) Nov 05 07:22:44 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 PUSH: Received control message: 'PUSH_REQUEST' Nov 05 07:22:45 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 Data Channel: cipher 'AES-256-GCM', peer-id: 0 Nov 05 07:22:45 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 Timers: ping 10, ping-restart 240 Nov 05 07:22:45 rocky10.2 openvpn[6936]: client-2/udp4:10.0.0.1:58682 Protocol options: explicit-exit-notify 1, protocol-flags cc-exit tls-ekm dyn-tls-crypt aead-epoch # 验证OpenVPN服务器连接状态 root@rocky10:/etc/openvpn/server/easy-rsa# iptables -t nat -nvL POSTROUTING Chain POSTROUTING (policy ACCEPT 57 packets, 3915 bytes) pkts bytes target prot opt in out source destination 50 3200 SNAT all -- * eth0 10.8.0.0/24 !10.8.0.0/24 to:10.0.0.201 root@rocky10:/etc/openvpn/server/easy-rsa# iptables -nvL OVPN_FWD Chain OVPN_FWD (1 references) pkts bytes target prot opt in out source destination 371 43453 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 root@rocky10:/etc/openvpn/server/easy-rsa# cat /etc/openvpn/server/ipp.txt client-2,10.8.0.2,
验证客户端的路由表
# MacOS ~ % route -n get 10.0.0.101 route to: 10.0.0.101 destination: 10.0.0.101 gateway: 10.8.0.1 interface: utun4 flags: <UP,GATEWAY,HOST,DONE,WASCLONED,IFSCOPE,IFREF> recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire 0 0 0 0 0 0 1500 0
OpenVPN 高级功能
本节介绍OpenVPN的高级功能,主要关于安全加强及客户端的管理功能,比如:员工入职、离职涉及到的创建账户与吊销账户证书。
设置客户端的私钥密码增强安全性
新建一个账户cy,并且设置证书密码,提高证书及登录VPN的安全性。
创建新用户,生成对应的有密码的私钥和证书申请
cd /etc/openvpn/easy-rsa-client/3 #./easyrsa gen-req cy nopass # 不带密码的证书 ./easyrsa gen-req cy # 带密码的证书 123456 [root@openvpn-server 3]#./easyrsa gen-req cy Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa-client/3.0.8/vars Using SSL: openssl OpenSSL 1.1.1g FIPS 21 Apr 2020 Generating a RSA private key ..............+++++ ....................+++++ writing new private key to '/etc/openvpn/easy-rsa-client/3/pki/easy-rsa-14457.v8A2IC/tmp.k97esu' Enter PEM pass phrase: #输入2遍密码 Verifying - Enter PEM pass phrase: ---------------------------------- 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) [cy]: #默认确认 Keypair and certificate request completed. Your files are: req: /etc/openvpn/easy-rsa-client/3/pki/reqs/cy.req key: /etc/openvpn/easy-rsa-client/3/pki/private/cy.key
导入用户证书申请并颁发证书
#cd /etc/openvpn/easy-rsa-server/3 ./easyrsa import-req /etc/openvpn/easy-rsa-client/3/pki/reqs/cy.req cy ll /etc/openvpn/easy-rsa-server/3/pki/reqs/cy.req #确保证书有效期是合理值 [root@centos8 3]#grep EASYRSA_CERT_EXPIRE vars set_var EASYRSA_CERT_EXPIRE 90 #颁发证书 [root@openvpn-server 3]#./easyrsa sign client cy Confirm request details: yes Certificate created at: /etc/openvpn/easy-rsa-server/3/pki/issued/cy.crt
将用户的证书相关文件放在指定的目录中
mkdir /etc/openvpn/client/cy
cp /etc/openvpn/easy-rsa-server/3/pki/issued/cy.crt /etc/openvpn/client/cy
cp /etc/openvpn/easy-rsa-server/3/pki/private/cy.key /etc/openvpn/client/cy
cp /etc/openvpn/certs/{ca.crt,dh.pem,ta.key} /etc/openvpn/client/cy
cp /etc/openvpn/client/client-x/client.ovpn /etc/openvpn/client/cy
[root@openvpn-server 3]#ll /etc/openvpn/client/cy/
total 28
-rw------- 1 root root 1204 Jan 26 15:55 ca.crt
-rw-r--r-- 1 root root 255 Jan 26 15:56 client.ovpn
-rw------- 1 root root 4484 Jan 26 15:53 cy.crt
-rw------- 1 root root 1854 Jan 26 16:00 cy.key
-rw------- 1 root root 424 Jan 26 15:55 dh.pem
-rw------- 1 root root 636 Jan 26 15:55 ta.key
[root@openvpn-server 3]#cd /etc/openvpn/client/cy/
#根据服务器端修改下面配置,需要和服务器同步
[root@openvpn-server cy]#vim client.ovpn
client
dev tun
proto tcp
remote 39.98.146.209 1194
resolv-retry infinite
nobind
#persist-key
#persist-tun
ca ca.crt
cert cy.crt
key cy.key
remote-cert-tls server
#tls-auth ta.key 1
cipher AES-256-CBC
verb 3
compress lz4-v2
tls-auth ta.key 1
# 归档压缩
tar cf cy.tar.gz ./
将相关文件传给客户端主机相应目录
放置到windows客户端的 C:\Program Files\OpenVPN\config 目录下
Windows 客户端重新连接
查看日志
账户证书管理
主要是证书的创建和吊销,对应的员工的入职和离职
证书自动过期
过期时间以服务器时间为准,如果过期,需要重新颁发证书
#过期时间由以下设置决定 [root@centos8 ~]#grep EASYRSA_CERT_EXPIRE /etc/openvpn/server/easy-rsa/vars set_var EASYRSA_CERT_EXPIRE 90
如果证书过期,在服务器端可以看到以下日志
#让服务器时间改为2年后时间 date -s '2 year'
证书手动注销
cd /etc/openvpn/server/easy-rsa # 查看当前证书的有效性,有效为V,无效为R cat pki/index.txt V 361101175001Z C8025EA9176D4AAFF783B3372686C22B unknown /CN=server V 270503192302Z D3BD2AA1331D4512559EC3124F679CB9 unknown /CN=client-x V 361101230608Z 97A987FC7CB84C4902D45B0B9E0D3D6D unknown /CN=client V 361101230642Z 0196E6E9D408EBA00453F94CA85D4A67 unknown /CN=client-2 # 吊销指定的用户的证 ./easyrsa --batch revoke client ./easyrsa --batch --days=3650 gen-crl \cp pki/crl.pem /etc/openvpn/server/crl.pem chown nobody:nobody /etc/openvpn/server/crl.pem # 如果有同名用户入职,之前的人离职 # 删除之前的带 R 的吊销记录,重新生成新的账户证书申请和私钥
账户证书签发
假如公司已有员工叫me已经离职,且证书已被吊销,现在又新来一个员工仍叫me,那么一般的区分办法是在用户名后面加数字,如:me1、me2等,假如还想使用me这个账户名签发证书的话,那么需要删除服务器之前me的账户,并删除签发记录和证书,否则新用户的证书无法导入,并重新颁发证书
脚本内容
插件
脚本插件
脚本插件可以通过在服务器端配置文件中添加 auth-user-pass-verify 指令来使用
auth-user-pass-verify auth-pam.pl via-file
范例:基于用户名密码登录
#1.首先我们需要编写一个用户认证的脚本 (脚本是由openvpn官网提供的) cat <<\EOF >> /etc/openvpn/checkpsw.sh #!/bin/sh ########################################################### # checkpsw.sh (C) 2004 Mathias Sundman # # This script will authenticate OpenVPN users against # a plain text file. The passfile should simply contain # one row per user with the username first followed by # one or more space(s) or tab(s) and then the password. PASSFILE="/etc/openvpn/psw-file" LOG_FILE="/etc/openvpn/openvpn-password.log" TIME_STAMP=`date "+%Y-%m-%d %T"` ########################################################### if [ ! -r "${PASSFILE}" ]; then echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >> ${LOG_FILE} exit 1 fi CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}` if [ "${CORRECT_PASSWORD}" = "" ]; then echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE} exit 1 fi if [ "${password}" = "${CORRECT_PASSWORD}" ]; then echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE} exit 0 fi echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE} exit 1 EOF chmod 755 /etc/openvpn/checkpsw.sh # 脚本加执行权限 #2.现在我们配置用户密码文件 cat /etc/openvpn/psw-file abcdocker 123456 abc 123456 test test #前面为用户名,后面为密码。 中间使用空格分开 #3.配置openvpn的server.conf cat >>/etc/openvpn/server.conf<<EOF script-security 3 system #允许通过环境变量将密码传递给脚本 auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #指定用户认证脚本 client-cert-not-required #不使用客户端证书,使用密码对 username-as-common-name #使用认证用户名,不使用证书的common name EOF #在service.conf最后一行添加 #4.修改client.ovpn client dev tun proto tcp remote 192.168.0.11 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt ;cert cyh.crt #注释 ;key cyh.key #注释 tls-auth ta.key 1 cipher AES-256-CBC comp-lzo verb 3 auth-user-pass #使用用户名密码登录openvpn服务器 #主要是注释crt和key路径,以及添加一行auth-user-pass #5.重启openvpn systemctl restart openvpn@server
mysql插件
范例:
#1.创建mysql库表 mysql> create database openvpn; mysql> use openvpn; #创建用户表 mysql> create table vpnuser ( name char (100) not null, password char (255) default null, active int (10) not null default 1, primary key (name) ); mysql> desc vpnuser; #创建登录表 mysql> create table logtable ( msg char (254), user char (100), pid char (100), host char (100), rhost char (100), time char (100) ); mysql> desc logtable; #插入一条用户数据,用户名test1 密码test1 mysql> insert into vpnuser (name,password) values ('test1',password('test1')); #创建库授权连接账号 mysql> grant all on openvpn.* to vpn@'localhost' identified by 'vpn'; mysql> flush privileges; #2.安装pam_mysql模块 yum install pam_mysql -y #配置pam_mysql模块 vim /etc/pam.d/openvpn auth sufficient pam_mysql.so user=vpn passwd=vpn host=localhost db=openvpn table=vpnuser usercolumn=name passwdcolumn=password [where=vpnuser.active=1] sqllog=0 crypt=2 sqllog=true logtable=logtable logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=time account required pam_mysql.so user=vpn passwd=vpn host=localhost db=openvpn table=vpnuser usercolumn=name passwdcolumn=password [where=vpnuser.active=1] sqllog=0 crypt=2 sqllog=true logtable=logtable logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=time #使用testsaslauthd验证登录情况,如果出现下面的情况,说明我们的配置是正确的 [root@iosvpn ~]# saslauthd -a pam [root@iosvpn ~]# testsaslauthd -u test1 -p test1 -s openvpn 0: OK "Success." #3.安装Openvpn服务器auth-pam插件 yum install pam_devel -y wget http://pkgs.fedoraproject.org/repo/pkgs/openvpn/openvpn-2.0.7.tar.gz/93528233f1f6d02fc18e2c00f82e0aca/openvpn-2.0.7.tar.gz tar xf openvpn-2.0.7.tar.gz cd openvpn-2.0.7/plugin/auth-pam/ make cp openvpn-auth-pam.so /etc/openvpn/ #4.配置openvpn的server.conf cat >>/etc/openvpn/server.conf<<EOF script-security 3 system #允许通过环境变量将密码传递给脚本 plugin /etc/openvpn/openvpn-auth-pam.so openvpn #指定用户认证脚本 client-cert-not-required #不使用客户端证书,使用密码对 username-as-common-name #使用认证用户名,不使用证书的common name EOF #在service.conf最后一行添加 #5.客户端配置文件client.ovpn client dev tun proto tcp remote 192.168.0.11 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt ;cert cyh.crt #注释 ;key cyh.key #注释 tls-auth ta.key 1 cipher AES-256-CBC comp-lzo verb 3 auth-user-pass #使用用户名密码登录openvpn服务器 #主要是注释crt和key路径,以及添加一行auth-user-pass #6.重启openvpn systemctl restart openvpn@server #7.登录验证 查看vpn用户登录表 select from * logtable; #8.禁用某个用户登录 只要把用户表里面的对应用户的active改为0,用户就无法登录
ldap插件
范例:
#1.安装openvpn-auth-ldap yum install openvpn-auth-ldap -y #2.修改ldap.conf认证配置 # 进入openvpn服务器认证配置文件夹 cd /etc/openvpn/auth/ # 备份默认配置文件 cp ldap.conf ldap.conf.bak # 开始修改配置,清空内容进行编辑 echo > ldap.conf cat <<\EOF >/etc/openvpn/auth/ldap.conf <LDAP> URL ldap://10.16.202.178:389 BindDN "cn=admin,dc=ienglsh,dc=cn" Password Ta9FIjsrlMXcWo Timeout 15 TLSEnable no FollowReferrals no </LDAP> <LDAP> # AD服务器地址 URL ldap://192.168.xxx.xxx:389 # 管理员DN BindDN CN=Administrator,CN=Users,DC=XXX,DC=com # 管理员密码 Password Y%G@$%^F^#UGH # 超时时间 Timeout 15 # 是否允许启用TLS TLSEnable no # 是否允许匿名 FollowReferrals no ##开启TLS #TLSEnable yes ## TLS CA证书文件 #TLSCACertFile /usr/local/etc/ssl/ca.pem ## TLS CA证书目录 #TLSCACertDir /etc/ssl/certs ## 客户证书和密钥 ## 如果需要TLS客户端身份验证 #TLSCertFile /usr/local/etc/ssl/client-cert.pem #TLSKeyFile /usr/local/etc/ssl/client-key.pem ## 密码套件,默认就好 ## TLSCipherSuite ALL:!ADH:@STRENGTH </LDAP> <Authorization> # 基础DN BaseDN "OU=xxxx,DC=going-link,DC=com" # 用户搜索过滤条件 我们用uid并且有vpn属性作为AD用户登录名 SearchFilter "(&(uid=%u)(ou=vpn)" # 需要组成员身份 RequireGroup false # 将非组成员添加到PF表(禁用) ##PFTable ips_***_users #<Group> # BaseDN "ou=Groups,dc=example,dc=com" # SearchFilter "(|(cn=ops)(cn=artists))" # MemberAttribute uniqueMember ## Add group members to a PF table (disabled) ## PFTable ips_***_eng #</Group> </Authorization> EOF #3.配置openvpn的server.conf cat >>/etc/openvpn/server.conf<<EOF plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf cn=%u" client-cert-not-required #不使用客户端证书,使用密码对 username-as-common-name #使用认证用户名,不使用证书的common name EOF #在service.conf最后一行添加 #4.客户端配置文件client.ovpn client dev tun proto tcp remote 192.168.0.11 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt ;cert cyh.crt #注释 ;key cyh.key #注释 tls-auth ta.key 1 cipher AES-256-CBC comp-lzo verb 3 auth-user-pass #使用用户名密码登录openvpn服务器 #主要是注释crt和key路径,以及添加一行auth-user-pass #5.重启openvpn systemctl restart openvpn@server #6.登录验证 #测试连接 #安装 openldap-clients yum install -y openldap-clients # 搜索实验 并输入密码 ldapsearch -x -W -D "CN=Administrator,CN=Users,DC=GOING-LINK,DC=com" -b "DC=GOING-LINK,DC=com" -h 192.168.1.62 -s one dn -LLL #输入密码,能看到域内一些OU组织则成功 ldapsearch -x -W -D "CN=Administrator,CN=Users,DC=GOING-LINK,DC=com" -b "DC=going-link,DC=com" -h 192.168.1.62 ldapsearch -x -W -D "CN=Administrator,CN=Users,DC=GOING-LINK,DC=com" -b "OU=disabled,DC=going-link,DC=com" -h 192.168.1.62
GoogleAuthenticator认证插件
什么是MFA?
- Multi-Factor Authentication (MFA) 多因子验证,是一种简单有效的最佳安全实践方法,它能够在用户名和密码之外再额外增加一层安全保护。
官方网站:https://github.com/google/google-authenticator-android
# 先安装epel源 # qrencode 生成二维码图片。 google‑authenticator PAM 模块,TOTP 二次验证(2FA)。 #apt install -y google-authenticator qrencode yum install -y google-authenticator qrencode gauth_dir="/etc/openvpn/server/google-authenticator" mkdir -p "$gauth_dir" chmod 711 "$gauth_dir" echo '#!/bin/bash # Permit (exit 0) when the client sent no username, or a username with no secret # file (i.e. not MFA-enrolled). Only users WITH a secret file fall through # (exit 1) to the google_authenticator OTP check. [ -z "$PAM_USER" ] && exit 0 [ -f "/etc/openvpn/server/google-authenticator/$PAM_USER" ] || exit 0 exit 1' > /etc/openvpn/server/pam-empty-check.sh chmod 755 /etc/openvpn/server/pam-empty-check.sh echo "auth [success=done default=ignore] pam_exec.so quiet /etc/openvpn/server/pam-empty-check.sh auth required pam_google_authenticator.so secret=$gauth_dir/\${USER} user=root account required pam_permit.so" > /etc/pam.d/openvpn echo '#!/bin/bash if [ -e "/etc/openvpn/server/google-authenticator/$common_name" ]; then [ "$username" = "$common_name" ] || exit 1 fi exit 0' > /etc/openvpn/server/verify-mfa.sh chmod 755 /etc/openvpn/server/verify-mfa.sh # 更新服务端配置 find_pam_plugin () { local p for p in /usr/lib/openvpn/openvpn-plugin-auth-pam.so \ /usr/lib64/openvpn/plugins/openvpn-plugin-auth-pam.so \ /usr/lib/openvpn/plugins/openvpn-plugin-auth-pam.so; do [[ -e "$p" ]] && { echo "$p"; return; } done find /usr/lib /usr/lib64 -name 'openvpn-plugin-auth-pam.so' 2>/dev/null | head -1 } echo "plugin $(find_pam_plugin) openvpn" >> /etc/openvpn/server/server.conf echo "auth-user-pass-optional" >> /etc/openvpn/server/server.conf echo "script-security 2" >> /etc/openvpn/server/server.conf echo "auth-user-pass-verify /etc/openvpn/server/verify-mfa.sh via-env" >> /etc/openvpn/server/server.conf echo "auth-gen-token 43200" >> /etc/openvpn/server/server.conf systemctl restart [email protected]
新加用户
# 新加用户 enroll_client_mfa () { local client_name="$1" local secret_file="$gauth_dir/$client_name" mkdir -p "$gauth_dir" # Generate a 160-bit secret -> 32-character base32 string (no padding) local secret secret=$(head -c 20 /dev/urandom | base32 | tr -d '=\n') # Write the secret file in google-authenticator format. The quoted lines are # module options: time-based OTP, rate-limit 3 per 30s, +/-3 step window for # clock skew. DISALLOW_REUSE is intentionally omitted so a code can be reused # within its validity window (avoids errors on quick reconnects). { echo "$secret" echo '" RATE_LIMIT 3 30' echo '" WINDOW_SIZE 3' echo '" TOTP_AUTH' } > "$secret_file" chown root:root "$secret_file" chmod 600 "$secret_file" # The secret file's existence is what marks this client as MFA-enrolled; # the server's verify script keys off it. No separate list to maintain. # Label is "issuer:account" so the authenticator app shows both the OpenVPN # issuer and the client name, making each entry easy to tell apart local uri="otpauth://totp/OpenVPN:${client_name}?secret=${secret}&issuer=OpenVPN" echo echo "Scan this QR code in your authenticator app (Google Authenticator, etc.):" echo printf '%s' "$uri" | qrencode -t ANSIUTF8 echo "Or enter this secret manually: $secret" echo "otpauth URI: $uri" echo echo "When connecting, enter the current 6-digit code as the password." echo } gauth_dir="/etc/openvpn/server/google-authenticator" script_dir=/root/ client="client-3" cd /etc/openvpn/server/easy-rsa/ ./easyrsa --batch --days=3650 build-client-full "$client" nopass grep -vh '^#' /etc/openvpn/server/client-common.txt /etc/openvpn/server/easy-rsa/pki/inline/private/"$client".inline > "$script_dir"/"$client".ovpn printf '<auth-user-pass>\n%s\n</auth-user-pass>\n' "$client" >> "$script_dir"/"$client".ovpn enroll_client_mfa "$client" # 输出新用户 MFA 的私钥信息和 二维码
范例2:LDAP+GoogleAuthenticator认证
参考文档:
插件下载地址:
#1.安装LDAP+GoogleAuthenticator #安装LDAP插件 git clone https://github.com/threerings/openvpn-auth-ldap cd openvpn-auth-ldap yum install re2c libtool openldap openldap-devel openvpn-devel gcc-objc -y #安装依赖 ./regen.sh ./configure --with-openldap=/usr/ --with-openvpn=/usr/ CFLAGS="-fPIC" OBJCFLAGS="-std=gnu11" make make install #安装OTP插件 git clone https://github.com/evgeny-gridasov/openvpn-otp cd openvpn-otp yum install autoconf automake libtool* -y ./autogen.sh ./configure make make install #插件位置 #/usr/local/lib/openvpn-auth-ldap.so #/usr/local/lib/openvpn/openvpn-otp.so #2.修改ldap.conf认证配置 # 进入openvpn服务器认证配置文件夹 cd /etc/openvpn/auth/ # 备份默认配置文件 cp ldap.conf ldap.conf.bak # 开始修改配置,清空内容进行编辑 echo > ldap.conf cat <<\EOF >/etc/openvpn/auth/ldap.conf <LDAP> URL ldap://10.16.202.178:389 BindDN "cn=admin,dc=ienglish,dc=cn" Password Ta9FIjsrlMXcWo Timeout 15 TLSEnable no FollowReferrals no </LDAP> <LDAP> URL ldap://192.168.xxx.xxx:389 BindDN CN=Administrator,CN=Users,DC=XXX,DC=com Password Y%G@$%^F^#UGH Timeout 15 TLSEnable no FollowReferrals no </LDAP> <Authorization> BaseDN "OU=xxxx,DC=going-link,DC=com" SearchFilter "(&(uid=%u)(ou=vpn)" RequireGroup false PasswordIsCR true #<Group> # BaseDN "ou=Groups,dc=example,dc=com" # SearchFilter "(|(cn=ops)(cn=artists))" # MemberAttribute uniqueMember #</Group> </Authorization> EOF #3.配置openvpn的server.conf cat >>/etc/openvpn/server.conf<<EOF plugin /usr/local/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf cn=%u" plugin /usr/local/lib/openvpn/openvpn-otp.so "password_is_cr=1 otp_secrets=/etc/openvpn/auth/otp-secrets" client-cert-not-required #不使用客户端证书,使用密码对 username-as-common-name #使用认证用户名,不使用证书的common name #调用验证脚本,生成OTP共享密钥 script-security 2 auth-user-pass-verify /etc/openvpn/scripts/check_cn_on_connect.sh via-env #服务器端会定期检查认证情况,默认3600秒一小时,使用OTP的话尽量时间长一些,否则客户端需要重新输入用户名密码和OTP一次性密码。 reneg-sec 0 EOF #在service.conf最后一行添加 #4.配置OTP相关内容/etc/openvpn/auth/otp-secrets #格式:bob otp totp:sha1:base32:LJYHR64TUI7IL3RD::xxx * #中间那串LJYHR64TUI7IL3RD就是密钥,熟悉GoogleAuthenticator原理的,可以直接生成专用字符串和二维码。用手机端扫一下就可以用了 #4.客户端配置文件client.ovpn client dev tun proto tcp remote 192.168.0.11 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt ;cert cyh.crt #注释 ;key cyh.key #注释 tls-auth ta.key 1 cipher AES-256-CBC comp-lzo verb 3 auth-user-pass #使用用户名密码登录openvpn服务器 auth-nocache static-challenge "Enter Google Authenticator Token" 1 #@弹出验证框,1表示明文显示,0表示密文显示 #5.重启openvpn systemctl restart openvpn@server
扩展知识
OpenVPN Access Server
VPN Access Server 归属于 OpenVPN Solution,是官方推荐的VPN服务,它介于 可收费与免费之间。
其实这个就是把Openvpn Community开源版做成了更方便,更稳定,优化过了的 商业版。商业版有后台有基于Web的管理面板,也没有各种复杂和编译,而且只 提供2个免费测试的USER,如果需要更多则需要购买许可证5美元/用户。适合个 人自用,不适合多人用。
参考文档: https://openvpn.net/vpn-server-resources/finishing-configuration-of-access-server/
安装 OpenVpn Access Server
#注意:CentOS8 不支持下面包 [root@centos7 ~]#ll openvpn-as-* -rw-r--r-- 1 root root 32742864 Apr 28 20:04 openvpn-as-2.8.3_f28d2eae- CentOS7.x86_64.rpm -rw-r--r-- 1 root root 130562200 Apr 28 20:03 openvpn-as-bundled-clients-8.rpm [root@centos7 ~]#yum -y install openvpn-as-2.8.3_f28d2eae-CentOS7.x86_64.rpm openvpn-as-bundled-clients-8.rpm To reconfigure manually, use the /usr/local/openvpn_as/bin/ovpn-init tool. +++++++++++++++++++++++++++++++++++++++++++++++ Access Server 2.8.3 has been successfully installed in /usr/local/openvpn_as Configuration log file has been written to /usr/local/openvpn_as/init.log Access Server Web UIs are available here: Admin UI: https://10.0.0.17:943/admin Client UI: https://10.0.0.17:943/ +++++++++++++++++++++++++++++++++++++++++++++++ Verifying : openvpn-as-2.8.3_f28d2eae-CentOS7.x86_64 1/4 Verifying : MySQL-python-1.2.5-1.el7.x86_64 2/4 Verifying : openvpn-as-bundled-clients-8-1.noarch 3/4 Verifying : cyrus-sasl-2.1.26-23.el7.x86_64 4/4 Installed: openvpn-as.x86_64 0:2.8.3_f28d2eae-CentOS7 openvpn-as- bundled-clients.noarch 0:8-1 Dependency Installed: MySQL-python.x86_64 0:1.2.5-1.el7 cyrus-sasl.x86_64 0:2.1.26-23.el7 Complete! [root@centos7 ~]#getent passwd openvpn openvpn:x:1002:1002::/home/openvpn:/sbin/nologin [root@centos7 ~]#passwd openvpn Changing password for user openvpn. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. #安装包后openvpnas.service 自动启动 [root@centos7 ~]#systemctl status openvpnas.service #查看打开的相关端口 [root@centos7 ~]#ss -ntlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 127.0.0.1:909 *:* users: (("python2",pid=1427,fd=15)) LISTEN 0 50 *:943 *:* users: (("python2",pid=1427,fd=8)) LISTEN 0 128 *:22 *:* users: (("sshd",pid=827,fd=3)) LISTEN 0 100127.0.0.1:25 *:* users: (("master",pid=962,fd=13)) LISTEN 0 1 *:443 *:* users: (("openvpn-openssl",pid=1481,fd=5)) LISTEN 0 50 127.0.0.1:904 *:* users: (("python2",pid=1427,fd=10)) LISTEN 0 50 127.0.0.1:905 *:* users: (("python2",pid=1427,fd=11)) LISTEN 0 50 127.0.0.1:906 *:* users: (("python2",pid=1427,fd=12)) LISTEN 0 50 127.0.0.1:907 *:* users: (("python2",pid=1427,fd=13)) LISTEN 0 50 127.0.0.1:908 *:* users: (("python2",pid=1427,fd=14)) LISTEN 0 128 [::]:22 [::]:* users: (("sshd",pid=827,fd=4)) LISTEN 0 100 [::1]:25 [::]:* users: (("master",pid=962,fd=14))
登录服务器Admin UI 进行管理
https://openvpn-server:943/admin
输入用户openvpn和上面设置的密码可以登录
登录成功
从 Client UI登录客户端连接地址下载相关文件
https://openvpn-server:943/
选择下载相应的软件版本安装
下载软件完成
安装客户端软件
安装完成
5.1.5 运行软件并登录
验证登录
客户端查看登录
服务器查看日志
[root@centos7 ~]#tail /var/log/openvpnas.log -f
一键安装
方案1
脚本分析+google认证
client="client" port="1194" protocol="udp" ip=$(ip -4 addr | grep inet | grep -vE '127(\.[0-9]{1,3}){3}' | cut -d '/' -f 1 | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}' | sed -n "1"p) vpn_subnet="10.8.0.0" group_name="nobody" # qrencode 生成二维码图片。 google‑authenticator PAM 模块,TOTP 二次验证(2FA)。 dnf install -y openvpn openssl ca-certificates tar google-authenticator qrencode iptables easy_rsa_url='https://github.com/OpenVPN/easy-rsa/releases/download/v3.2.5/EasyRSA-3.2.5.tgz' mkdir -p /etc/openvpn/server/easy-rsa/ { wget -qO- "$easy_rsa_url" 2>/dev/null || curl -sL "$easy_rsa_url" ; } | tar xz -C /etc/openvpn/server/easy-rsa/ --strip-components 1 chown -R root:root /etc/openvpn/server/easy-rsa/ cd /etc/openvpn/server/easy-rsa/ # Create the PKI, set up the CA and create TLS key ./easyrsa --batch init-pki # 初始化数据,在当前目录下生成pki ./easyrsa --batch build-ca nopass # 创建CA证书,不带密码, ./easyrsa gen-tls-crypt-key # 生成 OpenVPN tls‑crypt 的静态密钥文件(tls-crypt.key)客户端和服务端共用这一把密钥,对握手数据包做加密 + 认证 # 创建Diffie-Hellman密钥 echo '-----BEGIN DH PARAMETERS----- MIIBCAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz +8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a 87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7 YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi 7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD ssbzSibBsu/6iGtCOGEoXJf//////////wIBAg== -----END DH PARAMETERS-----' > /etc/openvpn/server/dh.pem ln -s /etc/openvpn/server/dh.pem pki/dh.pem # Create certificates and CRL ./easyrsa --batch --days=3650 build-server-full server nopass # 一次性生成**服务端完整证书套件**:服务器证书 + 私钥;不带密码 ./easyrsa --batch --days=3650 build-client-full "$client" nopass # 生成客户端完整证书套件,私钥无密码 ./easyrsa --batch --days=3650 gen-crl # 更新证书吊销列表文件 # Move the stuff we need cp pki/ca.crt pki/private/ca.key pki/issued/server.crt pki/private/server.key pki/crl.pem /etc/openvpn/server cp pki/private/easyrsa-tls.key /etc/openvpn/server/tc.key # 前面生成的 tls‑crypt 共享密钥,重命名 # CRL is read with each client connection, while OpenVPN is dropped to nobody chown nobody:"$group_name" /etc/openvpn/server/crl.pem # Without +x in the directory, OpenVPN can't run a stat() on the CRL file chmod o+x /etc/openvpn/server/ # 生成服务端配置 server.conf echo "local $ip port $port proto $protocol dev tun ca ca.crt cert server.crt key server.key dh dh.pem auth SHA512 tls-crypt tc.key topology subnet server $vpn_subnet 255.255.255.0 ifconfig-pool-persist ipp.txt push "dhcp-option DNS 8.8.8.8" push "dhcp-option DNS 8.8.4.4" " > /etc/openvpn/server/server.conf # 阻止客户端使用 VPN 以外的 DNS 服务器 echo 'push "block-outside-dns"' >> /etc/openvpn/server/server.conf echo "keepalive 10 120 user nobody group $group_name persist-key persist-tun verb 3 crl-verify crl.pem" >> /etc/openvpn/server/server.conf if [[ "$protocol" = "udp" ]]; then echo "explicit-exit-notify" >> /etc/openvpn/server/server.conf fi # 开启ip_forward转发功能 echo 'net.ipv4.ip_forward=1' > /etc/sysctl.d/99-openvpn-forward.conf # Enable without waiting for a reboot or service restart echo 1 > /proc/sys/net/ipv4/ip_forward # Create a service to set up persistent iptables rules iptables_path=$(command -v iptables) ip6tables_path=$(command -v ip6tables) echo "[Unit] After=network-online.target Wants=network-online.target [Service] Type=oneshot ExecStart=$iptables_path -w 5 -t nat -A POSTROUTING -s $vpn_subnet/24 ! -d $vpn_subnet/24 -j SNAT --to $ip ExecStart=$iptables_path -w 5 -I INPUT -p $protocol --dport $port -j ACCEPT ExecStart=$iptables_path -w 5 -I FORWARD -s $vpn_subnet/24 -j ACCEPT ExecStart=$iptables_path -w 5 -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT ExecStop=$iptables_path -w 5 -t nat -D POSTROUTING -s $vpn_subnet/24 ! -d $vpn_subnet/24 -j SNAT --to $ip ExecStop=$iptables_path -w 5 -D INPUT -p $protocol --dport $port -j ACCEPT ExecStop=$iptables_path -w 5 -D FORWARD -s $vpn_subnet/24 -j ACCEPT ExecStop=$iptables_path -w 5 -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT echo "RemainAfterExit=yes [Install] WantedBy=multi-user.target" > /etc/systemd/system/openvpn-iptables.service systemctl enable --now openvpn-iptables.service # 生成客户端配置 get_public_ip=$(grep -m 1 -oE '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' <<< "$(wget -T 10 -t 1 -4qO- "http://ip1.dynupdate.no-ip.com/" || curl -m 10 -4Ls "http://ip1.dynupdate.no-ip.com/")") public_ip="$get_public_ip" [[ -n "$public_ip" ]] && ip="$public_ip" echo "client dev tun proto $protocol remote $ip $port resolv-retry infinite nobind persist-key persist-tun remote-cert-tls server auth SHA512 ignore-unknown-option block-outside-dns verb 3" > /etc/openvpn/server/client-common.txt # client opts in individually via auth-user-pass-optional) client_mfa="Y" gauth_dir="/etc/openvpn/server/google-authenticator" setup_server_mfa () { mkdir -p "$gauth_dir" chown root:root "$gauth_dir" chmod 711 "$gauth_dir" echo '#!/bin/bash # Permit (exit 0) when the client sent no username, or a username with no secret # file (i.e. not MFA-enrolled). Only users WITH a secret file fall through # (exit 1) to the google_authenticator OTP check. [ -z "$PAM_USER" ] && exit 0 [ -f "/etc/openvpn/server/google-authenticator/$PAM_USER" ] || exit 0 exit 1' > /etc/openvpn/server/pam-empty-check.sh chown root:root /etc/openvpn/server/pam-empty-check.sh chmod 755 /etc/openvpn/server/pam-empty-check.sh echo "auth [success=done default=ignore] pam_exec.so quiet /etc/openvpn/server/pam-empty-check.sh auth required pam_google_authenticator.so secret=$gauth_dir/\${USER} user=root account required pam_permit.so" > /etc/pam.d/openvpn echo '#!/bin/bash if [ -e "/etc/openvpn/server/google-authenticator/$common_name" ]; then [ "$username" = "$common_name" ] || exit 1 fi exit 0' > /etc/openvpn/server/verify-mfa.sh chown root:root /etc/openvpn/server/verify-mfa.sh chmod 755 /etc/openvpn/server/verify-mfa.sh # Load the plugin and allow cert-only clients (those without MFA) through if ! grep -q 'openvpn-plugin-auth-pam.so' /etc/openvpn/server/server.conf; then echo "plugin $(find_pam_plugin) openvpn" >> /etc/openvpn/server/server.conf echo "auth-user-pass-optional" >> /etc/openvpn/server/server.conf mfa_restart_needed=1 fi # Bind MFA enforcement to the certificate CN if ! grep -q '^auth-user-pass-verify ' /etc/openvpn/server/server.conf; then echo "script-security 2" >> /etc/openvpn/server/server.conf echo "auth-user-pass-verify /etc/openvpn/server/verify-mfa.sh via-env" >> /etc/openvpn/server/server.conf mfa_restart_needed=1 fi # Issue a session token after the first successful MFA so TLS renegotiations # reuse the token instead of re-prompting for the OTP. 43200s = 12h lifetime. if ! grep -q '^auth-gen-token ' /etc/openvpn/server/server.conf; then echo "auth-gen-token 43200" >> /etc/openvpn/server/server.conf mfa_restart_needed=1 fi } setup_server_mfa # Enable and start the OpenVPN service systemctl enable --now [email protected] # inline 内嵌模式:所有证书密钥打包进单个`.ovpn`,客户端只需要这一个文件,部署最简单。 grep -vh '^#' /etc/openvpn/server/client-common.txt /etc/openvpn/server/easy-rsa/pki/inline/private/"$client".inline > "$script_dir"/"$client".ovpn # Generate a TOTP secret for a client and show the QR code for enrollment enroll_client_mfa () { local client_name="$1" local secret_file="$gauth_dir/$client_name" mkdir -p "$gauth_dir" # Generate a 160-bit secret -> 32-character base32 string (no padding) local secret secret=$(head -c 20 /dev/urandom | base32 | tr -d '=\n') # Write the secret file in google-authenticator format. The quoted lines are # module options: time-based OTP, rate-limit 3 per 30s, +/-3 step window for # clock skew. DISALLOW_REUSE is intentionally omitted so a code can be reused # within its validity window (avoids errors on quick reconnects). { echo "$secret" echo '" RATE_LIMIT 3 30' echo '" WINDOW_SIZE 3' echo '" TOTP_AUTH' } > "$secret_file" chown root:root "$secret_file" chmod 600 "$secret_file" # The secret file's existence is what marks this client as MFA-enrolled; # the server's verify script keys off it. No separate list to maintain. # Label is "issuer:account" so the authenticator app shows both the OpenVPN # issuer and the client name, making each entry easy to tell apart local uri="otpauth://totp/OpenVPN:${client_name}?secret=${secret}&issuer=OpenVPN" echo echo "Scan this QR code in your authenticator app (Google Authenticator, etc.):" echo printf '%s' "$uri" | qrencode -t ANSIUTF8 echo "Or enter this secret manually: $secret" echo "otpauth URI: $uri" echo echo "When connecting, enter the current 6-digit code as the password." echo } if [[ "$client_mfa" =~ ^[yY]$ ]]; then printf '<auth-user-pass>\n%s\n</auth-user-pass>\n' "$client" >> "$script_dir"/"$client".ovpn enroll_client_mfa "$client" else printf '<auth-user-pass>\n%s\nnone\n</auth-user-pass>\n' "$client" >> "$script_dir"/"$client".ovpn fi # 完毕 # 新增加客户端 cd /etc/openvpn/server/easy-rsa/ ./easyrsa --batch --days=3650 build-client-full "$client" nopass grep -vh '^#' /etc/openvpn/server/client-common.txt /etc/openvpn/server/easy-rsa/pki/inline/private/"$client".inline > "$script_dir"/"$client".ovpn printf '<auth-user-pass>\n%s\n</auth-user-pass>\n' "$client" >> "$script_dir"/"$client".ovpn enroll_client_mfa "$client" # 删除客户端 cd /etc/openvpn/server/easy-rsa/ ./easyrsa --batch revoke "$client" ./easyrsa --batch --days=3650 gen-crl rm -f /etc/openvpn/server/crl.pem rm -f /etc/openvpn/server/easy-rsa/pki/reqs/"$client".req rm -f /etc/openvpn/server/easy-rsa/pki/private/"$client".key # Drop the client's TOTP secret; this also un-enrolls it from MFA rm -f "$gauth_dir/$client" cp /etc/openvpn/server/easy-rsa/pki/crl.pem /etc/openvpn/server/crl.pem # CRL is read with each client connection, when OpenVPN is dropped to nobody chown nobody:"$group_name" /etc/openvpn/server/crl.pem echo echo "$client revoked!"
方案2
安装openvpn和ldap认证
#1.安装openvpn和ldap认证 curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh chmod +x openvpn-install.sh ./openvpn-install.sh install # /etc/openvpn/server/client-template.txt #安装过程 #确定服务器 IP 后按 Enter #使用 IPv6 ? 填 n (否) 后按 Enter #OpenVPN port ,选 1) default 1194 后按 Enter #传输协定类 UDP / TCP,选 1 UDP 后按 Enter #选 DNS 服务器,一般都会选 3.CloudFalre 或者9. Google,选完后按 Enter #连线数据压缩,不建议使用,填 n 后按 Enter #自定加密方式,如果不是高手,就填 n 后按 Enter,高手的话可以自己选 AES 加密长度及方式。 #Press any key to countinue … 按 Enter后开始安装 #运行完成后会要求你加入第一个用户 ( Client name: ),只可以填英文字串,如 edwardclient填完后按 Enter #是否用密码加密金钥 ? 一般我会选 2 ,自己输入密码,填完整个安装过程就完成了。 yum install openvpn-auth-ldap -y #2.配置ldap和openvpn服务端 #配置ldap cd /etc/openvpn/auth/ cp ldap.conf ldap.conf.old cat <<\EOF > /etc/openvpn/auth/ldap.conf <LDAP> URL ldap://10.16.202.178:389 BindDN "cn=admin,dc=ienglish,dc=cn" Password Ta9FIjsrlMXcWo Timeout 15 TLSEnable no FollowReferrals no </LDAP> <Authorization> BaseDN "ou=user,dc=iXX,dc=cn" SearchFilter "(&(uid=%u)(ou=vpn))" RequireGroup false # RequireGroup true # <Group> # BaseDN "ou=group,dc=iXX,dc=cn" # SearchFilter "cn=ops" # MemberAttribute member # </Group> </Authorization> EOF #修改 openvpn 服务端配置文件 cat <<\EOF> /etc/openvpn/server/server.conf port 1194 proto tcp dev tun user nobody group nobody keepalive 10 120 topology subnet server 10.251.254.0 255.255.255.0 push "dhcp-option DNS 114.114.114.114" push "dhcp-option DNS 223.5.5.5" #push "redirect-gateway def1 bypass-dhcp" #启用后,客户端所有流量都将通过VPN服务器 push "route 10.64.0.0 255.255.0.0" #给客户端生成的到达服务器后面网段的静态路由,下一跳为openvpn服务器的10.251.254.1 push "route 172.28.0.0 255.255.0.0" push "route 172.17.0.0 255.255.0.0" push "route 10.16.0.0 255.255.0.0" push "route 10.0.0.0 255.255.0.0" dh none ecdh-curve prime256v1 tls-crypt tls-crypt.key 0 crl-verify crl.pem ca ca.crt cert server_toHbMLW61cV2DLVL.crt key server_toHbMLW61cV2DLVL.key auth SHA256 cipher AES-128-GCM ncp-ciphers AES-128-GCM tls-server tls-version-min 1.2 tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256 status /var/log/openvpn/status.log verb 3 log-append /var/log/openvpn.log log /var/log/openvpn.log plugin /usr/lib64/openvpn/plugin/lib/openvpn-auth-ldap.so "/etc/openvpn/auth/ldap.conf cn=%u" verify-client-cert #client-cert-not-required reneg-sec 0 # 关闭默认的 3600s 客户端连接断开时间 duplicate-cn # 允许多用户登录 management 127.0.0.1 5555 #本地管理 EOF #3.启动服务 systemctl restart [email protected] #4. 客户端配置 cat << \EOF >clientVPN.ovpn client proto tcp remote ovpn.ops.ienglish.cn 1194 dev tun resolv-retry infinite nobind persist-key persist-tun remote-cert-tls server verify-x509-name server_toHbMLW61cV2DLVL name auth SHA256 auth-nocache cipher AES-128-GCM tls-client tls-version-min 1.2 tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256 verb 3 reneg-sec 0 auth-user-pass <ca> -----BEGIN CERTIFICATE----- xxx -----END CERTIFICATE----- </ca> <cert> -----BEGIN CERTIFICATE----- xxx -----END CERTIFICATE----- </cert> <key> -----BEGIN PRIVATE KEY----- xxx -----END PRIVATE KEY----- </key> <tls-crypt> # # 2048 bit OpenVPN static key # -----BEGIN OpenVPN Static key V1----- xxx -----END OpenVPN Static key V1----- </tls-crypt> EOF #查询 ldapsearch -x -D "cn=admin,dc=iXX,dc=cn" -w Ta9FIjsrlMXcWo -b "ou=user,dc=iXX,dc=cn" ou=vpn |grep uid iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE iptables -A INPUT -i tun0 -j ACCEPT iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT iptables -A INPUT -i eth0 -p udp --dport 1194 -j ACCEPT # Add a client ./openvpn-install.sh client add alice # List clients ./openvpn-install.sh client list # Revoke a client (immediately disconnects if connected) ./openvpn-install.sh client revoke alice # Renew a client certificate ./openvpn-install.sh client renew bob --cert-days 365 # Renew server certificate ./openvpn-install.sh server renew # Show connected clients (data refreshes every 60 seconds): ./openvpn-install.sh server status
监控
openvpn-monitor
openvpn-monitor是一个简单的python程序,用于生成html,显示OpenVPN服务器的状态,包括所有当前连接。
Gitis当前可用的源代码:
https://github.com/furlongm/openvpn-monitor
安装
#1.安装监控工具 yum install -y epel-release virtualenv + pip + gunicorn # apt-get install python-virtualenv geoip-database-extra geoipupdate # (debian/ubuntu) # yum install python-virtualenv GeoIP-update geolite2-city python2-geoip2 # (centos/rhel) mkdir /data/openvpn-monitor cd /data/openvpn-monitor virtualenv . . bin/activate pip install --upgrade pip pip install openvpn-monitor gunicorn gunicorn openvpn-monitor -b 0.0.0.0:80 # cat /data/openvpn-monitor/openvpn-monitor.conf [openvpn-monitor] site=Top_Learning #logo=logo.jpg #latitude=40.72 #longitude=-74 maps=False #geoip_data=/var/lib/GeoIP/GeoLite2-City.mmdb geoip_data=/data/openvpn-monitor/GeoIP/GeoLite2-City.mmdb datetime_format=%d/%m/%Y %H:%M:%S [VPN1] host=localhost port=5555 name=TOPE-VPN show_disconnect=False #2.配置openvpn的server.conf cat >>/etc/openvpn/server.conf<<EOF management 127.0.0.1 5555 #本地管理 EOF #在service.conf最后一行添加 #3.重启openvpn systemctl restart openvpn@server
访问对应80端口
二级 OpenVPN 搭建方案
比如,有集群A(172.31.0.0/16)、集群B(172.18.0.0/16)、受限服务器C(11.11.11.11)、入口服务器D(22.22.22.22)。 目标:本地机器通过 D 连接到 A、B、C。
方案如下:
- 准备上游环境:
- 集群 A 和 B 分别准备一台公网服务器 A1 和 B1
- 分别使用 openvpn-install.sh 脚本安装 OpenVPN 服务:
- 安装选项:
- A1 服务器用 10.8.0.0/24 网段
- B1 服务器用 10.8.1.0/24 网段
- 配置服务端:~vi /etc/openvpn/server/server.conf~
- 移除:
push "redirect-gateway def1 bypass-dhcp"(这条用来通知客户端把默认网关改成VPN网关)push "dhcp-option DNS 8.8.8.8"(这条用来通知客户端配置指定DNS解析服务器)push "dhcp-option DNS 8.8.4.4"push "block-outside-dns"(这条用来通知客户端禁止请求外部DNS)
- A1 添加:
push "route 172.31.0.0 255.255.0.0"(路由 A 集群内网IP)
- B1 添加:
push "route 172.18.0.0 255.255.0.0"(路由 B 集群内网IP)
- 移除:
- 重启服务端:
systemctl restart [email protected]
- 安装选项:
- 分别使用 openvpn-install.sh 脚本生成客户端配置:
- A1 服务器生成 a-bridge.ovpn
- B1 服务器生成 b-bridge.ovpn
- A1、B1、C 的IP白名单都添加 D 服务器
- 准备下游环境(服务器 D):
- 使用 openvpn-install.sh 脚本安装 OpenVPN 服务:
- 安装选项:
- 使用 10.8.100.0/24 网段
- 配置服务端:~vi /etc/openvpn/server/server.conf~
- 移除:
push "redirect-gateway def1 bypass-dhcp"(这条用来通知客户端把默认网关改成VPN网关)push "dhcp-option DNS 8.8.8.8"(这条用来通知客户端配置指定DNS解析服务器)push "dhcp-option DNS 8.8.4.4"push "block-outside-dns"(这条用来通知客户端禁止请求外部DNS)
- 添加:
push "route 172.31.0.0 255.255.0.0"(路由 A 集群内网IP)push "route 172.18.0.0 255.255.0.0"(路由 B 集群内网IP)push "route 11.11.11.11 255.255.255.255"(路由 C 服务器IP)
- 移除:
- 重启服务端:
systemctl restart [email protected]
- 配置客户端:
- 将 a-bridge.ovpn 和 b-bridge.ovpn 配置文件后缀名改为
.conf并移动到/etc/openvpn/client/目录下 - 移除:
dev tun
- 添加(a-bridge.conf):
dev vpn0dev-type tun
- 添加(b-bridge.conf):
dev vpn1dev-type tun
- 将 a-bridge.ovpn 和 b-bridge.ovpn 配置文件后缀名改为
- 启动客户端:
systemctl enable --now [email protected]systemctl enable --now [email protected]
- 配置路由:
- 先暂停:
systemctl stop openvpn-iptables.service
- 修改配置:~vi /etc/systemd/system/openvpn-iptables.service~
- 移除:
ExecStart=/usr/sbin/iptables -w 5 -t nat -A POSTROUTING -s 10.9.0.0/24 ! -d 10.9.0.0/24 -j SNAT --to ......ExecStop=/usr/sbin/iptables -w 5 -t nat -D POSTROUTING -s 10.9.0.0/24 ! -d 10.9.0.0/24 -j SNAT --to ......
- 添加:
ExecStart=/usr/sbin/iptables -w 5 -t nat -A POSTROUTING -s 10.9.0.0/24 -o eth0 -j MASQUERADE(eth0 改成实际网络出口接口名称)ExecStart=/usr/sbin/iptables -w 5 -t nat -A POSTROUTING -s 10.9.0.0/24 -o vpn0 -j MASQUERADEExecStart=/usr/sbin/iptables -w 5 -t nat -A POSTROUTING -s 10.9.0.0/24 -o vpn1 -j MASQUERADEExecStop=/usr/sbin/iptables -w 5 -t nat -D POSTROUTING -s 10.9.0.0/24 -o eth0 -j MASQUERADE(eth0 改成实际网络出口接口名称)ExecStop=/usr/sbin/iptables -w 5 -t nat -D POSTROUTING -s 10.9.0.0/24 -o vpn0 -j MASQUERADEExecStop=/usr/sbin/iptables -w 5 -t nat -D POSTROUTING -s 10.9.0.0/24 -o vpn1 -j MASQUERADE
- 移除:
- 启用配置:
systemctl daemon-reloadsystemctl start openvpn-iptables.service
- 检查路由:
iptables -t nat -L POSTROUTING -n -v(检查 nat 表的 POSTROUTING 链是否残留旧配置,是否存在新配置)
- 测试 A、B、C 网络连通性:
ping 172.31.0.1/nc -vz 172.31.11.11 22ping 172.18.0.1/nc -vz 172.18.11.11 22ping 11.11.11.11/nc -vz 11.11.11.11 22
- 先暂停:
- 安装选项:
- 使用 openvpn-install.sh 脚本生成客户端配置
- 生成 d-client.ovpn
- 使用 openvpn-install.sh 脚本安装 OpenVPN 服务:
- 本地测试:
- 导入 d-client.ovpn 配置并连接
- 测试 A、B、C 网络连通性:
ping 172.31.0.1/nc -vz 172.31.11.11 22ping 172.18.0.1/nc -vz 172.18.11.11 22ping 11.11.11.11/nc -vz 11.11.11.11 22