继上篇openvpn安装,本篇主要通过账号密码的方式连接openvpn服务器。解决了证书认证的一证书一客户端的限制。
用户密码认证
1.创建用户认证脚本(checkpsw.sh)
[root@localhost ~]# cat /etc/openvpn/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="/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
2.配置用户密码文件
[root@localhost ~]# cat /etc/openvpn/psw-file
user1 passwd1
user2 passwd2
3.修改服务端配置文件
在server.conf末尾添加如下几行信息:
script-security 3
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #指定用户认证脚本
username-as-common-name
verify-client-cert none
4.修改客户端配置文件
注释掉cert和key(客户端不需要crt和key文件,但是需要服务器的CA证书)
;cert eva.crt
;key eva.key
添加如下内容:
auth-user-pass
cat /etc/openvpn/client.ovpn
client
dev tun
proto tcp
remote 123.xxx.xxx.xxx 1194
resolv-retry infinite
nobind
persist-key
persist-tun
tls-auth ta.key 1
remote-cert-tls server
ca ca.crt #该文件一定要与服务器端ca.crt是同一个文件
;cert client.crt #定义客户端的证书文件,此处注释即可
;key client.key #定义客户端的密钥文件,此处不需要开启
comp-lzo #启用允许数据压缩,和Server端保持一致
verb 3
cipher AES-256-CBC
auth-user-pass #使用用户名密码登录openvpn服务器
配置客户端固定IP(只需要修改server端配置文件即可)
- 用户密码认证
mkdir /etc/openvpn/ccd #新建目录
1.服务端添加下面一行配置
cat server.conf
...
...
client-config-dir /etc/openvpn/ccd #添加此行
2.配置具体用户的IP
cat user1 #user1对应pws-file文件里面设置的用户名
ifconfig-push 172.16.0.56 172.16.0.57
3.重启服务
service openvpn restart #服务端重启服务
#linux客户端,kill掉进程重新执行下:
ps -ef| grep openvpn
root 10674 10658 0 11:48 pts/0 00:00:00 grep openvpn
root 32466 1 0 May25 ? 00:01:33 openvpn --daemon --cd /etc/openvpn/ --config client.ovpn --log-append /var/log/openvpn.log
kill -9 32466
openvpn --daemon --cd /etc/openvpn/ --config client.ovpn --log-append /var/log/openvpn.log
#windows客户端:
断开连接再重新连接
- 证书认证
如果是使用证书认证的方式,那么只需要将ccd目录下的用户名命名的文件改成客户端证书名即可生效。
例如:
如上面所设置/etc/openvpn/ccd/user1对应用户user1;则/etc/openvpn/ccd/client即对应客户端证书名client的配置。
本文由 Mr Gu 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: May 31, 2018 at 03:46 pm