@TOC
1.环境说明
- 系统平台:Centos7.6
- openVPN版本:2.4.12
- easy-rsa版本:3.0.8
- iptables-services:1.4.21
2.安装软件
- 安装 epel 源
yum -y install epel-release
- 安装核心软件
yum -y install openvpn easy-rsa iptables-services
3.制作证书
使用 easy-rsa 生成需要的证书及相关文件,在这个阶段会产生一些 key 和证书:
- CA根证书
- openvpn 服务器ssl证书
- openvpn 客户端ssl证书
- Diffie-Hellman 算法用到的 key
1.移动到统一管理目录
将 easy-rsa 脚本复制到 /etc/openvpn/,该脚本主要用来方便地生成 CA 证书和各种 key
mkdir -p /etc/openvpn/easy-rsa/
cp -a /usr/share/easy-rsa/3.0.8/* /etc/openvpn/easy-rsa/
2.配置环境变量
跳到 easy-rsa 目录并编辑 vars 文件,添加一些生成证书时用到的变量。
$ cd /etc/openvpn/easy-rsa
$ vim vars # 没这个文件的话新建,填写如下内容(变量值根据实际情况随便填写):
export KEY_COUNTRY="CN"
export KEY_PROVINCE="ZJ"
export KEY_CITY="Hangzhou"
export KEY_ORG="ERYAJF, Inc"
export KEY_EMAIL="Linuxlql@163.com"
export KEY_CN=vpn.eryajf.net
export KEY_NAME=vpnserver
export KEY_OU=OPS
$ source ./vars # 使变量生效
3.生成 CA 根证书
初始化pki目录,待会儿生成的证书都会放到这个目录下。
./easyrsa init-pki
Note: using Easy-RSA configuration from: ./vars
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki
生成 CA 根证书, 输入那里直接回车就行。
./easyrsa build-ca nopass
Note: using Easy-RSA configuration from: ./vars
Using SSL: openssl OpenSSL 1.0.2k-fips 26 Jan 2017
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]:eryajf-vpn
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
4.生成服务端证书
第一个参数 server 为证书名称,可以随便起
./easyrsa build-server-full server 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 '/etc/openvpn/easy-rsa/pki/private/server.key.bF3aSjbwlQ'
-----
Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf
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 16 09:03:45 2023 GMT (1080 days)
Write out database with 1 new entries
Data Base Updated
5.生成客户端证书
生成客户端请求文件,客户端私钥文件
./easyrsa gen-req client nopass
......
Keypair and certificate request completed. Your files are:
req: /opt/easy-rsa/pki/reqs/chenleilei.req ##客户端请求文件
key: /opt/easy-rsa/pki/private/chenleilei.key ##客户端私钥文件
给客户端的证书签名
[root@iZ8vb2567465ubejdyjpboZ easy-rsa]# ./easyrsa sign client client
Note: using Easy-RSA configuration from: ./vars
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:
subject=
commonName = client
Type the word 'yes' to continue, or any other input to abort.
Confirm request details: yes ## 输入yes
生成客户端文件: /opt/easy-rsa/pki/issued/client.crt
6.生成 Diffie-Hellman 算法需要的密钥文件
创建 Diffie-Hellman,这可能得等一小会儿。
./easyrsa gen-dh
Note: using Easy-RSA configuration from: ./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
7.生成 tls-auth key(这个文件最后没用到配置文件上,不知道为啥会报错)
这个 key 主要用于防止 DoS 和 TLS 攻击,这一步其实是可选的,但为了安全还是生成一下,该文件在后面配置 open VPN 时会用到。
openvpn --genkey --secret ta.key
8.移动到统一管理目录
将上面生成的相关证书文件整理到 /etc/openvpn/server/certs (这一步完全是为了维护方便)
mkdir /etc/openvpn/server/certs && cd /etc/openvpn/server/certs
cp /etc/openvpn/easy-rsa/pki/dh.pem ./
cp /etc/openvpn/easy-rsa/pki/ca.crt ./
cp /etc/openvpn/easy-rsa/pki/issued/server.crt ./
cp /etc/openvpn/easy-rsa/pki/private/server.key ./
cp /etc/openvpn/easy-rsa/ta.key ./
9.创建 open VPN 日志目录
mkdir -p /var/log/openvpn/
chown openvpn:openvpn /var/log/openvpn
4.配置 OpenVPN
可以从 /usr/share/doc/openvpn-/sample/sample-config-files 复制一份 demo 到 /etc/openvpn/(openvpn 版本号查看:yum info openvpn。)然后改改,或者从头开始创建一个新的配置文件。
cd /etc/openvpn/
touch server.conf && vim server.conf
填入如下内容(很多配置项不需要特别了解,重要的配置这里注释出来了,其他相关配置项想了解的话见 这里 :
# 监听的端口号
port 1194
# 服务端用的协议,udp 能快点,所以我选择 udp
proto udp
# 定义openvpn运行时使用哪一种模式,openvpn有两种运行模式一种是tap模式,一种是tun模式。
# tap模式也就是桥接模式,通过软件在系统中模拟出一个tap设备,该设备是一个二层设备,同时支持链路层协议。
# tun模式也就是路由模式,通过软件在系统中模拟出一个tun路由,tun是ip层的点对点协议。
dev tun
# 定义openvpn在使用tun路由模式时,分配给client端分配的IP地址段
server 10.106.0.0 255.255.255.0
# 这条命令可以重定向客户端的网关,在进行翻墙时会使用到
push "redirect-gateway def1 bypass-dhcp"
# 向客户端推送的路由信息,如下内容表示客户端连接之后与当前互通
push "route 10.0.0.0 255.255.0.0"
# 向客户端推送的DNS信息
push "dhcp-option DNS 223.5.5.5"
push "dhcp-option DNS 223.6.6.6"
push "dhcp-option DNS 114.114.114.114"
# 定义活动连接保时期限
keepalive 10 300
# 加密类型
cipher AES-256-CBC
# 启用允许数据压缩,客户端配置文件也需要有这项
comp-lzo
# 最多允许连接1000个客户端
max-clients 1000
# 通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys
persist-key
# 通过keepalive检测超时后,重新启动VPN,一直保持tun或者tap设备是linkup的。否则网络连接,会先linkdown然后再linkup
persist-tun
# 指定日志文件的记录详细级别,可选0-9,等级越高日志内容越详细
verb 4
# 重复日志记录限额
mute 20
# 禁用TSL重协商
reneg-sec 0
# 此选项开启只能使用udp协议。否则会报错error: --explicit-exit-notify can only be used with --proto udp
explicit-exit-notify 1
# 客户端1,服务端是0
key-direction 0
# 2.4版本之前使用 tls-auth,如果高于此版本,则用tls-crypt,我用的2.4.8使用了tls-auth,结果连通之后无法访问外网了
# tls-crypt /etc/openvpn/server/certs/ta.key 0
# 记录客户端和虚拟ip地址之间的关系。在openvpn重启时,再次连接的客户端将依然被分配和断开之前的IP地址
# 需要注释掉,不然连接的所有客户端ip相同
# ifconfig-pool-persist /etc/openvpn/ipp.txt
# 让所有客户端Ip不同
--duplicate-cn
# 使用客户提供的UserName作为Common Name
username-as-common-name
# 基于mysql进行认证,如不需要可注释掉,注意最后的openvpn_mysql是与后边配置文件名称相呼应的
# plugin /etc/openvpn/openvpn-plugin-auth-pam.so openvpn_mysql
# CA 根证书路径
ca /etc/openvpn/server/certs/ca.crt
# open VPN 服务器证书路径
cert /etc/openvpn/server/certs/server.crt
# open VPN 服务器密钥路径
key /etc/openvpn/server/certs/server.key
# Diffie-Hellman 算法密钥文件路径
dh /etc/openvpn/server/certs/dh.pem
# 指定 log 文件位置
log /var/log/openvpn/server.log
log-append /var/log/openvpn/server.log
status /var/log/openvpn/status.log
5.防火墙相关配置
1.防火墙
禁用 Centos7 默认的 firewalld,使用经典的 iptables 防火墙管理软件:
systemctl stop firewalld
systemctl mask firewalld
2.禁用 SELinux
马上关闭:setenforce 0 临时生效
永久关闭:sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config | 需要重启服务器生效
3.启用 iptables
systemctl enable iptables
systemctl start iptables
iptables -F # 清理所有防火墙规则
4,添加防火墙规则
将 openvpn 的网络流量转发到公网:snat 规则
# 如下网段记得与server.conf 当中定义的网段保持一致
iptables -t nat -A POSTROUTING -s 10.106.0.0/24 -o eth0 -j MASQUERADE
iptables -L -t nat
iptables-save > /etc/sysconfig/iptables # iptables 规则持久化保存
5,Linux 服务器启用地址转发
echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
sysctl -p # 这一步一定得执行,否则不会立即生效。
6,安全组
如果是云服务器,还需要注意将当前服务器安全组的 1194 端口开放,而确实 udp 的协议。
6.启动 open VPN 测试
测试能正常跑先关闭
systemctl start(stop,restart) openvpn@server # 启动
systemctl enable openvpn@server # 开机自启动
systemctl status openvpn@server # 查看服务状态
运行成功!
7.配置数据库
1,安装数据库。
yum -y install mariadb mariadb-server
systemctl enable mariadb
systemctl restart mariadb
mysql_secure_installation # 初始化root账号密码就行了
2.初始化数据。
创建数据库
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS openvpn DEFAULT CHARSET utf8;
创建连接账号
MariaDB [(none)]> grant all on openvpn.* to vpnadmin@'localhost' identified by 'vpn123456';
创建用户表
MariaDB [(none)]> use openvpn;
Database changed
MariaDB [openvpn]> create table vpnuser(name char(100)not null,password char(255)default null,active int(10)not null default 1,primary key(name));
# 查看
MariaDB [openvpn]> desc vpnuser;
+----------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| name | char(100) | NO | PRI | NULL | |
| password | char(255) | YES | | NULL | |
| active | int(10) | NO | | 1 | |
+----------+-----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
创建日志表
MariaDB [openvpn]> create table loginlog(msg char (254),user char(100),pid char(100),host char(100),rhost char(100),time char(100));
# 查看
MariaDB [openvpn]> desc loginlog;
+-------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| msg | char(254) | YES | | NULL | |
| user | char(100) | YES | | NULL | |
| pid | char(100) | YES | | NULL | |
| host | char(100) | YES | | NULL | |
| rhost | char(100) | YES | | NULL | |
| time | char(100) | YES | | NULL | |
+-------+-----------+------+-----+---------+-------+
6 rows in set (0.00 sec)
插入两个测试用户
MariaDB [openvpn]> insert into vpnuser (name,password) values ('test1',password('123456'));
Query OK, 1 row affected (0.00 sec)
MariaDB [openvpn]> insert into vpnuser (name,password) values ('test2',password('654321'));
Query OK, 1 row affected (0.00 sec)
# 查看用户
# 第一列为登录vpn的用户名,第二列为登录的密码,第三列是是否被禁用,1为启用,改为0表示不能登录。
MariaDB [openvpn]> select * from vpnuser;
+-------+-------------------------------------------+--------+
| name | password | active |
+-------+-------------------------------------------+--------+
| test1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1 |
| test2 | *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 | 1 |
+-------+-------------------------------------------+--------+
2 rows in set (0.00 sec)
8.配置认证
1.安装认证软件
rpm -Uvh http://www.nosuchhost.net/~cheese/fedora/packages/epel-7/x86_64/cheese-release-7-1.noarch.rpm
yum -y install pam_krb5 pam pam_devel gcc gcc-c++ cyrus-sasl autoconf
千万注意:pam_mysql不能直接用yum install pam_mysql安装,系统自带的版本是0.7的,使用这个版本的话会导致后边OpenVPN连接的时候认证不成功
安装pam_mysql包
rpm -ivh http://repo.iotti.biz/CentOS/7/x86_64/pam_mysql-0.8.1-0.22.el7.lux.x86_64.rpm
saslauthd 是一个利用 sasl 资料库进行明文身份验证的守护进程,等会儿配置完毕之后将通过这个工具进行命令行的简单验证。 这步操作之后,也会生成依赖库文件 /usr/lib64/security/pam_mysql.so,后边会提到这个模块。
systemctl enable saslauthd
systemctl restart saslauthd
ls /usr/lib64/security/pam_mysql.so
2.配置认证文件
上边了解到了一个工具叫 saslauthd,现在想要配置这个工具与 MySQL 的连接,则需要通过如下配置进行定义,配置默认路径在 /etc/pam.d:
$ cat > /etc/pam.d/openvpn_mysql << EOF
auth sufficient pam_mysql.so user=vpnadmin passwd=vpn123456 host=localhost db=openvpn table=vpnuser usercolumn=name passwdcolumn=password [where=vpnuser.active=1] sqllog=0 crypt=2 sqllog=true logtable=loginlog logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=time
account required pam_mysql.so user=vpnadmin passwd=vpn123456 host=localhost db=openvpn table=vpnuser usercolumn=name passwdcolumn=password [where=vpnuser.active=1] sqllog=0 crypt=2 sqllog=true logtable=loginlog logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=time
EOF
注意:此处引用的 pam_mysql.so,默认路径为 /usr/lib64/security/pam_mysql.so,此处也可以用绝对路径引用。
3.命令行验证
如上配置如果都没有问题,那么就可以通过 saslauthd 来进行一下验证了。 saslauthd 的用法是 testsaslauthd -u 用户 -p 密码 -s 服务名:
$ testsaslauthd -u test1 -p 123456 -s openvpn_mysql
0: OK "Success."
4.安装 openVPN 依赖库。
事实上,基于 MySQL 认证的 openVPN 使用的服务端配置文件,比普通认证的只多了一行配置:
取消注释(/etc/openvpn/server.conf 文件)
plugin /etc/openvpn/openvpn-plugin-auth-pam.so openvpn_mysql
这行配置的意思是,启用插件,通过 /etc/openvpn/openvpn-plugin-auth-pam.so这个模块儿实现,并对接 saslauthd管理的 openvpn_mysql。
现在还缺少 /etc/openvpn/openvpn-plugin-auth-pam.so这个模块,此模块需要通过 openVPN 的源码编译得来。
# 下载对应版本的源码包
linux根目录运行 wget http://swupdate.openvpn.org/community/releases/openvpn-2.4.12.tar.gz
# 解压
tar xf openvpn-2.4.12.tar.gz
cd openvpn-2.4.12/
# 安装编译所需依赖
yum -y install openssl openssl-devel lzo-devel pam-devel
# 在根目录进行编译
./configure
make
# 将编译生成的库文件拷贝到对应目录
cp src/plugins/auth-pam/.libs/openvpn-plugin-auth-pam.so /etc/openvpn/
拷贝完成之后,启动 openVPN。
ystemctl start openvpn@server
9.用户管理
基于 MySQL 进行用户管理之后就非常简便了,如果需要新增用户,只需要在 user 表新增一条数据即可,如果此用户离职了,那么只需要删除这条数据即可。
1.安装openvpn(WINDOWS测试)
下载 https://www.techspot.com/downloads/5182-openvpn.html 安装 分别从云服务器下载:
/etc/openvpn/easy-rsa/pki/ ca.crt
/etc/openvpn/easy-rsa/pki/private/ client.key
/etc/openvpn/easy-rsa/pki/issued/ client.crt
将客户端证书文件移动到C:\Program Files\OpenVPN\config
创建客户端配置文件:
C:\Program Files\OpenVPN\config 目录下 新建 client.ovpn
配置文件:
client #指定当前VPN是客户端
dev tun #使用tun隧道传输协议
proto udp #使用udp协议传输数据
remote 你的云服务器ip地址 1194 #openvpn服务器公网IP地址端口号
resolv-retry infinite #断线自动重新连接,在网络不稳定的情况下非常有用
nobind #不绑定本地特定的端口号
ca ca.crt #指定CA证书的文件路径
cert client.crt #指定当前客户端的证书文件路径
key client.key #指定当前客户端的私钥文件路径
verb 3 #指定日志文件的记录详细级别,可选0-9,等级越高日志内容越详细
persist-key #通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys
persist-tun
remote-cert-tls server
auth-user-pass
auth-nocache
comp-lzo
最后打开软件输入test1 123456测试连接
成功连上,至此特别感谢三位大佬: https://wiki.eryajf.net/pages/3809.html
https://blog.51cto.com/90xpy/5745957
https://blog.dianduidian.com/post/openvpn使用mysql认证/