前面我们是使用openvpn 秘钥的方式登录,这种登录安全性比较高。但是运维操作起来比较麻烦,如果有ldap的也推荐使用ldap集成openvpn。但是这里我们偷个懒,通过设置账号密码的方式连接vpn

配置OpenVPN

首先我们需要编写一个用户认证的脚本 (脚本是由openvpn官网提供的)

vim /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

接下来给脚本执行权限

chmod 755 /etc/openvpn/checkpsw.sh

现在我们配置用户密码文件

cat /etc/openvpn/psw-file 
abcdocker 123456
abc 123456
test test
 
#前面为用户名,后面为密码。 中间使用空格分开

接下来我们需要openvpn的server.conf

cat >>/etc/openvpn/server.conf<<EOF
script-security 3
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env    #指定用户认证脚本
username-as-common-name
verify-client-cert none
EOF
 
#在service.conf最后一行添加

接下来我们需要修改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

接下来我们重启一下openvpn即可

[root@vpn ~]# ps -ef|grep openvpn
root      54047  53844  0 14:38 pts/0    00:00:00 grep --color=auto openvpn
[root@vpn ~]# /usr/local/openvpn/sbin/openvpn --daemon --config /etc/openvpn/server.conf

然后我们导出client.ovpn,从新启动客户端就可以了