服務端:CentOS 6.7 32-bit
客戶端:Windows XP
# 關閉SELinux
sed -i '/^SELINUX\b/s/=.*/=disabled/' /etc/selinux/config
setenforce 0
# 安裝EPEL源(默認yum源沒有openvpn和easy-rsa軟件包)
rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/5/i386/epel-release-5-4.noarch.rpm
# 安裝openvpn和easy-rsa軟件包
yum -y install openvpn easy-rsa
# 切換到/usr/share/easy-rsa/2.0/目錄
cd /usr/share/easy-rsa/2.0/
# 初始化環境變量
source vars
# 清除所有與證書相關的文件
./clean-all
# 生成CA相關文件(一路按回車即可)
./build-ca
# 生成服務端相關文件(一路按回車,直到提示需要輸入y/n時,輸入y再按回車,一共兩次)
./build-key-server server
# 生成客戶端相關文件(一路按回車,直到提示需要輸入y/n時,輸入y再按回車,一共兩次)
./build-key client
# 生成dh2048.pem文件(生成過程時快時慢,在此期間不要去中斷它)
./build-dh
# 生成ta.key文件(防DDos攻擊)
openvpn --genkey --secret keys/ta.key
# 在openvpn的配置目錄下新建一個key目錄
mkdir /etc/openvpn/keys
# 將openvpn配置文件需要用到的文件復制一份到剛創建好的keys目錄中
cp /usr/share/easy-rsa/2.0/keys/{ca.crt,server.{crt,key},dh2048.pem,ta.key} /etc/openvpn/keys/
# 創建/etc/openvpn/server.conf文件,內容如下
port 1194
proto udp
dev tun
ca keys/ca.crt
cert keys/server.crt
key keys/server.key # This file should be kept secret
dh keys/dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 192.168.1.0 255.255.255.0" # 192.168.1.0/24是我這台VPN服務器所在的內網的網段,讀者應該根據自身實際情況進行修改
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3
# 開啟路由轉發功能
sed -i '/net.ipv4.ip_forward/s/0/1/' /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
# 配置防火牆
iptables -F
iptables -X
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
service iptables save
# 啟動openvpn服務並將其設置為開機啟動
service openvpn start
chkconfig openvpn on
客戶端配置
# 創建一份客戶端文件(命名為client.ovpn),內容如下(讀者要注意修改下面的服務端公網IP)
client
dev tun
proto udp
remote 服務端公網IP 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ns-cert-type server
comp-lzo
verb 3
tls-auth [inline] 1
將/usr/share/easy-rsa/2.0/keys/ca.crt的全部內容復制粘貼於此
將/usr/share/easy-rsa/2.0/keys/client.crt的全部內容復制粘貼於此
將/usr/share/easy-rsa/2.0/keys/client.key的全部內容復制粘貼於此
將/usr/share/easy-rsa/2.0/keys/ta.key的全部內容復制粘貼於此
從服務端下載client.ovpn,並將其復制到openvpn的安裝目錄的config目錄下,最後,啟動openvpn程序,連接服務端,如果能獲取到IP,且能ping內網的其他機器就表示配置成功了。
最後給出我的client.ovpn的范例文本供讀者參考。
client
dev tun
proto udp
remote 192.168.1.88 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ns-cert-type server
comp-lzo
verb 3
tls-auth [inline] 1
-----BEGIN CERTIFICATE-----
MIIFEjCCA/qgAwIBAgIJALomSu6uks0gMA0GCSqGSIb3DQEBCwUAMIG2MQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFTATBgNVBAcTDFNhbkZyYW5jaXNjbzEVMBMG
A1UEChMMRm9ydC1GdW5zdG9uMR0wGwYDVQQLExRNeU9yZ2FuaXphdGlvbmFsVW5p
dDEYMBYGA1UEAxMPRm9ydC1GdW5zdG9uIENBMRAwDgYDVQQpEwdFYXN5UlNBMSEw
HwYJKoZIhvcNAQkBFhJtZUBteWhvc3QubXlkb21haW4wHhcNMTQxMTA2MDg1NTA0
WhcNMjQxMTAzMDg1NTA0WjCBtjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRUw
E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t3HvsO
ZzrxKqFCiB1XE7rc05/2Tt0ll8FSNdlPIwfu3YPUzoU1SCMjb1Q9GrvR9H5DNv24
8wd74ThzOF0xiZUOZwj6X6ZxFvfdUe6hI5h/b0dG7pUw+JSkmnpD7BO+YE1MjjN3
nzqQTnecoQKBgQCyJFiyF0NE7PDxxbJC6OzPGFWbGyPPfInDSgzbgXxbAMvNQZIt
5I0Detvk6HHOO8yPs6oxWQfGVXrB7K+GfAGZiLV2ChBZVs0PSJ8AIVCXlwEzcbIg
MerjHESW/ivznea6ywrHCdk69PM7KyHyzXq2E+LRMJUR41k+xOP/fqwYcQKBgQDI
OU7wnLH3+JZOJPgD3L/f5f+8RBb0WqcmpZ0FXFTvAJzTxYsovv2P/kA9Nc4j8SA+
sObJl+rAq+0eHSTvRhDo9S8TTwxL7zEN4UM8x2dL3WygzYhmJi5koBTHc4djGuT8
3Sr3fwh2UY8rujnQqtcI+0B//irKOxE2EVvWQfw1IQKBgQCJgU0Ef+CDR6R2iImL
69xkwp2umQVDPFCJtlJ5Oqg7CRI4HHo2+ujfDq8hl4ihg0Zq6e+iIrBCBOOFlpLn
xrvhxAv79sB/w5Y3zSwTtqwnpUR65ZKi2X4exza0//yY77LAuGNcG5oKTUhTBDMz
FPnZX9Q0zJevs0+UfAeXvThc9g==
-----END PRIVATE KEY-----
-----BEGIN OpenVPN Static key V1-----
a692b93eeb708a615914f791ef42a2fb
4d14e99055aa297e564366ed272c25d7
116cd7a43d5f9d02c84d566406a3a657
84f1e69c23c3d954b1a19dc4d373b8a3
7c717d397c51e947183a628c4f4a7e98
173a65e0ce9806b2b04f1ce0e45ffacb
67bbca2db49cb3b78c573b85fb3d79c4
bbbf61d9147513957ac4668e541db859
c449eaf04b0d0585dc4c102ca010d91a
5ad275b7fb13e95f0a971a88a7550cb4
3485825fb6304b8537ac9cd6af5fda68
4a0d94d47f3a0478e722f20e0043de1c
c18684f5b68e6f19ad5b302cb9ddc1ca
b326c80c4b6bb235dda607a5fa79fbc8
5da586741a428e2ab390827c5145893a
d78f0bef7c86710ec7752d60cb94cada
-----END OpenVPN Static key V1-----