|
实验环境:服务器:ubuntu server 12.04 客户端:win7 旗舰版+openvpn GUI
一、Openvpn服务器端配置:
1. 安装
Ubuntu中安装OpenVPN极其简单:
#sudo apt-get install openvpn
拷贝openvpn相应配置文件到/etc/openvpn目录下
#sudo cp -R /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn/图片
#sudo vim /etc/openvpn/easy-rsa/2.0/vars /*做以下修改,其实也不用改业没关系*/
主要编辑以下部分,注意KEY_COUNTRY 和KEY_PROVINCE 都以两个大写字母表示
export KEY_COUNTRY="" #国家
export KEY_PROVINCE="" #省份
export KEY_CITY="" #城市
export KEY_ORG="" #机构
export KEY_EMAIL="" #Email
编辑完后保存!
其实这里的配置,不重要,配不配都无所谓~!
2. 设置认真机构,产生证书
#cd /etc/openvpn/easy-rsa/2.0/
#sudo chown -R root:admin . /*后面这个点不能忘了,虽然现在还不知道什么意思*/
#sudo chmod g+w .
执行该配置文件,使之生效 #source ./vars
提示没有openssl.cnf,所以把openssl-1.0.0.cnf 重命名为openssl.cnf即可.
查看所配置参数是否生效 env|grep KEY
清除keys文件下密钥文件,进行初始化 #./clean-all
建立ca文件 #./build-ca 选项默认即可。
确认产生的ca文件
产生server端key ./build-key-server server01-----------名字随意图片
查看所产生的server key文件
建立client端key文件 ./build-key client01 -------名字随意图片
查看clinet1_0_182新产生的client key文件
产生密码动态库
#cd keys
#openvpn --genkey --secret ta.key
拷贝keys目录下所产生的密钥文件
进行服务器文件的配置
终端下输入:
sudocp/usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz/etc/openvpn/
sudogunzip server.conf.gz#运行这条记得路径是在 /etc/openvpn/ 中!
#以上是将实例文件copy然后解压到我们所在的配置的文件夹 /etc/openvpn/ 中!(或输入cd .. 返回上一层目录)
打 server.conf 开进行配置,配置openvpn配置文件
#OpenVPN的监听地址
local xxx.xxx.xxx.xxx
#OpenVPN的监听端口
port 1194
#使用UDP Server
proto udp
#使用tun通道
dev tun
#相对路径 /etc/openvpn
ca ca.crt
cert server001.crt
key server001.key
dh dh1024.pem
#OpenVPN的子网地址
server 10.0.10.0 255.255.255.0
#客户端的Internet访问使用OpenVPN通道
push "redirect-gateway def1"
#Windows客户端会使用此DNS地址
push "dhcp-optionDNS 10.0.2.25" #这样填DNS需根据实际
#记录客户端分配的ip地址情况,如果OpenVPN服务重启
#客户端重新连接的时候会按照此文件分配原先的地址。
ifconfig-pool-persist/var/lib/openvpn/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/var/lib/openvpn/status.log
#日志文件的记录级别
verb 3
以上为server.conf 的配置主要
然后运行OPENVPN!
运行OPENVPN
终端下输入:openvpn /etc/openvpn/server.conf图片
若如上显示,那么恭喜你,你的OPENVPN已在运行当中了!
按 Ctrl+C 可以停止运行.
这种启动方式可以比较直观看到OPENVPN的运行详细
也可以使用ifconfig命令查看,会多了一个tun0接口:图片
将命令写上(注意位置!),那么下次开机的时候它就会自动运行这条命令,那么就不用担心重启后客户端上不了网了。在终端中输入如下命令:
sudo sysctl -wnet.ipv4.ip_forward=1图片
sudo iptables -t nat -A POSTROUTING -s 10.0.10.0/24 -o eth0 -j MASQUERADE
这时候即可~!
后来我发现存在一个问题,就是但我重启Ubuntu后,发现客户端连上后上不了网,之后我再重新输入
sudo iptables -t nat -A POSTROUTING -s 10.0.10.0/24 -o eth0 -j MASQUERADE
这时候又可以正常上网,所以我估计是Ubuntu关机后把这条命令给消了(不知道其他人会不会有这个现象),所以我将这条命令放到开机启动时运行,找到 /etc/rc.lcacl 并打开
将命令写上(注意位置!),那么下次开机的时候它就会自动运行这条命令,那么就不用担心重启后客户端上不了网了。
最后要配置客户端,win7系统
然后到OPENVPN 官网下载 windows client
下载链接:http://openvpn.net/release/openvpn-2.1.3-install.exe
版本 openvpn-2.1.3
下载完后直接安装!基本无它,反正是GUI,一路NEXT便是~
这时候桌面多了图片,当然还有右下角的这个图片
进入:C:Program FilesOpenVPNsample-config
把以下四个文件,从server端复制到openvpn安装目录的config下:
我使用的是putty的方法,http://www.cnblogs.com/gaizai/archive/2012/06/18/2553168.html 使用putty的链接图片
将从server端复制的那四个客户端文件一并复制到以下路径
C:Program Files (x86)OpenVPNconfig,并新建一个名称为cilent.ovpn的客户端配置文件
双击对图片进行配置
以下是配置内容:
#号和;号都是注释
# 定义是一个客户端
client
# 定义使用路由IP模式,与服务端一致
;dev tap
dev tun
# 定义Windows下使用的网卡名称,linux不需要
;dev-node 我的连接
# 定义使用的协议,与服务端一致
;proto tcp
proto udp
# 指定服务端地址和端口,可以用多行指定多台服务器
# 实现负载均衡(从上往下尝试)
remote 222.128.61.148 1194 (因为我在内网测试,所以地址为私有,若公网进行端口映射后,填公网IP即可)
;remote my-server-2 1194
# 若上面配置了多台服务器,让客户端随机连接
;remote-random
# 解析服务器域名
# Keep trying indefinitely to resolve the
# host name of the OpenVPN server. Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite
# 客户端不需要绑定端口
# Most clients do not need to bind to
# a specific local port number.
nobind
# 也是为了让Openvpn也nobody运行(安全)
# 注意:Windows不能设置
user nobody
group nobody
# Try to preserve some state across restarts.
persist-key
persist-tun
# 若客户端通过HTTP Proxy,在这里设置
# 要使用Proxy,不能使用UDP为VPN的通讯协议
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
# 无线网络有很多多余的头文件,设置忽略它
;mute-replay-warnings
# 重点,就是指定ca和客户端的证书
# 注意,下面的两个号是连在一起的,之间没有空格
ca ca.crt
cert client001.crt
key client001.key
# 如果服务端打开了PAM认证模块,客户端需要另其有效
;auth-user-pass
# 一些安全措施
# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to "server". This is an
# important precaution to protect against
# a potential attack discussed here:
# http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to "server". The build-key-server
# script in the easy-rsa folder will do this.
;ns-cert-type server
# If a tls-auth key is used on the server
# then every client must also have the key.
;tls-auth ta.key 1
# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x
# 使用lzo压缩,与服务端一致
comp-lzo
# Set log file verbosity.
verb 3
# Silence repeating messages
;mute 20
我的配置文件是以下:
client
dev tun
proto udp
comp-lzo
verb 3
mute 20
resolv-retry infinite
nobind
redirect-gateway def1
;user nobody
;group nobody
persist-key
persist-tun
ca ca.crt
cert client001.crt
key client001.key#这个是服务器配置密钥所产生的文件
remote 222.128.61.148 1194#vpn服务器的ip地址及端口
;auth-user-pass
tls-auth ta.key 1
ns-cert-type server
现在运行OpenVPN GUI,打开后其图标出现在菜单栏右下角图片,对其右击
选择图片
它会弹个框出来显示其连接的运行状态,最后一行提示连接成功~!
直到它隐藏后,右下角OPENVPN图标变绿,且气泡提示所分配到的IP
可以在客户端查看IP地址:会多出一个虚拟网卡:
然后 PING 下 VPN SERVER 等地址,看能否PING通,若 PING 通,说明openvpn 客户端配置成功~! |