使用OpenVPN搭建VPN伺服器的例子

來源:互聯網
上載者:User

環境公司內網一台 CentOS 6.6 伺服器 A,網卡eth0 內網IP:192.168.8.60, 公司公網IP為 116.228.12.88, 使用路由器的 DMZ 功能把公網映射到內網伺服器A(即內網IP:192.168.8.60)。

1、在伺服器上安裝OpenVPN。由於預設的Centos軟體源裡面沒有OpenVPN的軟體包,可以通過添加rpmforge的repo,從而實現yum安裝openvpn。
針對CentOS 5

rpm -ivh http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
針對CentOS 6

rpm -ivh http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
注意:伺服器是32位還是64位,由於我的伺服器安裝centos 是64位,所以上傳安裝的源是64位,如果是32位,可以通過瀏覽地址:http://apt.sw.be/redhat/el6/en 找到對應的版本的地址。

2、產生OpenVPN 所需的認證。

OpenVPN 內建了 easy-rsa 工具,可以通過它很方便的產生所需的認證。複製 工具目錄到 /etc/openvpn 下並賦予執行許可權。

cp -R /usr/share/doc/openvpn-*/easy-rsa /etc/openvpn
cd /etc/openvpn/easy-rsa/2.0
chmod +x  *
執行下述命令建立認證。

ln -s openssl-1.0.0.cnf openssl.cnf
. vars
./clean-all
./build-ca server
./build-key-server server
./build-key client
./build-dh
3、建立 OpenVPN 的設定檔server.conf, 檔案放在 /etc/openvpn

port        1194
proto       tcp
dev         tun
ca          /etc/openvpn/easy-rsa/2.0/keys/ca.crt
cert        /etc/openvpn/easy-rsa/2.0/keys/server.crt
key         /etc/openvpn/easy-rsa/2.0/keys/server.key
dh          /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem
server      10.1.1.0 255.255.255.0

auth-user-pass-verify   /etc/openvpn/auth/checkpsw.sh via-env
script-security 3 system
client-cert-not-required
username-as-common-name

push        "redirect-gateway def1 bypass-dhcp"
push        "dhcp-option DNS 8.8.8.8"
push        "dhcp-option DNS 114.114.114.114"
log         /var/log/openvpn.log
keepalive   10 120
verb        3
client-to-client
comp-lzo
persist-key
persist-tun
其中server 後面對應的IP 是指VPN虛擬網段,也就是用戶端擷取IP就是在這個段中,注意,不要和現有的區域網路IP段有衝突。
auth-user-pass-verify 下面的四行是配置用戶端可以使用使用者名稱密碼的方式認證,特別注意一定要加上 script-security 3 system, 後面的system 也不能少,我就是在這個上面浪費好多時間。

checkpsw.sh 指令碼內容如下:

#!/bin/sh
PASSFILE="/etc/openvpn/auth/psw-file"
LOG_FILE="/etc/openvpn/auth/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
其中 PASSFILE 是使用者名稱密碼的檔案路徑,LOG_FILE 輸出的記錄檔。 注意:checkpsw.sh 需要有執行許可權。PASSFILE 的格式為:使用者名稱+空格+密碼, 例如:

netingcn mypassword
4、啟動OpenVPN並將設定其為開機自動啟動。

啟動服務
/etc/init.d/openvpn start

加入開機自動啟動
chkconfig openvpn on
OpenVPN 服務的日誌位於 /var/log/openvpn.log, 如果啟動異常,可以查看該日誌,一般情況是由於生產認證那裡出現問題,可以重建一次。

5、伺服器其他設定。
關閉selinux

sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
開啟ip forward

sed -i '/net.ipv4.ip_forward/s/0/1/g' /etc/sysctl.conf
sysctl -w net.ipv4.ip_forward=1
開啟iptables NAT

iptables -t nat -A POSTROUTING -s 10.1.1.0/24 -j SNAT --to-source 192.168.8.60
特別注意:to source 的值,有些文章提到是公司的公網IP,這個說法有些不太準確,如果該伺服器的網卡綁定是公網IP,也就是說作為路由伺服器,那麼就是用公網IP,由於我的這台伺服器是區域網路內的一台機器,只有區域網路IP,所以這裡用的是原生IP。

如果沒有添加iptables 規則,出現的結果是能連上vpn server,但是不能上網。另外可能還需要用到的規則如下:

iptables -A FORWARD -i tun0 -s 10.1.1.0/24 -j ACCEPT
iptables -A FORWARD -i eth0 -d 10.1.1.0/24 -j ACCEPT
iptables -I INPUT -p tcp --dport 1194 -m comment --comment "openvpn" -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.1.1.0/24 -o eth0 -j MASQUERADE
用戶端官網下載地址:https://openvpn.net/index.php/download/community-downloads.html。

下面以Win 7 用戶端為例,安裝好用戶端後,開啟預設安裝路徑:C:\Program Files\OpenVPN\config, 在下面建立一個 client.ovpn 檔案,
認證認證方式的內容如下:

client
dev tun
proto tcp
remote 116.228.12.88 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
comp-lzo
verb 3
redirect-gateway def1
route-method exe
route-delay 2
需要複製伺服器的 ca.crt,client.crt 和 client.key 到目前的目錄, remote 用公司公網IP

使用者名稱密碼認證的方式如下:

client
dev tun
proto tcp
remote 116.228.208.10 2294
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
;auth-user-pass
auth-user-pass pass.txt
comp-lzo
verb 3
redirect-gateway def1
route-method exe
route-delay 2
只需要複製伺服器的 ca.crt 到目前的目錄即可,同時在目前的目錄建立一個名為 pass.txt,把使用者名稱密碼填入,注意格式為:

使用者名稱
密碼
至此配置完成,右鍵點擊用戶端然後選connect,應該就可以連上。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.