openvpn 配置文件

来源:互联网 发布:水知道答案知乎 编辑:程序博客网 时间:2024/06/05 01:09

# 声明VPN服务器的IP,也可以不声明
local 192.168.1.19

#申明使用的端口,默认1194

port 1194

# 申明使用的协议,默认使用UDP,如果使用HTTP proxy,必须使用TCP协议
;proto tcp
proto udp

# 申明使用的设备可选tap和tun, WIN下必须选tap
;dev tap
dev tun

# Windows  需要 TAP-Win32 网卡,非WIN不需要
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key)
# OpenVPN使用的ROOT CA,使用build-ca生成的,用于验证客户是证书是否合法
ca   /tmp/openvpn-2.0.5/easy-rsa/keys/ca.crt

#Server使用的证书文件
cert  /tmp/openvpn-2.0.5/easy-rsa/keys/server.crt

#Server使用的证书对应的key,注意文件的权限,防止被盗
key  /tmp/openvpn-2.0.5/easy-rsa/keys/server.key  # This file should be kept secret

#生成的Diffie-Hellman文件
dh /tmp/openvpn-2.0.5/easy-rsa/keys/dh1024.pem

---------------------------------------------------------------------------------------------

# #这条命令等效于:

#OpenVPN工作在Server模式,可以支持多client同时动态接入
# mode server

#使用TLS加密传输,本端为Server,Client端为tls-client

# tls-server

# if dev tun: #如果使用tun设备,等效于以下配置
# ifconfig 10.8.0.1 10.8.0.2 #设置本地tun设备的地址
# ifconfig-pool 10.8.0.4 10.8.0.251 #说明OpenVPN使用的地址池(用于分配给客户),分别是起始地址、结束地址
# route 10.8.0.0 255.255.255.0 #增加一条静态路由,省略下一跳地址,下一跳为对端地址,这里是: 10.8.0.2
# if client-to-client: #如果使用client-to-client这个选项
# push "route 10.8.0.0 255.255.255.0" #把这条路由发送给客户端,客户连接成功后自动加入路由表,省略了下一跳地址: 10.8.0.1
# else
# push "route 10.8.0.1" #否则发送本条路由,这是一个主机路由,省略了子网掩码和下一跳地址,分别为: 255.255.255.255 10.8.0.1
# if dev tap: #如果使用tap设备,则等效于以下命令
# ifconfig 10.8.0.1 255.255.255.0 #配置tap设备的地址
# ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0 #客户端使用的地址池,分别是起始地址、结束地址、子网掩码
# push "route-gateway 10.8.0.1" #把环境变量route-gateway传递给客户机

server 10.8.0.0 255.255.255.0

---------------------------------------------------------------------------------------------------------------------------------

# 用于记录某个Client获得的IP地址,防止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"

# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
;client-config-dir ccd

#VPN启动后,在VPN Server上增加的路由,VPN停止后自动删除
;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

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

 #使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 /tmp/openvpn-2.0.5/easy-rsa/keys/openvpn-status.log

# 记录日志,每次重新启动openvpn后删除原有的log信息

;log         openvpn.log

#和log一致,每次重新启动openvpn后保留原有的log信息,新信息追加到文件最后
;log-append  openvpn.log

 

# 相当于debug level,具体查看manual

# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 4

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

-------------------------------------------------------------------------------------------------------------

 

client.conf的内容

---------------------------------------------------------------------------------------------------------------------

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
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.19 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 #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca "D:\\OpenVPN\\config\\ca.crt"
cert "D:\\OpenVPN\\config\\client1.crt"
key "D:\\OpenVPN\\config\\client1.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
redirect-gateway def1

# Silence repeating messages
;mute 20
---------------------------------------------------------------------------------------------------------------------

只说明在 Linux 下的配置。Windows 类似。创建 /etc/vpn/server.conf,内容如下:

  port 1494
  proto udp
  dev tun
  ca ca.crt
  cert server.crt
  key server.key
  dh dh1024.pem

  server 10.1.0.0 255.255.255.0 

  push "route 10.1.0.0 255.255.255.0"
  push "route 10.1.1.0 255.255.255.0"
  client-config-dir /etc/vpn/ccd
  route 10.1.1.0 255.255.255.0

  client-to-client

  keepalive 10 120 

  user nobody
  group nobody

  persist-key
  persist-tun

  log-append openvpn.log
  verb 3
  其中 ca.crt, server.key,server.crt 可以用以前贴出的 ca 工具创建,dh1024.pem 用 OpenVPN 自带的工具创建。
  这个配置文件创建了两个网段:10.1.0.* 和 10.1.1.*,VPN 服务器将从这两个网段中给 Client 分配 IP 地址。VPN Server 自身 IP 将是 10.0.0.1。
  “client-config-dir”指明 Client 的专有配置文件目录。在这个目录下可以针对特定用户建立配置文件。例如,要为用户 abc 指定一个 IP 地址(如10.1.1.5)而不是让 VPN Server 自动分配,可以在配置目录/etc/vpn/ccd下建立一个 abc 文件,内容如下:
  ifconfig-push 10.1.1.5 10.1.1.6 
  那么 VPN Server 就会自动给 abc 用户分配 10.1.1.5 这个地址。注意第一个IP地址 的最后一个数字(这里是 5)必须是 4*n + 1 的数。 

  问题是,VPN Server 怎么知道哪个用户是 abc 呢?它是 Client 数字证书中的 Common Name 域来判断的。就是说,在连接协商时如果 Client 端数字证书的 Common Name 是 abc,那么 VPN Server 就找配置目录下 abc 这个文件

----------------------------------------------------------------------------------------------------------------------------------------

  在 Client 机器上 OpenVPN 安装目录的 config 目录下建立如下 client.ovpn 文件:
  client
  dev tun
  proto udp
  remote vpn_server_ip 1494

  ca ca.crt
  cryptoapicert "SUBJ: abc"

  nobind
  persist-key
  persist-tun
  verb 2 

  修改 remote 一行填上对应 VPN Server 的 IP 和 端口。ca.crt 和服务端 ca.crt 一样,必须把这个 ca.crt 也放在 config 目录下。


  关键是 cryptoapicert "SUBJ: abc" 这行。这一行指定客户端的数字证书从 Windows 证书 Store 里取。在 IE 的“选项 -> 内容 -> 证书”页面能看到你的个人数字证书。SUBJ:abc 指明选择证书主题中含有 abc 的证书。客户端数字证书也可以用以前贴的 ca 工具生成,如果要给用户 abc 签发数字证书,只用指明 Common Name 是 abc 即可,然后把生成的 abc.p12 传给 abc 用户并告诉导入口令


原创粉丝点击