在CentOS 6.2上搭建OpenVPN Server
Contents
OpenVPN是不同于PPTP、L2TP的另一种VPN软件包,基于SSL的VPN。OpenVPN使用需要客户端支持。这里系统为CentOS6.2。
一、检查OpenVPN所需环境
OpenVPN需要TUN设备支持,还需要iptables的nat模块支持。
1、检查TUN模块:
1 2 3 4 |
modinfo tun |
2、在OpenVZ虚拟化的VPS上,需要管理员在母鸡上打开TUN/TAP设备。
1 2 3 4 |
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是否支持:
1 2 3 4 |
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE |
如果返回:
iptables: Unknown error 4294967295
说明支持。
二、编译安装OpenVPN
1、安装LZO
LZO是一种数据压缩算法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
cd /usr/local/src wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz tar -zxvf lzo-2.06.tar.gz cd lzo-2.06 ./configure make make install |
2、安装OpenVPN
1 2 3 4 5 6 7 8 9 10 11 12 |
cd /usr/local/src wget http://swupdate.openvpn.org/community/releases/openvpn-2.2.2.tar.gz tar -zxvf openvpn-2.2.2.tar.gz cd openvpn-2.2.2 ./configure |
此时提示:
configure: error: OpenSSL Crypto headers not found.
原来是openssl的开发包没有安装:
1 2 3 4 |
yum install openssl-devel |
继续安装:
1 2 3 4 5 6 7 8 9 10 11 12 |
./configure make make install mkdir /etc/openvpn cp -R ./easy-rsa/ /etc/openvpn/ #复制生成证书的脚本 |
三、生成密钥和证书
1、初始化PIK
1 2 3 4 5 6 |
cd /etc/openvpn/easy-rsa/2.0/ vim vars |
设置最后的证书字段值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
export KEY_COUNTRY="CN" export KEY_PROVINCE="SH" export KEY_CITY="shanghai" export KEY_ORG="xxxx" export KEY_EMAIL="xxx@xxx.com" export KEY_EMAIL=xxx@xxx.com export KEY_CN=tom export KEY_NAME=tom export KEY_OU=tom export PKCS11_MODULE_PATH=tom export PKCS11_PIN=1234 |
使vars设置生效:
1 2 3 4 |
source ./vars |
报错:
No /etc/openvpn/easy-rsa/2.0/openssl.cnf file could be found
Further invocations will fail
将/etc/openvpn/easy-rsa/2.0目录下的openssl-1.0.0.cnf改名为openssl.cnf可解决:
1 2 3 4 5 6 |
cp openssl-1.0.0.cnf openssl-1.0.0.cnf.bak mv openssl-1.0.0.cnf openssl.cnf |
继续运行脚本设置变量,并清理:
1 2 3 4 5 6 |
source ./vars ./clean-all |
建立私钥:
1 2 3 4 |
./build-ca |
build-ca脚本用于生成一个1024位的RSA私钥,由于前面已经设置过vars文件,直接按回车。
2、建立server key
1 2 3 4 |
./build-key-server server |
前面几个按回车,中间有extra属性要填,最后按两个y。
3、建立client key
1 2 3 4 |
./build-key client1 |
和建立server key基本相同,生成的每个客户端证书名字要不同。一份客户端证书对应一个客户端。
4、生成Diffie Hellman参数
1 2 3 4 |
./build-dh |
5、将keys下的所有文件下载到本地
可以用winscp等工具。
6、创建服务端配置文件
将源代码解压后目录下的规范配置文件复制过来。
1 2 3 4 5 6 |
mkdir /etc/openvpn/easy-rsa/2.0/conf/ cp /usr/local/src/openvpn-2.2.2/sample-config-files/server.conf /etc/openvpn/easy-rsa/2.0/conf/ |
编辑配置文件:
1 2 3 4 |
vim /etc/openvpn/easy-rsa/2.0/conf/server.conf |
找到:
ca ca.crt
cert server.crt
key server.key
dh dh1024.pem
修改为:
1 2 3 4 5 6 7 8 9 10 |
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 |
并将以下几句前的分号去掉:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" client-to-client user nobody group nobody |
7、设置IP包转发
1 2 3 4 |
vim /etc/sysctl.conf |
将“net.ipv4.ip_forward”的值改为1。
使sysctl.conf配置生效:
1 2 3 4 |
sysctl -p |
这里忽略这些错误
1 2 3 4 5 6 7 8 |
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key error: "net.bridge.bridge-nf-call-iptables" is an unknown key error: "net.bridge.bridge-nf-call-arptables" is an unknown key |
8、添加iptables转发
1 2 3 4 5 6 7 8 |
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE /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
要安装一个软件包:
1 2 3 4 |
yum install policycoreutils |
9、设置OpenVPN开机自启动
1 2 3 4 |
echo "/usr/local/sbin/openvpn --config /etc/openvpn/easy-rsa/2.0/conf/server.conf &" >> /etc/rc.local |
10、启动OpenVPN
1 2 3 4 |
/usr/local/sbin/openvpn --config /etc/openvpn/easy-rsa/2.0/conf/server.conf |
四、安装Windows客户端OpenVPN GUI For Windows
1、下载客户端软件并安装
http://openvpn.se/files/install_packages/openvpn-2.0.9-gui-1.0.3-install.exe
2、配置客户端
将下载下来的keys文件夹内的:
ca.crt
ca.key
client1.crt
client1.csr
client1.key
复制到客户端安装路径config文件夹下。
例如:
C:Program FilesOpenVPNconfig
将安装路径sample-config文件夹下的client.ovpn也复制到config文件夹内。
打开client.ovpn,找到:
remote my-server-1 1194
将my-server-1修改为OpenVPN服务端的ip地址。
找到:
cert client.crt
key client.key
修改为客户端证书的名字。
cert client1.crt
key client1.key
在最后加上:
redirect-gateway def1
使客户端把OpenVPN服务器IP设为默认网关。
双击client.ovpn即可启动openvpn,或者通过OpenVPN GUI的控制启动VPN。
随后到ip查询的网站上查看自己的ip是否为OpenVPN服务器ip。
备注:
客户端连接时报错:
There are no TAP-Win32 adapters on this system. You should be able to create a TAP-Win32 adapter by going to Start -> All Programs -> OpenVPN -> Add a new TAP-Win32 virtual ethernet adapter.
需要安装TAP-Win32 virtual ethernet adapter。
参考资料:
http://rashost.com/blog/centos-openvpn-install
http://www.cnblogs.com/fangbo/archive/2011/08/04/2127710.html
http://www.fallday.org/archives/230
http://openvpn.net/index.php/open-source/documentation/howto.html