第一步:检查 OpenVPN 所需环境
如果返回信息为:cat: /dev/net/tun: File descriptor in bad state 说明正常
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE
如果返回信息为:iptables: Unknown error 4294967295 说明正常
第二步:安装 OpenVPN
# rpm -Uvh
http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
# yum install openvpn
第三步:配置 OpenVPN
# locate easy-rsa
# cp -R /usr/share/openvpn/easy-rsa /etc/openvpn/
# cd /etc/openvpn/easy-rsa/2.0
# vi vars
修改如下内容,保存
export KEY_COUNTRY="CN"
export KEY_PROVINCE="FJ"
export KEY_CITY="XM"
export KEY_ORG="21Andy.com"
export KEY_EMAIL="gmail@gmail.com"
生成证书
# ./clean-all
# source ./vars
# ./build-ca server
全部回车
# ./build-key-server server
全部回车 最后2个y
# ./build-key andy
全部回车 最后2个y
# ./build-dh
打包下载客户端所需文件
# tar -zcvf keys.tar.gz keys
把 keys 放到正确目录
新建 server.conf
# vi /etc/openvpn/server.conf
放入如下内容
port 1194
proto udp
dev tun
ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh1024.pem
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
client-to-client
keepalive 10 120
comp-lzo
persist-key
persist-tun
verb 3
启用 ipv4 转发
# echo 1 > /proc/sys/net/ipv4/ip_forward
或
# /sbin/sysctl -w net.ipv4.ip_forward=1
让它生效
如果此时出现错误 error: "Operation not permitted" setting key "net.ipv4.tcp_synCOOKIEs" ,就编辑 sysctl.conf
注释掉 net.ipv4.tcp_synCOOKIEs 那一行
配置 iptables
# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT
--to-source 你的服务器IP地址
/etc/init.d/iptables save
/etc/init.d/iptables restart
启动 OpenVPN
检查 OpenVPN
# netstat -anup | grep 1194
OpenVPN 开机启动