欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

在Linux系统的VPS上安装配置OpenVPN的详细教程

程序员文章站 2022-09-07 16:32:09
这篇文章主要介绍了在Linux系统的VPS上安装配置OpenVPN的方法,并讲解了Windows下客户端连接的相关配置,需要的朋友可以参考下... 16-01-15...

openvpn在安全性上比pptp,l2tp和ipsec都高,因为它通过三个证书的安全验证,实现了整个通信链路的完整加密。

安装步骤:

一、首先检查vps上的tun设备是否可用,如果不可用需要联系客服打开,否则openvpn无法启用。
通过root用户登陆,执行

复制代码
代码如下:

#cat /dev/net/tun
cat: /dev/net/tun: file descriptor in bad state

如果现实结果如上所示,则表示tun设备可用,其它则表示设备异常,无法安装openvpn。

二、下载安装所需软件包:


复制代码
代码如下:

wget ftp://rpmfind.net/linux/epel/5/i386/pkcs11-helper-devel-1.07-2.el5.1.i386.rpm
wget ftp://rpmfind.net/linux/epel/5/i386/pkcs11-helper-1.07-2.el5.1.i386.rpm
wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.04.tar.gz
wget http://swupdate.openvpn.net/community/releases/openvpn-2.1.4.tar.gz

三、使用yum安装程序所需开发包

复制代码
代码如下:

yum install -y openssl openssl-devel automake pkgconfig iptables


四、安装openvpn服务端
1.安装pkcs11-helper rpm包

复制代码
代码如下:

rpm -ivh pkcs11-helper-1.07-2.el5.1.i386.rpm
rpm -ivh pkcs11-helper-devel-1.07-2.el5.1.i386.rpm

2.安装 lzo

复制代码
代码如下:

tar zxvf lzo-2.04.tar.gz
cd lzo-2.04/
./configure –prefix=/opt/lzo
make
make check
make install
cd ../

3.安装 openvpn

复制代码
代码如下:

tar zxvf openvpn-2.1.4.tar.gz
cd openvpn-2.1.4
./configure –prefix=/opt/openvpn –with-lzo-headers=/opt/lzo/include –with-lzo-lib=/opt/lzo/lib –with-ssl-headers=/usr/include/openssl –with-ssl-lib=/usr/lib
make
make install

然后,下面就开始key证书文件的创建,总共需要创建三个,即ca,server key,client key。

4.生成证书key

复制代码
代码如下:

cp -r easy-rsa /opt/openvpn/
cd /opt/openvpn/easy-rsa/2.0/
vim export_new_var

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=bj #省份
export key_city=bj #城市
export key_org=”btschina.com” #组织
export key_email=”root@btschina.com” #邮箱
source export_new_var
./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) [bj]:
locality name (eg, city) [bj]:
organization name (eg, company) [btschina.com]:
organizational unit name (eg, section) []:btschina
common name (eg, your name or your server’s hostname) [btschina.com ca]:btschina
.com
name []:btschina
email address [root@btschina.com]:


 

5.建立 server key

复制代码
代码如下:

[root@centos 2.0]# ./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) [bj]:
locality name (eg, city) [bj]:
organization name (eg, company) [btschina.com]:
organizational unit name (eg, section) []:btschina
common name (eg, your name or your server’s hostname) [server]:btschina.com
name []:btschina
email address [root@btschina.com]:</p> <p>please enter the following ‘extra’ attributes
to be sent with your certificate request
a challenge password []:123456
an optional company name []:btschina.com
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
countryname :p rintable:’cn’
stateorprovincename :p rintable:’bj’
localityname :p rintable:’bj’
organizationname :p rintable:’btschina.com’
organizationalunitname:printable:’btschina’
commonname :p rintable:’btschina.com’
name :p rintable:’btschina’
emailaddress :ia5string:’root@btschina.com’
certificate is to be certified until apr 12 08:21:52 2021 gmt (3650 days)
sign the certificate? [y/n]:y

 

6.生成客户端 key

复制代码
代码如下:

[root@centos 2.0]# ./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) [bj]:
locality name (eg, city) [bj]:
organization name (eg, company) [btschina.com]:
organizational unit name (eg, section) []:btschina
common name (eg, your name or your server’s hostname) [client1]:
name []:btschina
email address [root@btschina.com]:

please enter the following ‘extra’ attributes
to be sent with your certificate request
a challenge password []:123456
an optional company name []:btschina.com
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
countryname :p rintable:’cn’
stateorprovincename :p rintable:’bj’
localityname :p rintable:’bj’
organizationname :p rintable:’btschina.com’
organizationalunitname:printable:’btschina’
commonname :p rintable:’client1′
name :p rintable:’btschina’
emailaddress :ia5string:’root@btschina.com’
certificate is to be certified until apr 12 08:24:33 2021 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

1 out of 1 certificate requests certified, commit? [y/n]y
write out database with 1 new entries
data base updated


如果想生成多个client key的话,重复以上步骤,修改client1为client2,client3……即可。

