环境
服务端: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
<ca>
将/usr/share/easy-rsa/2.0/keys/ca.crt的全部内容复制粘贴于此
</ca>
<cert>
将/usr/share/easy-rsa/2.0/keys/client.crt的全部内容复制粘贴于此
</cert>
<key>
将/usr/share/easy-rsa/2.0/keys/client.key的全部内容复制粘贴于此
</key>
<tls-auth>
将/usr/share/easy-rsa/2.0/keys/ta.key的全部内容复制粘贴于此
</tls-auth>
从服务端下载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
<ca>
-----BEGIN CERTIFICATE-----
MIIFEjCCA/qgAwIBAgIJALomSu6uks0gMA0GCSqGSIb3DQEBCwUAMIG2MQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFTATBgNVBAcTDFNhbkZyYW5jaXNjbzEVMBMG
A1UEChMMRm9ydC1GdW5zdG9uMR0wGwYDVQQLExRNeU9yZ2FuaXphdGlvbmFsVW5p
dDEYMBYGA1UEAxMPRm9ydC1GdW5zdG9uIENBMRAwDgYDVQQpEwdFYXN5UlNBMSEw
HwYJKoZIhvcNAQkBFhJtZUBteWhvc3QubXlkb21haW4wHhcNMTQxMTA2MDg1NTA0
WhcNMjQxMTAzMDg1NTA0WjCBtjELMAkGA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CplqoMKFUUHl9PbU5hiQKBgQDUSV4mIBdHMwEGpwUgYr/Yqc0LNdt3HvsO
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-----
</key>
<tls-auth>
-----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-----
</tls-auth>
时间: 2024-11-13 09:47:46