配置centos 6.3 openvpn server 和 window 7 openvpn client

来源:互联网 发布:淘宝差评更改链接 编辑:程序博客网 时间:2024/06/05 05:18

Openvpn 服务器配置

1、安装前的准备工作:

 

[root@data ~]# yum -y installopensslopenssl-devel gcc

下载软件包openvpn-2.2.0.tar.gz服务器端安装包

lzo-2.06.tar.gz压缩隧道通信数据安装包

openvpn-2.2.0-install.exe客户端安装包

软件打包下载地址:

http://down.51cto.com/data/1019725

 

2、安装lzo软件包:

lzo软件包用于压缩隧道通信数据以加快传输速度,操作步骤如下:

[root@data ~]# tar zxvflzo-2.06.tar.gz

[root@data ~]# cd lzo-2.06

[root@data lzo-2.03]#./configure--prefix=/usr/ && make && make install

 

4、安装openvn:

[root@data ~]# tar zxvfopenvpn-2.2.0.tar.gz

[root@data ~]# cdopenvpn-2.2.0

[root@data openvpn-2.2.0]#./configure--with-lzo-lib=/usr/ && make && make install

此处如果出现如下报错:configure:error: libpamrequired but missing

64位系统直接 yum install pam-devel.X86_64

如果你服务器是32位的

yum install pam-devel.i686

 

(1、)建立CA的详细信息

 

5、OpenVPN 服务器的配置

 

[root@data ~]# cdopenvpn-2.2.0/easy-rsa/2.0/

[root@data 2.0]# vim vars

exportKEY_COUNTRY="CN"\\你所在的国家,两个字符为限

exportKEY_PROVINCE="SH"\\你所在的省份

export KEY_CITY="SHANGHAI"\\你所在的城市

exportKEY_ORG="51CTO"\\你所在的组织

exportKEY_EMAIL="youyou@126.com"\\你的电子邮件

export KEY_OU="51CTO.com" \\你所在的单位

接下来输入如下命令:

 

[root@data 2.0]# sourcevars//此命令输入后会显示如下信息

NOTE: If you run ./clean-all,I will bedoing a rm -rf on /root/openvpn-2.2.0/easy-rsa/2.0/keys

 

接着输入下面的命令:

[root@data 2.0]# ./clean-all

[root@data 2.0]# ./build-ca//

一路回车即可会生成 root CA证书

 

Generating a 1024 bit RSAprivate key

.........................................++++++

.......................................................................................++++++

writing new private key to'ca.key'

-----

You are about to be asked toenterinformation that will be incorporated

into your certificaterequest.

What you are about to enteris what iscalled a Distinguished Name or a DN.

There are quite a few fieldsbut you canleave some blank

For some fields there will bea defaultvalue,

If you enter '.', the fieldwill be leftblank.

-----

Country Name (2 letter code)[CN]:

State or Province Name (fullname) [SH]:

Locality Name (eg, city)[SHANGHAI]:

Organization Name (eg,company) [51CTO]:

Organizational Unit Name (eg,section) [51cto.com]:

Common Name (eg, your name oryour server'shostname) [51CTO CA]:

Name []:

Email Address[youyou@126.com]:

 

可以看到ca.crt ,ca.key已经生成

为服务器生成Diffie-Hellman文件,后面配置OpenVPN Server时所需要用到的文件命令如下:

./build-dh

(2)、为服务器生成证书和密钥,命令如下:

[root@862.0]#./build-key-serverserver

Generating a 1024 bit RSAprivate key

.......++++++

.................................................++++++

writing new private key to'server.key'

-----

You are about to be asked toenterinformation that will be incorporated

into your certificaterequest.

What you are about to enteris what iscalled a Distinguished Name or a DN.

There are quite a few fieldsbut you canleave some blank

For some fields there will bea defaultvalue,

If you enter '.', the fieldwill be leftblank.

-----

Country Name (2 letter code)[CN]:

State or Province Name (fullname) [SH]:

Locality Name (eg, city)[SHANGHAI]:

Organization Name (eg,company) [51CTO]:

Organizational Unit Name (eg,section) [51cto.com]:

Common Name (eg, your name oryour server'shostname) [server]:

Name []:

Email Address[youyou@126.com]:

 

Please enter the following'extra'attributes

to be sent with yourcertificate request

A challenge password[]:123456

An optional company name[]:51cto.com

Using configurationfrom/root/soft/openvpn-2.2.0/easy-rsa/2.0/openssl.cnf

Check that the requestmatches thesignature

Signature ok

The Subject's DistinguishedName is asfollows

countryName:PRINTABLE:'CN'

stateOrProvinceName:PRINTABLE:'SH'

localityName:PRINTABLE:'SHANGHAI'

organizationName:PRINTABLE:'51CTO'

organizationalUnitName:PRINTABLE:'51cto.com'

commonName:PRINTABLE:'server'

emailAddress:IA5STRING:'youyou@126.com'

Certificate is to becertified until Nov 1908:20:18 2023 GMT (3650 days)

Sign the certificate? [y/n]:y

 

 

1 out of 1 certificaterequests certified,commit? [y/n]y

Write out database with 1 newentries

 

 

为客户端生成证书,这时候生成的证书要分发给客户端来用的。

 

[root@data 2.0]# ./build-key  ace.yuan

 

正常结束上面过程后我们可以看到keys目录生成了正常的证书文件,用以下命令可以看到:

[root@data 2.0]# ll keys/

 

( 3 )修改OpenVPN的配置文件/etc/server.conf

1)、复制示例文件到/etc目录下,然后再在其基础下进行更改,命令如下:

[root@data 2.0]# cp/root/openvpn-2.2.0/sample-config-files/server.conf/etc/server.conf

2、)编辑/etc/server.conf,注意以下几个重点:

