CentOS安裝配置OpenVPN並可以用認證或帳號密碼登入

來源:互聯網
上載者:User

OpenVPN是不同於PPTP、L2TP的另一種VPN軟體包,基於SSL的VPN。OpenVPN使用需要用戶端支援。這裡系統為CentOS6.4。
一、檢查OpenVPN所需環境
OpenVPN需要TUN裝置支援,還需要iptables的nat模組支援。
1、檢查TUN模組:

modinfo tun

顯示如下:

如果報錯則不支援TUN裝置。 2、在OpenVZ虛擬化的VPS上,需要管理員在母雞上開啟TUN/TAP裝置。

cat /dev/net/tun

如果返回:

cat: /dev/net/tun: File descriptor in bad state

VPS的TUN/TAP已經可以使用。
如果返回:

cat: /dev/net/tun: No such device

或者其它,說明TUN/TAP裝置沒有被正確配置,需要與客服溝通開通TUN/TAP。
3、檢查OpenVZ的VPS上iptables是否支援:

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE

如果返回:

iptables: Unknown error 4294967295

說明支援。
二、編譯安裝OpenVPN
1、安裝依賴環境

yum install -y openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig

2、安裝OpenVPN

wget -c http://swupdate.openvpn.org/community/releases/openvpn-2.3.0.tar.gz tar zxvf openvpn-2.3.0.tar.gz cd openvpn-2.3.0 ./configure make make install mkdir –p /etc/openvpn cp -Rf sample  /etc/openvpn/ cd ..

單獨下載easy-rsa,製作ca認證,服務端認證,用戶端認證

#openvpn-2.3.0.tar.gz 該版本源碼不包含easy-rsa,所以需要單獨下載安裝 easy-rsa wget -c https://github.com/OpenVPN/easy-rsa/archive/master.zip unzip master mv easy-rsa-master easy-rsa cp -Rf  easy-rsa /etc/openvpn cd /etc/openvpn/easy-rsa/easy-rsa/2.0

三、產生密鑰和認證

vim vars #調到最後找到一下代碼 export KEY_COUNTRY="CN" export KEY_PROVINCE="JN" export KEY_CITY="DZ" export KEY_ORG="lvtao" export KEY_EMAIL="admin@lvtao.net" export KEY_OU="lvtao" #x!儲存退出    ln -s openssl-1.0.0.cnf openssl.cnf chmod +x vars source ./vars ./clean-all #製作ca認證 ./build-ca #製作服務端認證 ./build-key-server server #製作用戶端認證 ./build-key client1 ./build-dh #打包keys tar zcvf keys.tar.gz keys/ #終端發送到用戶端備用 sz keys.tar.gz

#keys中的檔案說明

Filename

Needed By

Purpose

Secret

ca.crt

server + all clients

Root CA certificate

NO

ca.key

key signing machine only

Root CA key

YES

dh{n}.pem

server only

Diffie Hellman parameters

NO

server.crt

server only

Server Certificate

NO

server.key

server only

Server Key

YES

client1.crt

client1 only

Client1 Certificate

NO

client1.key

client1 only

Client1 Key

YES

cd /etc/openvpn/sample/sample-config-files/ cp server.conf ../../  #配置server.conf,具體參數不再累述 vi ../../server.conf  #複製keys cd /etc/openvpn/easy-rsa/easy-rsa/2.0/keys cp -Rf ca.crt server.crt server.key dh2048.pem /etc/openvpn/  # 編輯/etc/sysctl.conf,找到net.ipv4.ip_forward = 0改成net.ipv4.ip_forward = 1儲存。然後執行: sysctl –p  #設定nat轉寄.注意最後192.168.122.180改成你的伺服器的IP地址。 iptables -t nat -A POSTROUTING -s 10.8.8.0/24 -j SNAT --to-source 192.168.122.180 /etc/init.d/iptables save /etc/init.d/iptables restart

PS:如果iptables save報錯

iptables: Saving firewall rules to /etc/sysconfig/iptables: /etc/init.d/iptables: line 268: restorecon: command not found

要安裝一個軟體包:
yum install policycoreutils 設定OpenVPN開機自啟動

echo "/usr/local/sbin/openvpn --config /etc/openvpn/easy-rsa/2.0/conf/server.conf &" >> /etc/rc.local

啟動OpenVPN

/usr/local/sbin/openvpn --config /etc/openvpn/easy-rsa/2.0/conf/server.conf  >/dev/null 2>&1 &



使用密碼登入配置
修改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

驗證使用者登入指令檔並進行相應的修改,主要改PASSFILE和LOG_FILE兩個變數

#!/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="/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

準備使用者名稱和密碼認證檔案,使用者名稱和密碼用空格隔開,同時確保openvpn啟動使用者可讀取該檔案

vi psw-file    lvtao  954270

配置許可權

chmod 400 psw-file   chown nobody.nobody psw-file

修改用戶端設定檔
注釋掉

;cert lvtao.crt  ;key  lvtao.key

增加詢問使用者名稱和密碼

auth-user-pass
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.