在CentOS的VPS下安装OpenVPN
上回介绍了在CentOS下安装PPTP的VPN的方法,由于PPTP对环境的要求很高,大多数的VPS都是基于OpenVZ虚拟化技术的,所以无法安装。
这回介绍另一种VPN——OpenVPN,能在绝大多数VPS上安装,而且功能更加强大,比如能穿透中国移动的cmwap代理实现cmnet的功能等等。
不过OpenVPN有一个致命缺点,就是绝大多数移动设备不支持,电脑上使用需要安装复杂的客户端。
网上有很多的教程,但大都不适合VPS或者不完善,我根据我自己安装的经验,记录下来以便查阅。
以下所有命令我都加了下划线,即加下划线的一定是在类似[root@vps ~]#的提示符下输入。
SSH登陆VPS,我的系统是32的CentOS 5.4,如果用别的Linux发行版也差不多,有些命令需要修改下就是了。
OpenVPN需要TUN支持,大多数VPS默认都没有开启,你可以用这个命令检测:cat /dev/net/tun
如果返回信息为:cat: /dev/net/tun: File descriptor in bad state 说明正常,否则发个ticket给VPS公司让他们帮忙开吧。
另外如果你需要连上OpenVPN后能访问互联网,还需要iptables_nat模块支持,用这个命令检测:iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE
如果返回信息为:iptables: Unknown error 4294967295 说明正常,否则同样需要发个ticket让VPS公司帮忙开通。
环境准备好之后,我们正式开始安装OpenVPN了。网上的教程绝大多数都是用源代码编译方式安装的,但我觉得这种方式不便于以后升级维护,这里我们使用yum来安装。
默认情况下centos的yum源没有OpenVPN的,先安装EPEL这个东西,使用命令:rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
成功后yum源里面就有OpenVPN了,直接使用命令yum -y install openvpn

