Centos6.5 服务器配置OpenVPN使用账号/密码方式验证登录

来源:互联网 发布:ajax获取json数据实例 编辑:程序博客网 时间:2024/06/05 15:06

一:在开始之前请先配置配置好openvpn服务器和客户端,可参考以下安装文档!

http://blog.csdn.net/llq_200/article/details/74980266

二:修改openvpn服务主配置文件,添加如下内容;如果加上client-cert-not-required则代表只使用用户名密码方式验证登录,如果不加,则代表需要证书和用户名密码双重验证登录!
# tail -3 /etc/openvpn/server.conf
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
client-cert-not-required
username-as-common-name   #使用客户提供的UserName作为Common Name
script-security 4                           #加入脚本处理,如用密码验证

三:下载验证用户登录脚本并进行相应的修改,主要改PASSFILE和LOG_FILE两个变量

权限设置为:-rwxr--r-- (744)
所有者:nobody
chown nobody:nobody checkpsw.sh     #需要先cd到该目录

  1. # cd /etc/openvpn/checkpsw.sh  
  2. # wget http://openvpn.se/files/other/checkpsw.sh  
  3. # chmod +x checkpsw.sh  
  4. # cat checkpsw.sh   
  5. #!/bin/sh  
  6. ###########################################################  
  7. # checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se> 
  8. #  
  9. # This script will authenticate OpenVPN users against  
  10. # a plain text file. The passfile should simply contain  
  11. # one row per user with the username first followed by  
  12. # one or more space(s) or tab(s) and then the password.   
  13. PASSFILE="/etc/openvpn/psw-file" 
  14. LOG_FILE="/etc/openvpn/openvpn-password.log" 
  15. TIME_STAMP=`date "+%Y-%m-%d %T"`  
  16. ###########################################################  
  17. if [ ! -r "${PASSFILE}" ]; then  
  18.   echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >>    
  19. ${LOG_FILE}  
  20.   exit 1  
  21. fi  
  22. CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}`  
  23. if [ "${CORRECT_PASSWORD}" = "" ]; then   
  24.   echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=  
  25. \"${password}\"." >> ${LOG_FILE}  
  26.   exit 1  
  27. fi   
  28. if [ "${password}" = "${CORRECT_PASSWORD}" ]; then   
  29.   echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}  
  30.   exit 0  
  31. fi  
  32. echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=  
  33. \"${password}\"." >> ${LOG_FILE}  
  34. exit 1 

四:准备用户名和密码认证文件,用户名和密码用空格隔开,同时确保openvpn启动用户可读取该文件

  1. # cat psw-file   
  2. lilunqing 123456   
  3. # chmod 400 psw-file  
  4. # chown nobody.nobody psw-file 

五:修改客户端配置文件
注释掉
;cert client1.crt
;key  client1.key

#增加密码验证后,客户端只需包含ca.crt的配置文件

#增加询问用户名和密码   
auth-user-pass

六:修改客户端配置文件后重启服务端。

[html] view plain copy
  1.   service  openvpn restart    #启动openvpn的命令 

七:测试,若输入错误的用户名或密码,则提示重新输入用户名和密码,尝试3次后中断;

 


#tail -n 100 -f openvpn-password.log

原创粉丝点击