在Debian5 安装OpenVPN笔记

2,673次阅读
没有评论

如果 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

而后下载相应的文件到客户端,并按照之前介绍的步骤配置客户端。

正文完
 0
评论(没有评论)
验证码