如何配置OpenVPN使用用户
一:在开始之前请先配置配置好openvpn服务器和客户端,可参考以下安装文档!
二:修改openvpn服务主配置文件,添加如下内容;如果加上client-cert-not-required则代表只使用用户名密码方式验证登录,如果不加,则代表需要证书和用户名密码双重验证登录!
# tail -3 /usr/local/openvpn/etc/server.conf
auth-user-pass-verify /usr/local/openvpn/etc/checkpsw.sh via-env
client-cert-not-required
username-as-common-name
三:下载验证用户登录脚本并进行相应的修改,主要改PASSFILE和LOG_FILE两个变量
# cd /usr/local/openvpn/etc/ # wget http://openvpn.se/files/other/checkpsw.sh # chmod +x checkpsw.sh # cat checkpsw.sh #!/bin/sh ########################################################### # 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="/usr/local/openvpn/etc/psw-file" LOG_FILE="/usr/local/openvpn/var/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
四:准备用户名和密码认证文件,用户名和密码用空格隔开,同时确保openvpn启动用户可读取该文件
# cat psw-file yangliangwei 123456 # chmod 400 psw-file # chown nobody.nobody psw-file
五:修改客户端配置文件
注释掉
;cert yangliangwei.crt
;key yangliangwei.key
增加询问用户名和密码
auth-user-pass
六:测试,若输入错误的用户名或密码,则提示重新输入用户名和密码,尝试3次后中断;