Debian下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 client1
./build-dh
注意:上面操作全部直接回车即可,如果有选择Y/N的 全部选择Y即可。

OpenVPN需要tun和iptables_nat模块支持,所以先检查你的VPS支持不支持。
SSH登陆VPS后输入
cat /dev/net/tun
若返回信息为:cat: /dev/net/tun: File descriptor in bad state 说明tun/tap已经可以使用;
如果返回:cat: /dev/net/tun: No such device 或其他则说明tun/tap没有被正确配置,发TK联系客服申请开通tun/tap。
apt-get install iptables #如已安装跳过
如果你的VPS是Xen或KVM的请输入:(eth0要根据具体的网卡标识来,可以通过ifconfig查看)
iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
如果是OpenVZ的请输入:(11.22.33.44是你VPS的IP)
iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -j SNAT –to-source 11.22.33.44
iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -o venet0 -j SNAT –to 11.22.33.44 //注意如果是venet0
修改vi /etc/sysctl.conf net.ipv4.ip_forward=1 用sysctl –p使其生效
然后用
iptables -t nat -L
查看iptables转发状态,如果显示以下类似的结果则说明转发成功:
target prot opt source destination
SNAT all — 10.0.0.0/8 anywhere to:11.22.33.44

配置OpenVPN服务器端文件
dev tap
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 4.2.2.4”
comp-lzo

重启/etc/init.d/openvpn restart
用FTP将
/etc/openvpn/easy-rsa/2.0/keys/ca.crt
/etc/openvpn/easy-rsa/2.0/keys/client1.crt
/etc/openvpn/easy-rsa/2.0/keys/client1.key
三个文件下载到本地并保存在OpenVPN的config目录,然后添加client.ovpn客户端文件,内容如下:
client
dev tap
proto tcp

# The hostname/IP and port of the server.
# CHANGE THIS TO YOUR VPS IP ADDRESS
remote 11.22.33.44 1194

resolv-retry infinite
nobind

persist-key
persist-tun

ca ca.crt
cert client1.crt
key client1.key

comp-lzo
verb 3

重启一下VPS基本上可以连接了。

总结:

最后可能遇到的问题
1、Wed Oct 13 21:57:57 2010 us=250000 TCP: connect to 2**.*3*.*1*.1**:23 failed, will try again in 5 seconds: Connection refused (WSAECONNREFUSED)

检查dev  将dev tun 改 dev tap 或反之 就OK了

2、有可能无法连接ROUTE: route addition failed using CreateIpForwardEntry: 拒绝访问。
这是无法添加路由,在windows7中想用route add 或相关route等命令需要以管理员身份运行,如果windows7下面没有以管理身份运行那么添加路由时候route命令后提示:请求的操作需要提升!
解决方法:更改OpenVPN GUI兼容性

h201188161431

[download id=”5″]

评论

此博客中的热门博文

FreeBSD安装Pure-FTPd及user manager for PureFTPd

debian lighttpd php ssl

解决nginx出现File not found的问题