本次部署的通過賬戶與密碼進(jìn)行認(rèn)證,實(shí)現(xiàn)多人登錄使用VPN,只需要分發(fā)固定的證書和用戶名、密碼就可以,簡單快捷。
一、軟件與規(guī)劃網(wǎng)絡(luò)
軟件版本:
centos7.6
easy-rsa 3.0.8
OpenVPN 2.4.9
網(wǎng)絡(luò)環(huán)境規(guī)劃:
VPN客戶端地址段:10.98.1.0/24
VPN服務(wù)器網(wǎng)卡地址:10.99.1.253
VPN流量出設(shè)備NAT為10.99.1.253
二、基礎(chǔ)環(huán)境配置
2.1、關(guān)閉SElinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
2.2、開啟內(nèi)核轉(zhuǎn)發(fā)
grep -qF ".NET.ipv4.ip_forward" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
2.3、關(guān)閉Firewall防火墻
systemctl stop firewalld
systemctl disable firewalld
三、服務(wù)器安裝與部署
3.1、軟件與環(huán)境安裝
本文使用yum來安裝openvpn,openvpn及其依賴的一些包在epel源上,首先先安裝epel源。
yum -y update
#更新軟件包
yum install -y epel-release
#安裝epel源
yum install -y openssl lzo pam openssl-devel lzo-devel pam-devel
yum install -y easy-rsa
#安裝依賴包
yum install -y openvpn
#安裝openvpn
3.2、easy-rsa配置證書密鑰
cp -rf /usr/share/easy-rsa/3.0.8 /etc/openvpn/server/easy-rsa
cd /etc/openvpn/server/easy-rsa
#復(fù)制easy-rsa工具
find / -type f -name "vars.example" | xargs -i cp {} . && mv vars.example vars
#復(fù)制vars.example并重命名vars
配置vars文件,文件也有該內(nèi)容不過是注釋的,可以直接再最后追加如下內(nèi)容:
cat << EOF >> /etc/openvpn/server/easy-rsa/vars
set_var EASYRSA_REQ_COUNTRY "CN"
# 國家
set_var EASYRSA_REQ_PROVINCE "BJ"
# 省
set_var EASYRSA_REQ_CITY "BeiJing"
# 城市
set_var EASYRSA_REQ_ORG "Lin"
# 組織
set_var EASYRSA_REQ_EMAIL "[email protected]"
# 郵箱
set_var EASYRSA_REQ_OU "Lin"
# 擁有者
set_var EASYRSA_KEY_SIZE 2048
# 長度
set_var EASYRSA_ALGO rsa
# 算法
set_var EASYRSA_CA_EXPIRE 36500
# CA證書過期時(shí)間,單位天
set_var EASYRSA_CERT_EXPIRE 36500
# 簽發(fā)證書的有效期是多少天,單位天
EOF
生成證書與私鑰:
./easyrsa init-pki
./easyrsa build-ca nopass
#生成CA證書,需要填寫組織名稱,隨便寫。
./easyrsa build-server-full server nopass
./easyrsa gen-dh
openvpn --genkey --secret ta.key
3.3、創(chuàng)建日志存儲與用戶目錄
mkdir -p /var/log/openvpn/
# 日志存放目錄
mkdir -p /etc/openvpn/server/user
# 用戶管理目錄
chown -R openvpn:openvpn /var/log/openvpn
# 配置權(quán)限
3.4、創(chuàng)建用戶名密碼文件
echo 'vpnuser01 admin123456' >> /etc/openvpn/server/user/psw-file
#后續(xù)添加用戶直接在該文件下添加就可以;
chmod 600 /etc/openvpn/server/user/psw-file
chown openvpn:openvpn /etc/openvpn/server/user/psw-file
3.5、創(chuàng)建密碼檢查腳本
創(chuàng)建一個(gè)shell文件
/etc/openvpn/server/user/checkpsw.sh,內(nèi)容如下:
#!/bin/sh
PASSFILE="/etc/openvpn/server/user/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
賦予密碼檢查腳本權(quán)限:
chmod 700 /etc/openvpn/server/user/checkpsw.sh
chown openvpn:openvpn /etc/openvpn/server/user/checkpsw.sh
3.7、創(chuàng)建OpenVPN服務(wù)器配置文件
編輯
/etc/openvpn/server/server.conf文件,并寫入以下內(nèi)容:
(也可以復(fù)制一份模板文件進(jìn)行改寫,模板文件路徑
/usr/share/doc/openvpn-2.4.9/sample/sample-config-files/server.conf
port 10444
proto udp
dev tun
user openvpn
group openvpn
#配置證書信息
ca /etc/openvpn/server/easy-rsa/pki/ca.crt
cert /etc/openvpn/server/easy-rsa/pki/issued/server.crt
key /etc/openvpn/server/easy-rsa/pki/private/server.key
dh /etc/openvpn/server/easy-rsa/pki/dh.pem
tls-auth /etc/openvpn/server/easy-rsa/ta.key 0
#配置賬號密碼的認(rèn)證方式
auth-user-pass-verify /etc/openvpn/server/user/checkpsw.sh via-env
script-security 3
verify-client-cert none
username-as-common-name
client-to-client
duplicate-cn
#配置網(wǎng)絡(luò)信息
server 10.98.1.0 255.255.255.0
push "route 10.99.1.0 255.255.255.0"
push "route 172.16.0.9 255.255.255.255"
compress lzo
cipher AES-256-CBC
keepalive 10 120
persist-key
persist-tun
verb 3
reneg-sec 0
#配置日志存放位置
log /var/log/openvpn/server.log
log-Append /var/log/openvpn/server.log
status /var/log/openvpn/status.log
設(shè)置server.conf配置文件軟鏈接,因?yàn)槌绦颢@取的配置文件為:.server.conf
cd /etc/openvpn/server/
ln -sf server.conf .service.conf
3.8、設(shè)置NAT規(guī)則或防火墻規(guī)則
需要配置一條NAT的規(guī)則,這里我使用的是iptables,下面也有firewalld的示例(網(wǎng)卡eth0名稱根據(jù)實(shí)際修改),如果你VPN互訪是通過路由通信則不需要配置NAT規(guī)則:
iptables:
systemctl stop firewalld
systemctl disable firewalld #關(guān)閉firewalld防火墻
yum -y install iptables-services
systemctl enable iptables.service
systemctl start iptables.service
iptables -t nat -A POSTROUTING -s 10.98.1.0/24 -o eth0 -j MASQUERADE #添加NAT規(guī)則
iptables-save
iptables-save > /etc/sysconfig/iptables #保存iptable規(guī)則并開機(jī)自動(dòng)加載
[root@Cloud_Pool_OpenVPN ~]# iptables -t nat -nvL #查看nat規(guī)則
firewalld(建議使用iptables):
#設(shè)置防火墻開機(jī)自啟動(dòng)
systemctl enable firewalld --now
firewall-cmd --public --add-masquerade # 允許防火墻偽裝IP
firewall-cmd --public --add-port=10444/udp
firewall-cmd --public --direct --passthrough ipv4 -t nat -A POSTROUTING -s 10.98.1.0/24 -o eth0 -j MASQUERADE
firewall-cmd --reload
3.9、啟動(dòng)服務(wù)并設(shè)置開機(jī)自動(dòng)啟動(dòng)
rpm -ql openvpn |grep service
# 查看service名
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/[email protected]
/usr/lib/systemd/system/[email protected]
systemctl start [email protected]
# 啟動(dòng)
systemctl status [email protected]
#檢查服務(wù)狀態(tài)
systemctl enable [email protected]
#設(shè)置開機(jī)自啟
四、windows客戶端配置
因?yàn)槲覀兦懊媾渲玫氖琴~號密碼認(rèn)證,所以我們只需要下載ca.crt、ta.key文件即可,從server上將生成的ca.crt、ta.key下載到客戶端的配置文件config下。ca.crt在
/etc/openvpn/server/easy-rsa/pki/下
在config目錄(目錄位置:C:Users[用戶名]OpenVPNconfig)下新建一個(gè)文件 client.ovpn,文件內(nèi)容如下:
client
proto udp
dev tun
auth-user-pass
remote www.aalook.com 10444
ca ca.crt
tls-auth ta.key 1
remote-cert-tls server
cipher AES-256-CBC
auth-nocache
persist-tun
persist-key
reneg-sec 0
compress lzo
verb 3
mute 10
配置完成就可以進(jìn)行連接測試了:
參考文章:
https://www.jianshu.com/p/637b4123fc92
https://www.fandenggui.com/post/centos7-install-openvpn.html