一是将proto udp 改为proto tcp ,即服务启动用TCP1194端口,也可以改为80端口。

二是将ca那4行内容改成如下所示的形式(记得写绝对路径):

ca      /root/openvpn-2.2.0/easy-rsa/2.0/keys/ca.crt

ca后面接的是 root CA 它使用的是buid-ca生成的,用于验证客户端证书是否合法的。

cert    /root/openvpn-2.2.0/easy-rsa/2.0/keys/server.crt

key    /root/openvpn-2.2.0/easy-rsa/2.0/keys/server.key

上面是服务器对应的证书等相关文件

dh/root/openvpn-2.2.0/easy-rsa/2.0/keys/dh1024.pem 

三是将server那行改为如下内容:

server 10.8.0.0 255.255.255.0//这是OpenVPN服务器启动时为VPN网络分配的网段,注意不要与公网中的IP发生冲突。


四是将verb3改为verb5

 

 

3、)启动OpenVPN

1、关闭iptables和selinux,以免对OpenVPN造成不必要的干扰

2、开启系统中自带的IP转发功能,命令如下:

[root@data keys]# echo"1">/proc/sys/net/ipv4/ip_forward

 

3、用命令启动OpenVPN,命令如下:

[root@data keys]#/usr/local/sbin/openvpn--config /etc/server.conf

如果看到InitializationSequence Completed 表示启动成功

用lsof -i:1194来验证一下

[root@datakeys]# lsof -i:1194

COMMAND PID USER FD TYPEDEVICE SIZE/OFFNODE NAME

openvpn 20074 root 5u IPv494836 0t0 TCP*:openvpn (LISTEN)

 

 

Openvpn 客户端配置

 

安装OpenVPN

首先需要安装OpenVPN客户端。一般来说直接使用yum即可(在Debian系中是apt-get)。执行如下命令安装:

1

[root@www ~]# yum installopenvpn

稍等片刻将自动安装好openvpn需要的软件包。安装完成后,应该出现

/etc/openvpn/

文件夹。

 

配置OpenVPN

作为客户端,OpenVPN并没有特定的配置文件,而是由服务器提供方给出一个配置文件。对于认证,OpenVPN提供了两种认证方法:基于用户名/密码的认证与SSL证书认证。用户名/密码的认证方法无法(或较难)限制一个账号同时连接多个客户端,而采用证书,则可保证同一证书同一时间只能有一个客户端连接。当然,这些都是由服务器端决定的,不需要客户端进行选择。

首先将OpenVPN服务器提供商发给你的配置文件解压,并将所有文件都复制到 /etc/openvpn/中。

这些文件中至少包含一个.ovpn文件;如果服务器需要证书认证,则应该还存在另外三个证书文件。

看懂OpenVPN配置格式。下面是一个.ovpn配置示例:

client

dev tap(window 用tap,linux 用tun0)

proto tcp-client

remote 192.168.135.75 1194

resolv-retry infinite

nobind

mute-replay-warnings

redirect-gateway

ca  /etc/ca.crt

cert /etc/client.crt

key /etc/client.key

comp-lzo

verb 4

一般来说,红色的内容可能需要你进行修改。将红色的内容修改成这三个文件的实际位置。然后保存即可。

 Chmod 600 /etc/ca.crt /etc/client.crt /etc/client.key 需要从服务器拷贝

连接OpenVPN

在配置好.ovpn文件后,执行

openvpn /etc/***.ovpn

即可连接服务器了。注意,上面的参数应该换成你的配置文件实际位置。

此时,终端会回显很多连接日志。如果连接不成功,则可以通过这些日志来确定出错位置。如果要断开,只需要通过Ctrl+C强制终止即可。

上面的命令在实际中并不方便,因为它要占用一个独立的终端。在测试成功后,使用以下命令即可在后台连接OpenVPN:

openvpn /etc/***.ovpn >/dev/null &

添加默认路由 route add -net0.0.0.0/0 dev tun0

route add -net 0.0.0.0 gw10.8.0.5

值得称赞的是,openvpn非常智能,在连接异常中断、无法连接服务器、断网的情况下,它会自动重连。因此,如果希望开机即自动连接OpenVPN,或者是VPN常年在线,则可将上述命令行加入

/etc/rc.local

中。注意,命令末尾的&符号不能省略,否则将可能阻塞系统的正常启动。

 

0 0
原创粉丝点击