环境准备:
一、EasyRSA-2.2.2.tgz证书制作工具(github下载)
centos6.8yum源配置以及epel下载
yum install epel-release
yum clean all
yum makechache
二、制作证书密钥、配置openvpn配置文件
tar -zxvf EasyRsa-2.2.2.tgz
更改vars文件夹内容(只需要更改红框中内容即可)
#:source vars
#./clean-all 生成keys文件夹
#:./bulid-ca 生成根证书和根密钥(不用写入任何参数直接一直确定)
#./bulid-key-server server(名称可随意)。 生成服务证书和服务密钥;
#./bulid-key client(名称可随意).生成客户端证书和密钥
#./bulid-dh 生成密钥交换文件(客户端和服务密钥交换)
三、配置openvpn服务端配置文件
yum install -y openvpn
mkdir /etc/openvpn/keys (将/root/EasyRSA-2.2.2/keys目录下的证书文件复制过去)新建一个文件夹单独存储证书和密钥文件
cp /usr/share/doc/openvpn-2.4.9/sample/sample-config-files/server.conf /etc/openvpn 将配置文件复制到openvpn目录下
#:vi /etc/openvpn/server.conf 只更改这部分内容即可
#:openvpn --daemon --config /etc/openvpn/server.conf 启动openvpn服务
netstat -anput 查看openvpn端口是否监听
四、客户端基于证书连接VPN配置
mkdir /root/client
cp /usr/share/doc/openvpn-2.4.9/sample/sample-config-files/client.conf client/ 复制客户端配置文件至client文件夹
mv /root/client/client.conf /root/client/client.ovpn 更改客户端后缀是为了让windows下能识别配置文件
cp ca.crt ta.key client.key client.crt /root/client
vi /root/client.ovpn 只变更如下配置
客户端操作系统远程将client文件夹内容拷贝至openvpn客户端配置文件存储目录下,后直接连接,如下提示连接成功
五、基于用户认证的openvpn配置
vi /etc/openvpn/server.conf 中添加如下内容并禁用tls-auth /etc/openvpn/keys/ta.key 参数
client-cert-not-required
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
username-as-common-name
script-security 3
vi checkpsw.sh 创建用户认证脚本,并给与执行权限 chmod -766 checkpsw.sh ***都创建在/etc/openvpn/目录下
#!/bin/bash
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se>
#
# 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="/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
vi psw-file 创建保存账号密码文件,并给与执行权限 chmod -766 psw-file
vi client.ovpn 配置客户端配置文件 并更改以下内容;
将更改完成的客户端配置文件导入客户端配置文件目录下,进行用户登录认证
标签:username,0pen,etc,client,centos6.8,openvpn,password,pn,客户端 From: https://blog.51cto.com/u_12489404/6076517