1. 程式人生 > >centos6.8+openvpn實現賬戶密碼連接

centos6.8+openvpn實現賬戶密碼連接

說明 文件的 persist exist base span htm sig 更改

#搭建openvpn(編譯安裝)

初始化環境

#update epel mirror
yum install wget -y
cd /etc/yum.repos.d && rm -rf *
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo


#Yum Packs
echo "install/update gcc make wget vim ntpdate sysstat
" yum install -y gcc gcc-c++ vim ntpdate lsof #disable selinux sed -i /SELINUX/s/enforcing/disabled/ /etc/selinux/config setenforce 0 #alias vim echo alias vi="vim" >>/etc/profile source /etc/profile #modify UseDNS sed -i #UseDNS yes/UseDNS no/g /etc/ssh/sshd_config #ntpdate corntab echo ntpdate ntp1.aliyun.com
echo */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >> /var/spool/cron/root chmod 600 /var/spool/cron/root ntpdate ntp1.aliyun.com cat << EOF +--------------------------------------------------------------+ | ===System init over=== | +--------------------------------------------------------------+ +---------------------------by liuhuihuang---------------------+ EOF echo
"##########################################################"

創建一個專門存放openvpn軟件的目錄
mkdir -p /home/xianlai/tools/openvpn

通過xftp把lzo-2.06.tar.gz openvpn-2.2.2.tar.gz checkpsw.sh傳到/home/xianlai/tools/openvpn目錄裏

安裝 lzo 軟件,用於後面文件的壓縮
tar zxf lzo-2.06.tar.gz
cd lzo-2.06
./configure && make && make install

通過 yum 安裝 openssl,openssl-devel 兩個軟件,沒有它倆 openvpn 的安裝就會報錯
yum -y install openssl openssl-devel

安裝 openvpn 軟件
tar zxf openvpn-2.2.2.tar.gz
cd openvpn-2.2.2
./configure --with-lzo-headers=/usr/local/include --with-lzo-lib=/usr/local/lib
make
make install

檢查 openvpn 是否安裝成功,並且查看其版本
which openvpn
openvpn --version

備份 var 文件,並且修改模板
cd /home/xianlai/tools/openvpn/openvpn-2.2.2/easy-rsa/2.0
cp vars vars.bak
vim vars
export KEY_COUNTRY="CN"
export KEY_PROVINCE="BJ"
export KEY_CITY="BJ"
export KEY_ORG="XL"
export KEY_EMAIL="[email protected]"
export [email protected]
export KEY_CN=CN
export KEY_NAME=xianlai
export KEY_OU=xianlai
export PKCS11_MODULE_PATH=changeme
export PKCS11_PIN=1234

執行 source 命令使配置文件生效
source vars
./clean-all

創建 CA 證書,一路回車
./build-ca
生成服務器端證書和秘鑰文件,輸入兩次y,一路回車
./build-key-server server
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y

生成客戶端證書和 key 文件(用戶user1)
./build-key user1
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y

生成傳輸進行秘鑰交換時用到的交換秘鑰協議文件
./build-dh

生成一個防止惡意攻擊的文件(效果似乎一般)
openvpn --genkey --secret keys/ta.key

創建 openvpn 目錄,用於存放配置文件
mkdir /etc/openvpn

拷貝2.0目錄下的 keys 目錄,並切換目錄拷貝 server.conf client.conf 文件
cp -ap keys/ /etc/openvpn/
cd /home/xianlai/tools/openvpn/openvpn-2.2.2/sample-config-files/
cp client.conf server.conf /etc/openvpn/

編輯 openvpn 的配置文件,在操作前養成好習慣,先進行備份
cd /etc/openvpn
cp server.conf server.conf.bak

可以通過 grep 命令過濾查看配置文件,並通過一個臨時文件將過濾後的結果再次導入 server.conf
grep -vE "^;|^#|^$" server.conf > tmp.log
cat tmp.log > server.conf

編輯過濾後的 server.conf 文件
vim server.conf
port 1194 #端口可以更改
proto tcp
dev tun
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key # This file should be kept secret
dh /etc/openvpn/keys/dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1"
push "route 192.168.**.0 255.255.255.0"
push "dhcp-option DNS *.*.*.*"
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
client-cert-not-required #不使用客戶端證書,使用密碼進行驗證
username-as-common-name #使用認證用戶名,不使用證書
script-security 3 system #允許通過環境變量將密碼傳遞給腳本
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #指定路徑,允許登陸的用戶名及密碼

打開路由轉發功能
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p

拷貝啟動腳本至/etc/init.d目錄
cp /home/xianlai/tools/openvpn/openvpn-2.2.2/sample-scripts/openvpn.init /etc/init.d/openvpn
chmod 700 /etc/init.d/openvpn
chkconfig --add openvpn

修改腳本openvpn
切到148行,把ls *.conf 改為ls server.conf保存即可

#啟動openvpn
/etc/init.d/openvpn start

切到/etc/openvpn目錄下
cd /etc/openvpn
cp /home/xianlai/tools/openvpn/checkpsw.sh .
創建psw-file文件
touch psw-file
添加一個用戶測試,刑如:liuhuiuhang 123456(用戶和密碼寫在一行,需要添加用戶的話往下順延即可)

分別修改一下這兩個文件的權限
chmod +x checkpsw.sh
chmod 400 psw-file

導出 client.conf ca.crt
sz -y client.conf
sz -y keys/ca.crt

創建一個後綴名為 .ovpn 的文件(my.ovpn),內容如下:
client
dev tun
proto tcp
remote 公網IP 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
comp-lzo
verb 3
auth-user-pass

##特別提示:
#配置防火墻,別忘記保存
iptables -I INPUT -p tcp --dport 1194 -m comment --comment "openvpn" -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
service iptables save

##端口映射

本文使用的是端口映射的方式做的,具體命令如下:

#windows客戶端配置
安裝openvpn-2.2.2-install.exe,默認安裝,一直默認,然後新建一個文件夾vpn,把client.conf、ca.crt、my.ovpn放至文件夾vpn中,然後把vpn拷貝至openvpn的安裝目錄中的config目錄中。

#MAC客戶端配置
下載tunnelblick,然後安裝,把vpn文件夾拷貝至MAC電腦中,把my.ovpn放至tunnelblick中即可。具體操作可參考鏈接:http://www.xitongzhijia.net/xtjc/20150414/45176.html

轉載:http://aby028.blog.51cto.com/5371905/1886851

附件:checkpsw.sh如下所示:

#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <[email protected]>
#
# 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="/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

安裝包和win、mac客戶端軟件下載處:鏈接: http://pan.baidu.com/s/1boC59Bh 密碼: 45e7

寫在最後:後期還可以和ldap對接起來,再次說明轉載處,沒有他的辛苦,我也搞不出來:http://aby028.blog.51cto.com/5371905/1886851

centos6.8+openvpn實現賬戶密碼連接