Linux · 2010-05-26

在VPS中安装配置OpenVPN

如果VPS的带宽和内存充足,通过它使用VPN代理上网是非常方便的。本文以Ubuntu系统为例,介绍OpenVPN的安装与配置。 OpenVPN身份验证使用的是证书文件,而非账号密码形式。 安装配置OpenVPN之前,请确保VPS已支持tun/tap,且iptables已支持NAT。ramhost的VPS默认都是支持的。 如果还没有安装iptables请先安装iptables: apt-get install iptables 安装OpenVPN: apt-get install openvpn 拷贝文件夹,以方便后续配置: cp -R /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn 生成服务端和客户端的密钥及证书文件: cd /etc/openvpn/easy-rsa/2.0 source ./vars ./clean-all ./build-ca ./build-key-server server ./build-key clientsteven ./build-dh 其中遇到需要输入信息的步骤全部按回车过去,直到最后一个步骤提示是否生成证书,按Y即可。 配置iptables规则,以转发来自VPN的请求: chmod 755 /etc/rc.local vim /etc/rc.local 将rc.local文件修改为以下内容: #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will “exit 0” on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # add iptables rule for openvpn iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j SNAT –to 202.248.185.66 /etc/init.d/openvpn start exit 0 其中,202.248.185.66是VPS的IP. 注意:上面那行尾SNAT –to 202.248.185.66,是两个“-”号,WP把两个变成一个了,不知道为什么 创建OpenVPN的配置文件: vim /etc/openvpn/openvpn.conf openvpn.conf文件内容如下: dev tun proto tcp port 1194 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 user nobody group nogroup server 10.8.0.0 255.255.255.0 persist-key persist-tun #status openvpn-status.log #verb 3 client-to-client push “redirect-gateway def1” push “dhcp-option DNS 8.8.8.8” push “dhcp-option DNS 8.8.4.4” comp-lzo 启动OpenVPN: /etc/init.d/openvpn start /etc/rc.local 至此,服务端的配置结束,接下来是Windows客户端的配置。 下载以下几个文件到客户端机器: /etc/openvpn/easy-rsa/2.0/keys/ca.crt /etc/openvpn/easy-rsa/2.0/keys/clientsteven.crt /etc/openvpn/easy-rsa/2.0/keys/clientsteven.key 下载安装OpenVPN,下载链接: http://openvpn.net/release/openvpn-2.1.0-install.exe 将下载的ca.crt、clientsteven.crt、clientsteven.key三个文件拷贝到OpenVPN安装目录的config 文件夹下,并新建clientsteven.ovpn文件,内容如下: client dev tun proto tcp # The hostname/IP and port of the server. # CHANGE THIS TO YOUR VPS IP ADDRESS remote 202.248.185.66 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt cert clientsteven.crt key clientsteven.key comp-lzo verb 3 其中,202.248.185.66是VPS的IP. 若之后还需要添加其他的VPN账号,则需要以下命令: cd /etc/openvpn/easy-rsa/2.0 ./build-key clientsusan 而后下载相应的文件到客户端,并按照之前介绍的步骤配置客户端。