至此,所有的key都已经生成完毕,如果你想删除这些key,重新生成的话,执行以下命令。

复制代码
代码如下:

source var
./clean-all

7.生成 diffie hellman 参数

复制代码
代码如下:

./build-dh

8.将 keys 下的所有文件打包下载到本地

复制代码
代码如下:

tar -cf keys.tar keys

9.创建服务端配置文件

复制代码
代码如下:

vi /opt/openvpn/etc/server.conf

内容如下:
port 1194
proto udp

dev tun

ca /opt/openvpn/easy-rsa/2.0/keys/ca.crt
cert /opt/openvpn/easy-rsa/2.0/keys/server.crt
key /opt/openvpn/easy-rsa/2.0/keys/server.key
dh /opt/openvpn/easy-rsa/2.0/keys/dh1024.pem

server 10.10.20.0 255.255.255.0

client-to-client
keepalive 10 120

comp-lzo

persist-key
persist-tun
status /opt/openvpn/easy-rsa/2.0/keys/openvpn-status.log
verb 4

push “dhcp-option dns 10.10.20.1′
push “dhcp-option dns 8.8.8.8′
push “dhcp-option dns 8.8.4.4′
 

10.创建客户端配置文件

复制代码
代码如下:

vi /opt/openvpn/etc/client.conf

内容如下
client

dev tun
proto udp

remote 204.74.215.57 1194 #请换成自己ip
persist-key
persist-tun
ca /opt/openvpn/easy-rsa/2.0/keys/ca.crt
cert /opt/openvpn/easy-rsa/2.0/keys/client1.crt
key /opt/openvpn/easy-rsa/2.0/keys/client1.key
ns-cert-type server
comp-lzo
verb 3

redirect-gateway def1
route-method exe
route-delay 2
 

12.设置 openvpn 服务器 reboot后自动启动 openvpn

复制代码
代码如下:

vi /etc/rc.local

加入:

复制代码
代码如下:

/opt/openvpn/sbin/openvpn –config /opt/openvpn/etc/server.conf > /dev/null 2>&1 &

运行openvpn服务端

复制代码
代码如下:

/opt/openvpn/sbin/openvpn –config /opt/openvpn/etc/server.conf > /dev/null 2>&1 &

检查服务端是否启动
执行
复制代码
代码如下:
netstat -lnp

如果看到有 0.0.0.0:1194 则证明服务端已经成功启动。

五、防火墙规则设置
1.开放openvpn服务器端口

复制代码
代码如下:

iptables -a input -p udp -m state –state new -m udp –dport 1194 -j accept

2.启用nat映射,实现共享上网

复制代码
代码如下:

iptables -t nat -a postrouting -s 10.10.20.0/255.255.255.0 -o eth0 -j masquerade

3.开启数据转发

复制代码
代码如下:

sysctl -w net.ipv4.ip_forward=1

六、openvpn gui for windows 客户端安装
1.下载并安装 .net framwork 3.5 sp1
由于openvpn客户端需要 .net framwork 3.5 sp1 以上的支持,所以很遗憾,我们不得不先安装它。
下载地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=333325fd-ae52-4e35-b531-508d977d32a6&displaylang=zh-cn

2.下载并安装openvpn客户端
下载地址:http://swupdate.openvpn.net/downloads/openvpn-client.msi

3.创建客户端配置文件

进入程序目录,c:\program files\openvpn technologies\openvpn client\etc\profile
创建配置文件client.ovpn,内容如下:
client

dev tun
proto udp

remote 204.74.215.57 1194 #请换成自己ip

persist-key
persist-tun
ca ca.crt
cert client1.crt
key client1.key
ns-cert-type server
comp-lzo
verb 3

redirect-gateway def1
route-method exe
route-delay 2
4.将所需的证书等文件放入客户端目录
将之前压缩的keys.tar压缩文件解压,并将目录下的所有文件移动到目录:
c:\program files\openvpn technologies\openvpn client\etc\profile

5.启动openvpn客户端
启动openvpn客户端软件,点击添加连接配置,选择本地文件导入,然后导入刚刚创建的client.ovpn文件。
如下图所示:
在Linux系统的VPS上安装配置OpenVPN的详细教程

在Linux系统的VPS上安装配置OpenVPN的详细教程

然后界面上会出现一个client1选项,点击即可登录
在Linux系统的VPS上安装配置OpenVPN的详细教程

登录成功
在Linux系统的VPS上安装配置OpenVPN的详细教程

至此,整个openvpn的安装过程就已经完成了,如果你也是国外的vps主机的话,那就开始体验翻过gfw的快乐吧。

七、卸载openvpn
如果你觉得openvpn用起来太过于麻烦或其它原因想卸载openvpn,那么,请执行以下操作。

复制代码
代码如下:

killall openvpn
rpm -e pkcs11-helper-1.07-2.el5.1.i386.rpm
rpm -e pkcs11-helper-devel-1.07-2.el5.1.i386.rpm
rm -rf /opt/lzo
rm -rf /opt/openvpn