OpenVPN
1. OpenVPN介绍
VPN直译就是虚拟专用通道,是提供给企业之间或者个人与公司之间安全数据传输的隧道,OpenVPN无疑是Linux下开源VPN的先锋,提供了良好的性能和友好的用户GUI。
OpenVPN 是一个基于 OpenSSL 库的应用层 VPN 实现。和传统 VPN 相比,它的优点是简单易用。
2. OpenVPN应用场景
Peer-to-Peer VPN(点对点连接),将Internet两台机器(公网地址)使用VPN连接起来,比如上海服务器和北京服务器之间的数据需要相互调用,但是数据有比较敏感,直接通过http公共传输,容易被窃取。如果拉一条专线成本又太高。那么我们可以通过VPN使用现有网络,将两台主机逻辑上捆绑在一个虚拟网络中,这样既保证了数据安全传输,同时又节省了成本。
Slte-to-Slte VPN(站点对站点),用于连接两个或者多个地域上不同的局域网LAN,每个LAN有一台OpenVPN服务器作为接入点,组成虚拟专用网络,使得不同LAN里面的主机和服务器都能够相互通讯(比如国内公司与海外分公司的连接)
Remote Access VPN(远程访问),应用于外网用户访问内部资源。在这个场景中远程访问者一般通过公网IP连接VPN服务,然后通过分配后的内网地址与内网网段进行通信。
3. OpenVPN场景应用
地址规划:
服务端: 10.0.0.61 172.16.1.61
客户端: windows 10.0.0.1
[root@openvpn ~]
[root@openvpn ~]
Disabled
[root@openvpn ~]
Fri Mar 20 09:49:43 CST 2020
1.下载证书软件
[root@openvpn ~]
2.准备证书目录及vars文件
[root@openvpn ~]
[root@openvpn ~]
[root@openvpn /opt/easy-rsa]
[root@openvpn /opt/easy-rsa]
[root@openvpn /opt/easy-rsa]
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 "Shanghai"
set_var EASYRSA_REQ_CITY "Shanghai"
set_var EASYRSA_REQ_ORG "gcc"
set_var EASYRSA_REQ_EMAIL "123456@qq.com"
set_var EASYRSA_NS_SUPPORT "yes"
3.初始化证书,
[root@openvpn /opt/easy-rsa]
[root@openvpn /opt/easy-rsa]
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Enter New CA Key Passphrase:
Re-Enter New CA Key Passphrase:
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:
/opt/easy-rsa/pki/ca.crt
[root@openvpn /opt/easy-rsa]
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Generating a 2048 bit RSA private key
................................................+++
.......................................................................................................................................................................................+++
writing new private key to '/opt/easy-rsa/pki/private/server.key.1FeUnEIles'
-----
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: /opt/easy-rsa/pki/reqs/server.req
key: /opt/easy-rsa/pki/private/server.key
[root@openvpn /opt/easy-rsa]
Note: using Easy-RSA configuration from: ./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 1080 days:
subject=
commonName = server
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes
Using configuration from /opt/easy-rsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /opt/easy-rsa/pki/private/ca.key:
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 Mar 5 02:22:30 2023 GMT (1080 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /opt/easy-rsa/pki/issued/server.crt
#5.创建Diffie-Hellman文件,秘钥交换时的Diffie-Hellman算法
[root@openvpn /opt/easy-rsa]# ./easyrsa gen-dh
#6.创建client端证书和私钥文件,nopass表示不加密私钥文件,其他可默认
[root@openvpn /opt/easy-rsa]# ./easyrsa gen-req client nopass
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
Generating a 2048 bit RSA private key
.......................................................................................................+++
......................................................................................................................................................................+++
writing new private key to '/opt/easy-rsa/pki/private/client.key.xBKxgsSroC'
-----
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) [client]: #回车
Keypair and certificate request completed. Your files are:
req: /opt/easy-rsa/pki/reqs/client.req
key: /opt/easy-rsa/pki/private/client.key
#7.给client端证书签名,首先是对一些信息的确认,可以输入yes,然后创建ca根证书时设置的密码
[root@openvpn /opt/easy-rsa]# ./easyrsa sign client client
Note: using Easy-RSA configuration from: ./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 1080 days:
subject=
commonName = client
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes #输入yes
Using configuration from /opt/easy-rsa/pki/safessl-easyrsa.cnf
Enter pass phrase for /opt/easy-rsa/pki/private/ca.key: #输入密码 1234
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'client'
Certificate is to be certified until Mar 5 02:27:01 2023 GMT (1080 days)
Write out database with 1 new entries
Data Base Updated
Certificate created at: /opt/easy-rsa/pki/issued/client.crt
4. 安装openVPN
[root@openvpn /opt/easy-rsa]
5. 配置openVPN
[root@openvpn /opt/easy-rsa]
[root@openvpn /etc/openvpn]
total 0
drwxr-x--- 2 root openvpn 6 2019-11-01 20:11 client
drwxr-x--- 2 root openvpn 6 2019-11-01 20:11 server
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh 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
keepalive 10 120
max-clients 100
status openvpn-status.log
verb 3
client-to-client
log /var/log/openvpn.log
persist-key
persist-tun
duplicate-cn
6. 将vpn server端用的证书文件拷贝到openVPN目录中
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
total 24
-rw------- 1 root root 1172 2020-03-20 10:59 ca.crt
drwxr-x--- 2 root openvpn 6 2019-11-01 20:11 client
-rw------- 1 root root 424 2020-03-20 11:12 dh.pem
drwxr-x--- 2 root openvpn 6 2019-11-01 20:11 server
-rw-r--r-- 1 root root 322 2020-03-20 11:11 server.conf
-rw------- 1 root root 4800 2020-03-20 10:59 server.crt
-rw------- 1 root root 1704 2020-03-20 10:59 server.key
7. 开启内核转发
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
net.ipv4.ip_forward = 1
8. 启动openvpn
[root@openvpn /etc/openvpn]
Created symlink from /etc/systemd/system/multi-user.target.wants/openvpn@server.service to /usr/lib/systemd/system/openvpn@.service.
[root@openvpn /etc/openvpn]
[root@openvpn /etc/openvpn]
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
udp 0 0 0.0.0.0:1194 0.0.0.0:* 9324/openvpn
udp 0 0 0.0.0.0:648 0.0.0.0:* 5986/rpcbind
udp6 0 0 :::111 :::* 1/systemd
udp6 0 0 :::648 :::* 5986/rpcbind
windows客户部署
4. 客户端部署
1.windows
1.下载windows的openvpn软件
下载之后,进行默认安装就可以。需要翻墙下载。
2.下载服务端生成的客户端密钥文件和ca文件至windows指定C:\Program Files\OpenVPN\config 目录中
[root@m01 ~]
[root@m01 openvpn]
[root@m01 openvpn]
[root@m01 openvpn]
3.在C:\Program Files\OpenVPN\config 创建一个客户端配置文件,名称叫client.ovpn,内容如下:
client
dev tun
proto udp
remote 10.0.0.61 1194
resolv-retry infinite
nobind
ca ca.crt
cert client.crt
key client.key
verb 3
persist-key
persist-tun
4.双击运行openvpn,然后连接即可。
5.客户端测试是否能连接VPN服务端内网地址
经过测试,是可以连通VPN服务端内网地址,但是不能连接服务器其它的内网地址。
6.Windows查看推送过来的路由信息
route print -4
2.linux端
1.安装openvpn
[root@openvpn-client ~]
2.下载证书文件
[root@openvpn-client ~]
[root@openvpn-client openvpn]
[root@openvpn-client openvpn]
[root@openvpn-client openvpn]
3.配置客户端
[root@openvpn-client openvpn]
client
dev tun
proto udp
remote 10.0.0.61 1194
resolv-retry infinite
nobind
ca ca.crt
cert client.crt
key client.key
verb 3
persist-key
persist-tun
4.启动Linux客户端的openvpn
[root@openvpn-client ~]
5.进行测试连接
[root@openvpn-client openvpn]
3.mac端
Mac安装openvpn参考文档
https://www.jianshu.com/p/a5fd8dc95ad4
https://www.cnblogs.com/airoot/p/7252987.html
如果是Mac电脑,需要将所有的文件放入一个文件夹中,然后将文件夹的名称修改为talk
5. OpenVPN访问内网网段
[root@web01 ~]
[root@openvpn ~]
[root@openvpn ~]
success
[root@openvpn ~]
success
[root@openvpn ~]
success
[root@openvpn ~]
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client openvpn
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
进行客户端重启
6. 双重认证登录
Openvpn秘钥+用户名密码双重身份验证登录
为什么需要用户名密码验证登录,我们已经使用了CA证书、交换秘钥这几种方式进行加密了,可以说已经很安全了,为什么还要需要用户名密码呢。
首先安全还是很重要的,其次就是管理这些秘钥和证书还是比较麻烦的,如果用户量比较多,我们不可能为每个用户都创建一套加密,每个用户创建一个秘钥比较麻烦,但多人使用一个秘钥又不具有唯一性,比如说有用户不在需要VPN的时候,我们就只能吊销证书。但是如果多人使用一个秘钥的情况下,吊销证书了,其他的用户也登录不了。所以我们就需要秘钥加用户名和密码,这样就可以多个用户使用同一个证书,使用不同的用户名和密码。
新用户加入的时候,只需要添加一个用户名和密码,如果有人不需要VPN时,直接删除用户名和密码就可以了。
首先需要确保能使用秘钥成功连接openVPN服务,然后再配置秘钥+用户名密码验证登录方式
配置如下:
1. 修改服务端配置文件
[root@openvpn /etc/openvpn]
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh 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
keepalive 10 120
max-clients 100
status openvpn-status.log
verb 3
client-to-client
log /var/log/openvpn.log
persist-key
persist-tun
duplicate-cn
script-security 3
auth-user-pass-verify /etc/openvpn/check.sh via-env
username-as-common-name
2.准备脚本
[root@openvpn /etc/openvpn]
#!/bin/sh
PASSFILE="/etc/openvpn/openvpnfile"
LOG_FILE="/var/log/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
[root@openvpn /etc/openvpn]
qls 123456
[root@openvpn /etc/openvpn]
配置文件添加此行
auth-user-pass
重启客户端
[root@openvpn /etc/openvpn]
2020-03-20 14:54:28: Successful authentication: username="qls".
2020-03-20 14:56:53: User does not exist: username="qls", password="123456".
2020-03-20 14:57:07: User does not exist: username="qls", password="123456".
2020-03-20 14:57:18: User does not exist: username="qls", password="123456".
2020-03-20 14:58:13: Successful authentication: username="oldboy".