首先,你要确定你的VPS的环境:
验证OpenVZ VPS的TUN/TAP设备已被正确呢。可以用下面命令测试:
#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设备没有被正确配置,请与你的客服继续联系。
另外还是要提醒,要使用VPN路由需要在VPS中配置NAT,比如下面命令:
# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE
如果返回错误,比如:
iptables v1.4.3.2: can’t initialize iptables table `nat’: Table does not exist (do you need to insmod?) Perhaps iptables or your kernel needs to be upgraded.
那么说明你的母机变不允许你在VPS中设置NAT,也需要你和客服联系,让他们开通VPS的NAT。其他错误就是OK了。
第二,本机安装OpenVPN
1 |
sudo apt-get install openvpn |
在ubuntu下安装软件就这么简单,一条指令够了。
第三、配置OpenVPN服务器和客户端
1、生成证书
配置证书的过程稍微复杂一点,先拷贝/usr/share/doc/openvpn/examples/easy-rsa/2.0目录到/etc/openvpn目录下或者home目录里。修改目录里面的vars文件中的几个配置项:
1 2 3 4 5 |
export KEY_COUNTRY="" export KEY_PROVINCE="" export KEY_CITY="" export KEY_ORG="" export KEY_EMAIL="" |
请将上面这些变量设置称你所想要的。KEY_COUNTRY使用2个大写字母来表示国家,比如中国代号可以设置成 CN
然后执行如下命令
1 2 3 4 5 |
./clean-all #清理一下之前生成的、扔在同级keys目录下的证书 ./build-ca #建立ca证书 ./build-key-server server #生成服务器端的证书和密钥,名字自选 ./build-key client1 #生成客户端的证书和密钥,名字自选,可以做若干个 ./build-dh #生成Diffie Hellman参数,非对称加密传递密钥用 |
注意:在进入 Common Name (eg, your name or your server’s hostname) []: 的输入时, 每个证书输入的名字必须不同,以上生成的证书在keys文件夹中,下面配置会用到。
2、配置服务端
拷贝示例的server.conf文件到/etc/openvpn目录,进行修改:
1 2 |
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ sudo gzip -d /etc/openvpn/server.conf.gz |
按照下面进行修改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#OpenVPN的监听地址 local xxx.xxx.xxx.xxx #OpenVPN的监听端口 port 1194 #使用UDP Server proto udp #使用tun通道 dev tun #相对路径 /etc/openvpn ca ca.crt cert server.crt key server.key dh dh1024.pem #OpenVPN的子网地址 server 10.8.0.0 255.255.255.0 #客户端的Internet访问使用OpenVPN通道 push "redirect-gateway def1" #Windows客户端会使用此DNS地址 push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" #记录客户端分配的ip地址情况,如果OpenVPN服务重启, #客户端重新连接的时候会按照此文件分配原先的地址。 ifconfig-pool-persist ipp.txt #每10秒ping一次,120秒内客户端没有动作就断开连接 keepalive 10 120 #客户端之间可以互相访问 client-to-client #对OpenVPN链接进行压缩,客户端的配置文件必须同时使用 comp-lzo #OpenVPN的用户和用户组 user nobody group nobody #如果user和group的用户和用户组没有访问key的权限 #避免产生权限不足的bug persist-key persist-tun #日志文件 status openvpn-status.log #日志文件的记录级别 verb 3 |
3、客户端配置修改
1 2 3 |
cd /usr/share/doc/openvpn/examples/sample-config-files/ #进入源代码解压目录下的sample-config-files子目录 cp client.conf /etc/openvpn #cp客户端配置文件到/usr/local/etc (如果你准备用WINDOWS做客户端。这个放哪里就没有关系了。配置完就传送到客户端去。) vim /etc/openvpn/client.conf #配置文件如下所示 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#配置文件开始,按照实际情况填写 client dev tun proto udp remote 这里填写VPSIP地址 1194 persist-key persist-tun ca ca.crt cert client01.crt key client01.key ns-cert-type server comp-lzo verb 3 redirect-gateway def1 #所有的数据都会从VPN走 #配置文件结束 |
windows下客户端的配置过程:
1.安装 OpenVPN GUI For Windows, 到 http://www.openvpn.net/index.php/open-source/downloads.html 下载.
2.依屏幕指示安装openvpn gui.
3.配置 openvpn gui :
安装结束后,进入安装文件夹下的 config 目录, 然后将上面建立的 client.conf 文件从 server 上下载到此文件夹, 并更名为 client.ovpn
同时, 将上面制作的下列证书文件解压到此文件夹.
ca.crt
client1.crt
client1.key
4.然后双击 client.ovpn 即可启动 openvpn, 或者通过 OpenVPN GUI 的控制启动 VPN.
如果双击 client.ovpn 没有反应, 则在任务栏点 OpenVPN GUI 的小图标右键, 选择 edit config, 将内容复制过去再保存. 然后再点右键中的 connect 即可.
(也可以在client.ovpn的右键菜单中选择"Start OpenVPN on this config file"就可以连上了。确认没问题后,启动OpenVPN服务即可。该服务会扫描配置文件目录,为每个配置文件启动一个进程进行相应的连接。)
第四、启动服务器
添加IPTABLES规则:
最好的写法
端口规则
1 |
iptables -A INPUT -p udp --dport 1194 -j ACCEPT |
路由规则
1 |
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source VPSIP |
OPENVZ的机器请执行下面这条指令
1 |
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source 184.22.141.74 |
不同的机器,-o eth0 参数可能不一样,具体可输入 ifconfig 查看,搞清 ip所在的网卡号.
同时, 需要将 ip forward 打开. 不要用 echo 1 > /proc/sys/net/ipv4/ip_forward 的方式, 这种方式重启后无效. 先查看一下:
sysctl -a | grep for
#查看结果:
net.ipv4.conf.tun0.mc_forwarding = 0
net.ipv4.conf.tun0.forwarding = 1
net.ipv4.conf.eth0.mc_forwarding = 0
net.ipv4.conf.eth0.forwarding = 1
net.ipv4.conf.lo.mc_forwarding = 0
net.ipv4.conf.lo.forwarding = 1
net.ipv4.conf.default.mc_forwarding = 0
net.ipv4.conf.default.forwarding = 1
net.ipv4.conf.all.mc_forwarding = 0
net.ipv4.conf.all.forwarding = 1
net.ipv4.ip_forward = 1
如果你的主机上列数值不是为1, 则要将其改成1, 例如:
sysctl -w net.ipv4.ip_forward=1
依此类推.
之后就可以启动服务器了
1 2 3 4 5 |
/usr/sbin/openvpn --config /usr/local/etc/server.conf & #服务端启动用命令 /usr/sbin/openvpn –config /usr/local/etc/client.conf & #客户端起动用命令 #在ubunt中也可以这样启动 service openvpn start #这样默认使用的是server.conf的配置文件 |
这样一台openvpn服务器就配置好了。
下面说下各个证书的作用:
Filename | Needed By | Purpose | Secret |
ca.crt | server + all clients | Root CA certificate | NO |
ca.key | key signing machine only | Root CA key | YES |
dh{n}.pem | server only | Diffie Hellman parameters | NO |
server.crt | server only | Server Certificate | NO |
server.key | server only | Server Key | YES |
client1.crt | client1 only | Client1 Certificate | NO |
client1.key | client1 only | Client1 Key | YES |
client2.crt | client2 only | Client2 Certificate | NO |
client2.key | client2 only | Client2 Key | YES |
client3.crt | client3 only | Client3 Certificate | NO |
client3.key | client3 only | Client3 Key | YES |
参考文档:
http://a18zhizao.com/y2010/1302_vps-on-the-installation-and-use-openvpn.html
http://www.xiaohui.com/dev/server/20070514-install-openvpn.htm
http://www.linuxfly.org/post/84/
http://www.lenghost.cn/vps/burstnet-vps-openvpn-install/
http://www.black-xstar.com/blog/693.html
http://www.linuxfly.org/post/86/
http://www.shorewall.net/OPENVPN.html
http://openvpn.net/index.php/open-source/documentation/install.html (官方)
http://www.douhua.im/2010/01/06/ubuntu-server-install-openvpn-server/
声明: 本文由( liva )原创编译,转载请保留链接: ubuntu 下 openvpn的配置
ubuntu 下 openvpn的配置:目前有1 条留言
发表评论

I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all site owners and bloggers made good content as you did, the internet will be much more useful than ever before.
2013-08-04 上午 11:17