这里就体现了yum安装的好处,比如OpenVPN需要lzo支持,安装的时候会检测系统,没有的组件会自动安装进去。
我们来找一下安装到哪去了,使用命令:locate easy-rsa
找出来了原来在这里:/usr/share/openvpn/easy-rsa 大家应该都是一样的。
我们把easy-rsa这个文件夹移出来,用命令:cp -R /usr/share/openvpn/easy-rsa /etc/openvpn/
然后cd /etc/openvpn/easy-rsa/2.0进入,生成OpenVPN需要的证书。
用vi vars来编辑环境变量,这里涉及到编辑器vi的用法,不会用就自己google一下。
把最后几行根据实际情况修改:
export KEY_COUNTRY="CN"
export KEY_PROVINCE="GD"
export KEY_CITY="GZ"
export KEY_ORG="Black-Xstar Net Empire"
export KEY_EMAIL="webmaster [at] black-xstar.com"
保存后运行. vars设置生效。
接下来运行./build-ca server创建证书颁发机构。
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) [GZ]:回车
Organization Name (eg, company) [Black-Xstar Net Empire]:回车
Organizational Unit Name (eg, section) []:回车
Common Name (eg, your name or your server's hostname) [Black-Xstar Net Empire CA]:回车
Name []:回车
Email Address [webmaster@black-xstar.com]:回车
注意红色的地方,需要按回车的。
创建CA之后来生成服务器证书,输入./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) [GZ]:回车
Organization Name (eg, company) [Black-Xstar Net Empire]:回车
Organizational Unit Name (eg, section) []:回车
Common Name (eg, your name or your server's hostname) [server]:回车
Name []:回车
Email Address [webmaster@black-xstar.com]:回车Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:回车
An optional company name []:回车
Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryNameRINTABLE:'CN'
stateOrProvinceNameRINTABLE:'GD'
localityNameRINTABLE:'GZ'
organizationNameRINTABLE:'Black-Xstar Net Empire'
commonNameRINTABLE:'server'
emailAddress :IA5STRING:'webmaster@black-xstar.com'
Certificate is to be certified until Nov 18 17:25:15 2019 GMT (3650 days)
Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
同样注意红色字的部分,是需要人工输入的。
服务器证书生成完了,我们来生成客户端证书,理论上每个OpenVPN用户都有独立的证书,我们先来生成一个试试。
输入命令:./build-key client1,这里的client1是客户端名称,如果第二个就是client2了。
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) [GZ]:回车
Organization Name (eg, company) [Black-Xstar Net Empire]:回车
Organizational Unit Name (eg, section) []:回车
Common Name (eg, your name or your server's hostname) [client1]:回车
Name []:回车
Email Address [webmaster@black-xstar.com]:回车Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:回车
An optional company name []:回车
Using configuration from /etc/openvpn/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryNameRINTABLE:'CN'
stateOrProvinceNameRINTABLE:'GD'
localityNameRINTABLE:'GZ'
organizationNameRINTABLE:'Black-Xstar Net Empire'
commonNameRINTABLE:'client1'
emailAddress :IA5STRING:'webmaster@black-xstar.com'
Certificate is to be certified until Nov 18 17:31:21 2019 GMT (3650 days)
Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
注意红色字的部分,和上面生成服务器证书差不多。
最后生成Diffie Hellman参数:./build-dh,这个需要一点时间的。
完成上面的过程后,把/etc/openvpn/2.0/keys里面的东西下载回来。
接下来我们开始配置OpenVPN了,我的配置文件只是一个参考,可以根据实际情况修改。
首先cd ..回到上一级目录,然后vi server.conf新建一个配置文件,输入下面内容:
port 443
proto tcp
dev tun
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
server 10.8.0.0 255.255.255.0
push "redirect-gateway def1"
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
client-to-client
keepalive 10 120
comp-lzo
persist-key
persist-tun
verb 3
其中DNS服务器地址可以换成主机商的,也可以和我一样用opendns。另外我有了tcp协议和443端口,是为了方便我在cmwap下使用,也可以换别的。
到这里为止OpenVPN就配置好了,接下来我们设置外网访问。
输入vi /etc/sysctl.conf开始编辑,找到net.ipv4.ip_forward = 0改成net.ipv4.ip_forward = 1保存。然后执行sysctl -p这个命令。
输入iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source 1.2.3.4添加规则,注意最后1.2.3.4改成你的VPS的IP地址。
完成后用/etc/init.d/iptables save保存iptables设置,然后/etc/init.d/iptables restart重新启动下。
把OpenVPN添加到开机启动,用vi /etc/rc.local进入编辑,在后面加入/usr/sbin/openvpn --config /etc/openvpn/server.conf &这一行。
需要在服务器上完成的操作到这里就结束了,输入openvpn --config /etc/openvpn/server.conf &启动。
和PPTP不一样,OpenVPN需要安装客户端才行,在http://www.openvpn.net/index.php/open-source/downloads.html下载最新版本的Windows Installer安装。
然后在下载回来keys文件夹里面找到ca.crt、client1.crt和client1.key这三个文件,放到C:\Program Files\OpenVPN\config里面。
同时在这里面新建一个名字为“client1.ovpn”的文本文件,输入下面内容:
client
dev tun
proto tcp
remote 1.2.3.4 443
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
ns-cert-type server
comp-lzo
verb 3
把第四行的1.2.3.4换成VPS的IP地址,然后保存。
在开始菜单里面找到OpenVPN GUI并运行,Vista和Win7下需要管理员身份运行。点Connect后等一下,是不是成功连上去了?
打开youtube或twitter(follow我@billzhong)试试,爽吧!
特别感谢 Hello, Host! 提供支持:https://hellohost.net/vps.html
最后广告一下,我的 VPN 服务: http://pptp.us 提供PPTP、L2TP和OpenVPN三种方式哦。
参考文章:
http://www.xiaohui.com/dev/server/20070514-install-openvpn.htm
黑猩猩开始用linux替代windows了?
@jason
欢迎高手指导。
服务器而已啦。
@Black-Xstar
我是小白~
@jason
高手都喜欢这么说~
@Black-Xstar
囧,你才是高手啊,那么长一串的域名
最近发了。。还有vps了
保存后运行. vars设置生效。
接下来运行./build-key-server server创建证书颁发机构。
应该修改为
保存后运行. vars设置生效。
接下来运行./build-ca server创建证书颁发机构。
在IE6下侧边栏错位了哦~
楼主用的什么软件管理vps的啊~我用的putty,但是在vi编辑的时候,经常显示不正常!
VPN,很好用!~~
不错的资料.收藏一下.
不过代码里有的是 QQ 表情了..
/usr/sbin/openvpn --config /etc/openvpn/server.conf &
这句话照抄了,忘了改。结果vps reload了五六次。
请问,如果我赚了kloxo 再来装这个会有什么问题吗?
@A.shun
哈哈,其实vps很便宜的。
@James
谢谢,已经改正。
@老T
没有啊。
@HJin_me
secureCRT
@flash软件
呃,这个没办法。
@上善若水
什么意思?
@feeltown
不清楚哦。
博主你好,我是按照你这个做法给我的VPS装上OpenVPN的
但我碰到一个奇怪的问题,不知道你有没有碰到过:
一开始上网站,连接的时候很快,但一旦你上了那些被 墙 的网站,网速立刻慢下来,最后还断了. 过一会再来,速度恢复,但也是很快就慢下来
是不是墙在干扰SSL的传送呢? 还是配置有问题? 谢谢了
@erv2
呃,好吧。
我朋友大面积部署的openvpn也没这个问题。
我还是觉得是vps的问题,可能是母机问题。
博主的回复也真快!
那你的建议就是跟VPS的support反映一下对吧?
@erv2
这问题主机商不管的吧,除非是managed的vps。
你现在用哪家vps呢?
的确不是managed的,我用的是Burst.net
博主可以留个gtalk给我吗? 或者直接在gmail里加我留在这里的email,我们可以在那里交流一下
无论如何,谢谢你的答复
@erv2
给我发邮件吧,不太喜欢im聊天,比较浪费时间。
我的email在这里:http://applife.net/about
在执行sysctl -p的时候出现了错误error: "Operation not permitted" setting key "net.ipv4.tcp_syncookies"
请问是怎么回事?google了很多,但没找到解答。谢谢~
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j MASQUERADE
之后无回显,是不是说明这个模块没有启用哦?
请教:
我的windows GUI
Sun May 30 16:27:55 2010 SENT CONTROL [server]: 'PUSH_REQUEST' (status=1)
Sun May 30 16:27:55 2010 PUSH: Received control message: 'PUSH_REPLY,route 10.0.161.6 255.255.255.255,route 10.0.161.0 255.255.255.0,route 10.99.0.0 255.255.255.0,ping 10,ping-restart 120,ifconfig 10.99.0.6 10.99.0.5'
Sun May 30 16:27:55 2010 OPTIONS IMPORT: timers and/or timeouts modified
Sun May 30 16:27:55 2010 OPTIONS IMPORT: --ifconfig/up options modified
Sun May 30 16:27:55 2010 OPTIONS IMPORT: route options modified
Sun May 30 16:27:57 2010 There are no TAP-Win32 adapters on this system. You should be able to create a TAP-Win32 adapter by going to Start -> All Programs -> OpenVPN -> Add a new TAP-Win32 virtual ethernet adapter.
Sun May 30 16:27:57 2010 Exiting
Tue Jun 1 19:50:04 2010 MULTI: multi_create_instance called
Tue Jun 1 19:50:04 2010 Re-using SSL/TLS context
Tue Jun 1 19:50:04 2010 LZO compression initialized
Tue Jun 1 19:50:04 2010 Control Channel MTU parms [ L:1544 D:140 EF:40 EB:0 ET:0 EL:0 ]
Tue Jun 1 19:50:04 2010 Data Channel MTU parms [ L:1544 D:1450 EF:44 EB:135 ET:0 EL:0 AF:3/1 ]
Tue Jun 1 19:50:04 2010 Local Options hash (VER=V4): 'c0103fa8'
Tue Jun 1 19:50:04 2010 Expected Remote Options hash (VER=V4): '69109d17'
Tue Jun 1 19:50:04 2010 TCP connection established with 160.10.252.124:1302
Tue Jun 1 19:50:04 2010 Socket Buffers: R=[131072->131072] S=[131072->131072]
Tue Jun 1 19:50:04 2010 TCPv4_SERVER link local: [undef]
Tue Jun 1 19:50:04 2010 TCPv4_SERVER link remote: 160.10.252.124:1302
Tue Jun 1 19:50:04 2010 160.10.252.124:1302 TLS: Initial packet from 160.10.252.124:1302, sid=e8e32eba 484c96bd
Tue Jun 1 19:50:04 2010 160.10.252.124:1302 Connection reset, restarting [-1]
Tue Jun 1 19:50:04 2010 160.10.252.124:1302 SIGUSR1[soft,connection-reset] received, client-instance restarting
Tue Jun 1 19:50:04 2010 TCP/UDP: Closing socket
Tue Jun 1 19:50:07 2010 MULTI: multi_create_instance called
Tue Jun 1 19:50:07 2010 Re-using SSL/TLS context
Tue Jun 1 19:50:07 2010 LZO compression initialized
Tue Jun 1 19:50:07 2010 Control Channel MTU parms [ L:1544 D:140 EF:40 EB:0 ET:0 EL:0 ]
Tue Jun 1 19:50:07 2010 Data Channel MTU parms [ L:1544 D:1450 EF:44 EB:135 ET:0 EL:0 AF:3/1 ]
Tue Jun 1 19:50:07 2010 Local Options hash (VER=V4): 'c0103fa8'
Tue Jun 1 19:50:07 2010 Expected Remote Options hash (VER=V4): '69109d17'
Tue Jun 1 19:50:07 2010 TCP connection established with 160.10.252.124:1303
Tue Jun 1 19:50:07 2010 Socket Buffers: R=[131072->131072] S=[131072->131072]
Tue Jun 1 19:50:07 2010 TCPv4_SERVER link local: [undef]
Tue Jun 1 19:50:07 2010 TCPv4_SERVER link remote: 160.10.252.124:1303
Tue Jun 1 19:50:07 2010 160.10.252.124:1303 Connection reset, restarting [0]
Tue Jun 1 19:50:07 2010 160.10.252.124:1303 SIGUSR1[soft,connection-reset] received, client-instance restarting
Tue Jun 1 19:50:07 2010 TCP/UDP: Closing socket
我感觉你的 blog 的 icon 很像“流光”啊,你说呢。
客户端连接,怎么还要用户名和密码啊,教程里没有提及啊。