最后更新日期:2011-02-11
基于伟大的 GFW 越来越牛B,网站的正常维护如 FTP、pop & smtp 的邮件收发、在 google 查技术资料,都经常被 GFW 强行断开。为了解决这个问题,于是我在自己的国外主机上安装了一个 OpenVPN,当时记录了一下安装的经过。
今天正好又有一个朋友问及 OpenVPN 安装的事情,于是我重新整理一下这篇Linux 下 OpenVPN 安装和 Windows OpenVPN GUI 安装笔记(https://www.xiaohui.com/dev/server/20070514-install-openvpn.htm), 希望对大家有所帮助。
当时在安装 OpenVPN 的时候,得到了 WenZK 的指导帮助。在此表示感谢。
root@a [/]# modinfo tun filename: /lib/modules/2.4.20-31.9/kernel/drivers/net/tun.o description:如果没有 modinfo 命令, 直接找一下, 看看 kernel 里是否有 tun.o 文件:author: license: "GPL"
find -name tun.o ./lib/modules/2.4.20/kernel/drivers/net/tun.o检查iptables 模块, 查看是否有下列文件:
yum install openssl yum install openssl-devel
cd /
地址: http://www.oberhumer.com/opensource/lzo/download/ 代码:
wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.02.tar.gz
地址: http://openvpn.net/download.html 代码:
wget http://openvpn.net/release/openvpn-2.0.5.tar.gz
cd /lzo-2.02 ./configure make make check make install
代码:
cd /openvpn-2.0.5 ./configure # 或用指定dir: (注:下述命令, 应该在一行写完. 为了方便显示, 这里分成了四行) # ./configure --with-lzo-headers=/usr/local/include # --with-lzo-lib=/usr/local/lib # --with-ssl-headers=/usr/local/include/openssl # --with-ssl-lib=/usr/local/lib make make install
初始化 PKI
(如果没有 export 命令也可以用 setenv [name] [value] 命令)
代码:
cd /openvpn-2.0.5/easy-rsa export D=`pwd` export KEY_CONFIG=$D/openssl.cnf export KEY_DIR=$D/keys export KEY_SIZE=1024 export KEY_COUNTRY=CN export KEY_PROVINCE=GD export KEY_CITY=SZ export KEY_ORG="xiaohui.com" export KEY_EMAIL="your-email [at] xiaohui.com"Build:
代码:
./clean-all ./build-ca Generating a 1024 bit RSA private key ................++++++ ........++++++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [CN]: State or Province Name (full name) [GD]: Locality Name (eg, city) [SZ]: Organization Name (eg, company) [xiaohui.com]: Organizational Unit Name (eg, section) []:xiaohui.com Common Name (eg, your name or your server's hostname) []:server Email Address [your-email [at] xiaohui.com]:# 建立 server key 代码: 代码:
./build-key-server server Generating a 1024 bit RSA private key ......++++++ ....................++++++ writing new private key to 'server.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [CN]: State or Province Name (full name) [GD]: Locality Name (eg, city) [SZ]: Organization Name (eg, company) [xiaohui.com]: Organizational Unit Name (eg, section) []:xiaohui.com Common Name (eg, your name or your server's hostname) []:server Email Address [your-email [at] xiaohui.com]: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:abcd1234 An optional company name []:xiaohui.com Using configuration from /openvpn-2.0.5/easy-rsa/openssl.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName :PRINTABLE:'CN' stateOrProvinceName :PRINTABLE:'GD' localityName :PRINTABLE:'SZ' organizationName :PRINTABLE:'xiaohui.com' organizationalUnitName:PRINTABLE:'xiaohui.com' commonName :PRINTABLE:'server' emailAddress :IA5STRING:'your-email [at] xiaohui.com' Certificate is to be certified until Mar 19 08:15:31 2016 GMT (3650 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated
#生成客户端 key
代码:
./build-key client1 Generating a 1024 bit RSA private key .....++++++ ......++++++ writing new private key to 'client1.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [CN]: State or Province Name (full name) [GD]: Locality Name (eg, city) [SZ]: Organization Name (eg, company) [xiaohui.com]: Organizational Unit Name (eg, section) []:xiaohui.com Common Name (eg, your name or your server's hostname) []:client1 #重要: 每个不同的 client 生成的证书, 名字必须不同. Email Address [your-email [at] xiaohui.com]: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:abcd1234 An optional company name []:xiaohui.com Using configuration from /openvpn-2.0.5/easy-rsa/openssl.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName :PRINTABLE:'CN' stateOrProvinceName :PRINTABLE:'GD' localityName :PRINTABLE:'SZ' organizationName :PRINTABLE:'xiaohui.com' organizationalUnitName:PRINTABLE:'xiaohui.com' commonName :PRINTABLE:'client1' emailAddress :IA5STRING:'your-email [at] xiaohui.com' Certificate is to be certified until Mar 19 08:22:00 2016 GMT (3650 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated
依次类推生成其他客户端证书/key
代码:
./build-key client2 ./build-key client3注意在进入 Common Name (eg, your name or your server's hostname) []: 的输入时, 每个证书输入的名字必须不同.
./build-dh
代码:
tar -cf mykeys.tar /openvpn-2.0.5/easy-rsa/keys cp mykeys.tar /home/xiaohui.comsys/public_html/mykeys.tar将 mykeys.tar 移到 web public(绝对路径因人而异) 上, 然后用 http://www.a.com/mykeys.tar 方式将其下载到本地保存, 然后将其从server删除: 代码:
rm /home/xiaohui.comsys/public_html/mykeys.tar也可以用其他方法把 key file搞到本地,例如 ftp.
从样例文件创建:
代码:
cd $dir/sample-config-files/ # 进入源代码解压目录下的sample-config-files子目录 cp server.conf /usr/local/etc # cp服务器配置文件到/usr/local/etc vi /usr/local/etc/server.conf我建立的server.conf 的内容稍后另附.
代码:
cd $dir/sample-config-files/ #进入源代码解压目录下的sample-config-files子目录 cp client.conf /usr/local/etc #cp客户端配置文件到/usr/local/etc vi /usr/local/etc/client.conf我建立的client.conf 的内容稍后另附.
/usr/local/sbin/openvpn --config /usr/local/etc/server.conf
安装结束后, 进入安装文件夹下的 config 目录, 然后将上面第 10 步建立的 client.conf 文件从 server 上下载到此文件夹, 并更名为 client.ovpn
同时, 将第8 步打包的 mykeys.tar 中的下列证书文件解压到此文件夹:
代码:
ca.crt client1.crt client1.csr client1.key然后双击 client.ovpn 即可启动 openvpn, 或者通过 OpenVPN GUI 的控制启动 VPN.
如果双击 client.ovpn 没有反应, 则在任务栏点 OpenVPN GUI 的小图标右键, 选择 edit config, 将内容复制过去再保存. 然后再点右键中的 connect即可.
如果需要第二台机器上使用 vpn , 进行同样的配置, 只需要将 client1.crt, client1.csr, client1.key 换成对应的 client2.xxx 即可, 然后将 client.ovpn 中的对应key文件值改掉.
代码:
local 70.8.7.6 port 1194 proto udp dev tun ca /openvpn-2.0.5/easy-rsa/keys/ca.crt cert /openvpn-2.0.5/easy-rsa/keys/server.crt key /openvpn-2.0.5/easy-rsa/keys/server.key # This file should be kept secret dh /openvpn-2.0.5/easy-rsa/keys/dh1024.pem server 10.8.0.0 255.255.255.0 client-to-client keepalive 10 120 comp-lzo persist-key persist-tun status /openvpn-2.0.5/easy-rsa/keys/openvpn-status.log verb 4 push "dhcp-option DNS 10.8.0.1" push "dhcp-option DNS 70.88.98.10" # name server 地址, 如何获取见随后说明 push "dhcp-option DNS 70.88.99.11" # name server 地址, 如何获取见随后说明说明: 有些 domain 被 GFW 封掉了, 这时, 如果要访问这些网站, 应该将 server 上的 DNS push 到 client. 上面示例中的 dns ip: 70.88.98.10, 70.88.99.10, 可以在 /etc/resolv.conf 中找到: 代码:
vi /etc/resolv.conf nameserver 70.88.98.10 nameserver 70.88.99.11
代码:
client dev tun proto udp remote 70.8.7.6 1194 persist-key persist-tun ca ca.crt cert client1.crt key client1.key ns-cert-type server comp-lzo verb 3 redirect-gateway def1
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source 70.8.7.6 /etc/init.d/iptables save /etc/init.d/iptables restart不同的机器,-o eth0 参数可能不一样,具体可输入 ifconfig 查看,搞清 ip(70.8.7.6)所在的网卡号.
同时, 需要将 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依此类推.
如果你需要访问一些已经被GFW封掉了域名的网站, 但你的 OpenVPN 服务器没有被封的话,那么你需要在你的主机上开启 name server, 并将 dns push 给 client。 一般的独立主机, 都带有 private dns server.
代码:
rpm -qa | grep bind /etc/init.d/named start另外, 必须保证 server.conf 配置中, 有这三个配置:
代码:
push "dhcp-option DNS 10.8.0.1" push "dhcp-option DNS 70.88.98.10" # name server 地址 push "dhcp-option DNS 70.88.99.11" # name server 地址当 client 连接成功后, 在 cmd 下执行 ipconfig /all, 应该有这类似这样的输出:
代码:
Ethernet adapter Local Area Connection 3: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : TAP-Win32 Adapter V8 Physical Address. . . . . . . . . : 00-FF-AA-B0-60-2B Dhcp Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 10.8.0.6 Subnet Mask . . . . . . . . . . . : 255.255.255.252 Default Gateway . . . . . . . . . : 10.8.0.5 DHCP Server . . . . . . . . . . . : 10.8.0.5 DNS Servers . . . . . . . . . . . : 10.8.0.1 70.88.98.10 70.88.99.11 Lease Obtained. . . . . . . . . . : 2006年5月25日 5:13:52 Lease Expires . . . . . . . . . . : 2007年5月25日 5:13:52
代码:
vi /etc/rc.local然后在最后面加入此行:
代码:
/usr/local/sbin/openvpn --config /usr/local/etc/server.conf > /dev/null 2>&1 &
你可以用 VPN 登录上去之后, 测试 MSN, QQ, IE 等网络应用, 也可以尝试访问一些被 GFW 禁掉的网站, 当然, 前提是你的 VPN 服务器不在境内.
不建议用 VPN 登录 paypal 帐户和 google adsense 帐户. 否则有可能导致帐户受限或带来其他风险.
2011.01.11 补充:今天用 yum -y update 升级了 CentOS 之后,发现 OpenVPN 连接不上去了,老是说用户检验出错。经检查 server log,发现有以下日志:
Thu Feb 10 11:13:07 2011 us=3362 222.244.***.**:45771 TLS: Initial packet from 222.244.***.**:45771, sid=eec450eb 8673ceef
Thu Feb 10 11:13:10 2011 us=798063 222.244.***.**:45771 openvpn_execve: external program may not be called unless '--script-secur ity 2' or higher is enabled. Use '--script-security 3 system' for backward compatibility with 2.1_rc8 and earlier. See --help t ext or man page for detailed info.
Thu Feb 10 11:13:10 2011 us=798127 222.244.***.**:45771 TLS Auth Error: user-pass-verify script failed to execute: /usr/bin/php - q /home/xiaohui/openvpn-manager/admin/openvpn-auth.php
Thu Feb 10 11:13:10 2011 us=798141 222.244.***.**:45771 TLS Auth Error: Auth Username/Password verification failed for peer
经查,原来是 CentOS 在进行 yum update 时,将 OpenVPN 也由2.0 升级到了 2.1。而 OpenVPN 2.1 最大的改变之一,就是加了一个 script-security参数。如果按我的这篇教程进行安装,但装的是 OpenVPN 2.1 及以上版本的话,记得在 server.conf 中再加上这么一行配置即可:
script-security 3相关文章:
抱歉,今天才看到你的邮件。
我没有遇到过这个错误。你确认你的环境变量、软件版本、目录设置,都是按我说的来做的吗?
刚才在GOOGLE搜索了一下,你看看这里:
http://episteme.arstechnica.com/eve/forums/a/tpc/f/96509133/m/732009283831
注意最后的一个贴子,他说他解决了这个问题。你看看对你有用没。
原帖由 FlyFire 于 2009-3-29 21:44 发表
XiaoHui 是不是写了个 HOOK,将网站文章的评论 和 论坛回贴,同步了?
我看见论坛里好多回贴的都标着“通过 XiaoHui.com 评论系统发表”。:)
客户端与服务端的物理链接不通。确认 1194 端口是开的,客户端与服务端能够正常PING通。下面是 OPENVPN 官方解释,你自己对照排除一下:
You get the error message: TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity). This error indicates that the client was unable to establish a network connection with the server.
Solutions:
- Make sure the client is using the correct hostname/IP address and port number which will allow it to reach the OpenVPN server.
- If the OpenVPN server machine is a single-NIC box inside a protected LAN, make sure you are using a correct port forward rule on the server's gateway firewall. For example, suppose your OpenVPN box is at 192.168.4.4 inside the firewall, listening for client connections on UDP port 1194. The NAT gateway servicing the 192.168.4.x subnet should have a port forward rule that says forward UDP port 1194 from my public IP address to 192.168.4.4.
- Open up the server's firewall to allow incoming connections to UDP port 1194 (or whatever TCP/UDP port you have configured in the server config file).