RHEL5.4下openvpn的详细配置
搞了两天 openvpn终于拿下。本以为会很容易 没想到这么麻烦
以下是一篇自我实践版(注:此为原创帖,转帖请注明出处:http://user.qzone.qq.com/184294950/)
一.安装软件
1.软件:lzo-2.03.tar.gz openvpn-2.0.9.tar.gz
2.服务器端安装:
[root@Open local]# /usr/local
[root@OpenVPN local]# tar zxvf lzo-2.03.tar.gz
[root@OpenVPN local]# cd lzo-2.03
[root@OpenVPN lzo-2.03]# ./configure && make && make install
[root@OpenVPN lzo-2.03]# /usr/local
[root@OpenVPN local]# tar zxvf openvpn-2.0.9.tar.gz
[root@OpenVPN local]# cd openvpn-2.0.9
[root@OpenVPN openvpn-2.0.9]# ./configure && make && make install
[root@OpenVPN openvpn-2.0.9]# mkdir -p /etc/openvpn
[root@OpenVPN openvpn-2.0.9]# cd /etc/openvpn/
[root@OpenVPN openvpn-2.0.9]# cp -r /usr/local/openvpn-2.0.9/easy-rsa /etc/openvpn/
[root@OpenVPN openvpn]# cp -r /usr/local/openvpn-2.0.9/sample-config-files/server.conf
/etc/openvpn/
[root@OpenVPN openvpn]# cd easy-rsa/
[root@OpenVPN easy-rsa]# vi vars
修改vars 文件
# easy-rsa parameter settings
# NOTE: If you installed from an RPM,
# don't edit this file in place in
# /usr/share/openvpn/easy-rsa --
# instead, you should copy the whole
# easy-rsa directory to another location
# (such as /etc/openvpn) so that your
# edits will not be wiped out by a future
# OpenVPN package upgrade.
# This variable should point to
# the top level of the easy-rsa
# tree.
export D=`pwd`
# This variable should point to
# the openssl.cnf file included
# with easy-rsa.
export KEY_CONFIG=$D/openssl.cnf
# Edit this variable to point to
# your soon-to-be-created key
# directory.
#
# WARNING: clean-all will do
# a rm -rf on this directory
# so make sure you define
# it correctly!
export KEY_DIR=$D/keys
# Issue rm -rf warning
echo NOTE: when you run ./clean-all, I will be doing a rm -rf on $KEY_DIR
# Increase this to 2048 if you
# are paranoid. This will slow
# down TLS negotiation performance
# as well as the one-time DH parms
# generation process.
export KEY_SIZE=1024
# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
# 定义你所在的国家,2个字符
export KEY_COUNTRY=CN
# 你所在的省份
export KEY_PROVINCE=SHANDONG
# 你所在的城市
export KEY_CITY=QINGDAO
# 你所在的组织
export KEY_ORG=" OpenVPN ORG"
# 你的单位
export KEY_OU="OpenVPN Service"
# 你的邮件地址
export KEY_EMAIL="[email protected]"
修改后保存,下面我们开始生成keys
[root@OpenVPN easy-rsa]# chmod +x vars
[root@OpenVPN easy-rsa]# . ./vars #使修改的变量生效
NOTE: when you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
[root@OpenVPN easy-rsa]# ./clean-all #初始化keys目录,创建所需要的文件和目录
[root@OpenVPN easy-rsa]# ./build-ca #生成Root CA证书,用于签发Server和Client证书,请保护好keys/ca.key文件。
出现“Common Name (eg, your name or your server's hostname) []:”时输入BaiHe Root CA,别的默认直接回车就可以
[root@OpenVPN easy-rsa]# ./build-key-server server
出现“Common Name (eg, your name or your server's hostname) []:”时输入server
出现:“Certificate is to be certified until Aug 17 08:30:49 2017 GMT (3650 days)
Sign the certificate? [y/n]:“ 时选 y(一共有两次,别的不用管回车就可以)
接下来为VPN Client颁发CA证书,如果以后要为其他Client颁发证书,直接使用build-key命令签发新证书
[root@OpenVPN easy-rsa]# ./build-key client
出现:“Common Name (eg, your name or your server's hostname) []:”输入“client”
出现:“Certificate is to be certified until Aug 17 08:33:36 2017 GMT (3650 days)
Sign the certificate? [y/n]:“时先y
[root@OpenVPN easy-rsa]# ./build-dh
为防止恶意攻击(如DOS、UDP port flooding),我们生成一个"HMAC firewall"
[root@OpenVPN easy-rsa]#openvpn --genkey --secret keys/ta.key
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
[root@OpenVPN easy-rsa]# cd keys/
[root@OpenVPN keys]# cp -r ca.crt server.key server.crt
二.修改配置文件
[root@OpenVPN keys]# cd /etc/openvpn/
VPN Server使用的配置文件server.conf
[root@OpenVPN openvpn]# vi server.conf
# 申明本机使用的IP地址,也可以不说明
local 192.168.1.10
# 申明使用的端口,默认1194
port 1194
# 申明使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议
;proto tcp
proto udp
# 申明使用的设备可选tap和tun,tap是二层设备,支持链路层协议。
# tun是三层的点对点协议,限制稍微多一些
;dev tap
dev tun
# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel if you
# have more than one. On XP SP2 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap
# OpenVPN使用的ROOT CA,使用build-ca生成的,用于验证客户是证书是否合法
ca ca.crt
# Server使用的证书文件
cert server.crt
# Server使用的证书对应的key,注意文件的权限,防止被盗
key server.key # This file should be kept secret
# 上面提到的生成的Diffie-Hellman文件
dh dh1024.pem
# VPN分配给客户端的地址池,VPN Server分配的地址为10.8.0.1
server 10.8.0.0 255.255.255.0
# 用于记录某个Client获得的IP地址,类似于dhcpd.lease文件,
# 防止openvpn重新启动后“忘记”Client曾经使用过的IP地址
ifconfig-pool-persist ipp.txt
# Bridge状态下类似DHCPD的配置,为客户分配地址,由于这里工作在路由模式,所以不使用
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
# 通过VPN Server向Client push路由,client通过pull指令获得Server push的所有选项并应用
push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
# VPN启动后,在VPN Server上增加的路由,VPN停止后自动删除
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
# iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN. This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.
# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
# ifconfig-push 10.9.0.1 10.9.0.2
# Run script or shell command cmd to validate client
# virtual addresses or routes. 具体查看manual
;learn-address ./script
#
# 其他的一些需要PUSH给Client的选项
#
# 使Client的默认网关指向VPN,让Client的所有Traffic都通过VPN走
;push "redirect-gateway"
# DHCP的一些选项,具体查看Manual
;push "dhcp-option DNS 10.8.0.1"
;push "dhcp-option WINS 10.8.0.1"
# 如果可以让VPN Client之间相互访问直接通过openvpn程序转发,
# 不用发送到tun或者tap设备后重新转发,优化Client to Client的访问效率
;client-to-client
# 如果Client使用的CA的Common Name有重复了,或者说客户都使用相同的CA
# 和keys连接VPN,一定要打开这个选项,否则只允许一个人连接VPN
;duplicate-cn
# NAT后面使用VPN,如果VPN长时间不通信,NAT Session可能会失效,
# 导致VPN连接丢失,为防止之类事情的发生,keepalive提供一个类似于ping的机制,
# 下面表示每10秒通过VPN的Control通道ping对方,如果连续120秒无法ping通,
# 认为连接丢失,并重新启动VPN,重新连接
#( mode server模式下的openvpn不会重新连接)。
keepalive 10 120
# 上面提到的HMAC防火墙,防止DOS攻击,对于所有的控制信息,都使用HMAC signature,
# 没有HMAC signature的控制信息不予处理,注意server端后面的数字肯定使用0,client使用1
tls-auth ta.key 0 # This file is secret
# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC # Blowfish (default)
;cipher AES-128-CBC # AES
;cipher DES-EDE3-CBC # Triple-DES
# 对数据进行压缩,注意Server和Client应该一致
comp-lzo
# 定义最大连接数
;max-clients 100
# 定义运行openvpn的用户
user nobody
group nobody
# 通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys
persist-key
# 通过keepalive检测超时后,重新启动VPN,一直保持tun或者tap设备是linkup的,
# 否则网络连接会先linkdown然后linkup
persist-tun
# 定期把openvpn的一些状态信息写到文件中,以便自己写程序计费或者进行其他操作
status openvpn-status.log
# 记录日志,每次重新启动openvpn后删除原有的log信息
log openvpn.log
# 和log一致,每次重新启动openvpn后保留原有的log信息,新信息追加到文件最后
log-append openvpn.log
# 相当于debug level,具体查看manual
verb 3
# Silence repeating messages. At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20
三.添加系统服务
创建OpenVPN启动脚本,可以在源代码目录中找到,在sample-scripts目录下的openvpn.init文件,将其复制到/etc/init.d/目录中,改名为openvpn
然后运行:
[root@OpenVPN openvpn]# cp /usr/local/openvpn-2.0.9/sample-scripts/openvpn.init /etc/init.d/openvpn
[root@OpenVPN openvpn]# chkconfig --add openvpn
[root@OpenVPN openvpn]# chkconfig --level 2345 openvpn on
四.客户端配置
1.软件:lzo-2.03.tar.gz openvpn-2.0.9.tar.gz
[root@VPNClient ~]# cd /usr/local/
[root@VPNClient local]# tar zxvf lzo-2.03.tar.gz
[root@VPNClient local]# cd lzo-1.08
[root@VPNClient lzo-2.03]# ./configure && make && make install
[root@VPNClient ~]# cd /usr/local/
[root@VPNClient local]# tar zxvf openvpn-2.0.9.tar.gz
[root@VPNClient local]# cd openvpn-2.0.9
[root@VPNClient openvpn-2.0.9]# ./configure && make && make install
[root@VPNClient openvpn-2.0.9]# mkdir -p /etc/openvpn
[root@VPNClient openvpn-2.0.9]# cd /etc/openvpn/
[root@VPNClient openvpn]# cp -r /usr/local/openvpn-2.0.9/sample-config-files/client.conf /etc/openvpn/
[root@VPNClient openvpn]# scp ip_address:/etc/openvpn/easy-rsa/keys/client.crt /etc/openvpn
[root@VPNClient openvpn]# scp ip_address:/etc/openvpn/easy-rsa/keys/client.key /etc/openvpn
[root@VPNClient openvpn]# scp ip_address:/etc/openvpn/easy-rsa/keys/ca.crt /etc/openvpn
[root@VPNClient openvpn]# scp ip_address::/etc/openvpn/easy-rsa/keys/ta.key /etc/openvpn
2.客户端配置文件
Linux或Unix下使用扩展名为.conf Windows下使用的是.ovpn,并把需要使用的keys复制到配置文件所在目录ca.crt client.crt client.key ta.key
[root@VPNClient openvpn]# vi client.conf
# 申明为client,配置从server端pull过来,如IP地址,路由信息之类“是VPN Server使用push指令push过来的”
client
# 指定接口的类型,严格和Server端一致
;dev tap
dev tun
# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap
# 使用的协议,与Server严格一致
;proto tcp
proto udp
# 设置Server的IP地址和端口,如果有多台机器做负载均衡,可以多次出现remote关键字
remote 192.168.1.10 1194
;remote my-server-2 1194
# 随机选择一个Server连接,否则按照顺序从上到下依次连接
;remote-random
# 始终重新解析Server的IP地址(如果remote后面跟的是域名),
# 保证Server IP地址是动态的使用DDNS动态更新DNS后,Client在自动重新连接时重新解析Server的IP地址
# 这样无需人为重新启动,即可重新接入VPN
resolv-retry infinite
# 在本机不邦定任何端口监听incoming数据,Client无需此操作,除非一对一的VPN有必要
nobind
# 运行openvpn用户的身份,旧版本在win下需要把这两行注释掉,新版本无需此操作
;user nobody
;group nobody
# 在Client端增加路由,使得所有访问内网的流量都经过VPN出去
# 当然也可以在Server的配置文件里头设置,Server配置里头使用的命令是
# push "route 192.168.0.0 255.255.255.0"
;route 192.168.0.0 255.255.0.0
# 和Server配置上的功能一样 如果使用了chroot或者su功能,最好打开下面2个选项,防止重新启动后找不到keys文件,或者nobody用户没有权限启动tun设备
persist-key
persist-tun
# 如果你使用HTTP代理连接VPN Server,把Proxy的IP地址和端口写到下面
# 如果代理需要验证,使用http-proxy server port [authfile] [auth-method]
# 其中authfile是一个2行的文本文件,用户名和密码各占一行,auth-method可以省略,详细信息查看Manual
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
# 对于无线设备使用VPN的配置
# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings
# Root CA 文件的文件名,用于验证Server CA证书合法性,通过easy-rsa/build-ca生成的ca.crt,和Server配置里的ca.crt是同一个文件
ca ca.crt
# 用easy-rsa/build-key生成的key pair文件,上面生成key部分中有提到,不同客户使用不同的keys修改以下两行配置并使用他们的keys即可。
cert client.crt
key client.key
# Server使用build-key-server脚本什成的,在x509 v3扩展中加入了ns-cert-type选项
# 防止VPN client使用他们的keys + DNS hack欺骗vpn client连接他们假冒的VPN Server
# 因为他们的CA里没有这个扩展
;ns-cert-type server
# 和Server配置里一致,ta.key也一致,注意最后参数使用的是1
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
# 压缩选项,和Server严格一致
comp-lzo
# Set log file verbosity.
verb 3
# Silence repeating messages
;mute 20
Windows下OpenVPN的安装,Windows下有图形界面的OpenVPN-GUI程序,下载地址http://openvpn.se
下载安装,默认下一步就OK了,安装完事后在托盘上出现一个新的图标,把client.ovpn ca.crt client.crt client.key ta.key文件拷贝到C:\Program Files\openvpn\config目录下,然后点右键connect就OK了。
3.服务自启动
[root@VPNClient openvpn]# cp /usr/local/openvpn-2.0.9/sample-scripts/openvpn.init /etc/init.d/openvpn
[root@VPNClient openvpn]# chkconfig --add openvpn
[root@VPNClient openvpn]# chkconfig --level 2345 openvpn on
五.为应用服务器添加路由表(这里由于VPN服务器不是网关所以需要添加路由表到应用服务器,如果VPN服务器是网关则不用)
[root@VPNClient ~]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
- 最新评论
