个人工具

UbuntuHelp:OpenVPN/zh

来自Ubuntu中文

跳转至: 导航, 搜索

介绍/概况

更新说明可以查询 the Ubuntu 9.10 Server guide (Karmic Koala) -- OpenVPN.

概况

OpenVPN 是由ubuntu库提供的私人虚拟网络(VPN)解决方案. 具有灵活,易用,可靠,安全的特点. 这些说明是使用x509证书建立一个VPN桥接和一般管理任务.

什么是 VPN 桥接 ?

VPN 桥接技术允许在客户看起来好像他们是在同一局域网(LAN)的服务器系统. VPN 使用结合虚拟设备来完成这项任务 -- 一些人称之为"桥接",另一些则称之为"tap device". Tap device就像一个虚拟以太网适配器和桥接设备一样工作,还有就是虚拟集线器. 当你桥接到一个物理以太网设备和tap device时, 必不可少的是创造一个物理网络和远程客户之间的集线器. 因此,所有网络服务(LAN services)对远程客户都是可见的.

系统的安装与配置

对于 Linux 网络协议栈的理解是必不可少的. 下面这个例子是使用安装了Ubuntu Jeos 8.04的开源虚拟机(KVM)的环境下完成的 (可以很容易的在一个独立的安装了 ubuntu 服务器的电脑中完成). 在我的配置中,eth0 连接到互联网,eth1连接到局域网,将在它们之间建立桥接. .

服务器的安装

安装 OpenVPN:

sudo apt-get install openvpn bridge-utils
配置桥接器
  • 编辑 /etc/network/interfaces

当一个 Linux 服务器开启了防火墙, 文件 /etc/network/interfaces 可能是:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo eth0
iface lo inet loopback

# The primary network interface
iface eth0 inet static
  address 192.168.1.10
  netmask 255.255.255.0
  gateway 192.168.1.1

编辑它并添加一个桥接端口:

 
sudo nano /etc/network/interfaces

然后它看起来可能是:


auto lo br0

iface lo inet loopback

iface br0 inet static 
  address 192.168.1.10 
  netmask 255.255.255.0
  gateway 192.168.1.1
  bridge_ports eth0

iface eth0 inet manual
  up ifconfig $IFACE 0.0.0.0 up
  up ip link set $IFACE promisc on
  down ip link set $IFACE promisc off
  down ifconfig $IFACE down 
  • 如果你在一个虚拟机中运行 Linux, 你需要对桥接连接补充以下参数:
  bridge_fd 9      ## from the libvirt docs (forward delay time)
  bridge_hello 2   ## from the libvirt docs (hello time)
  bridge_maxage 12 ## from the libvirt docs (maximum message age)
  bridge_stp off   ## from the libvirt docs (spanning tree protocol)
  • 重启网络:
sudo /etc/init.d/networking restart

桥接从 libvirt 文件开始减速. (我真的只认识 bridge_ports 指令和 bridge_stp 指令.请添加更多的说明.)

证书的认证
  • 服务器证书的认证. 为此我将使用我自己的证书颁发机构提供的 easy-rsa 脚本目录 /usr/share/doc/openvpn/examples/easy-rsa/ . 另一个选择是使用图形界面的 tinyca 去建立你自己的证书(CA).

第 1 步: 复制文件到 /etc/openvpn/easy-rsa/

sudo mkdir /etc/openvpn/easy-rsa/ 
sudo cp -R /usr/share/doc/openvpn/examples/easy-rsa/2.0/* /etc/openvpn/easy-rsa/ 

第 2 步: 编辑 /etc/openvpn/easy-rsa/vars

sudo vi /etc/openvpn/easy-rsa/vars

在选项位置使用你的新CA更改下列项:

export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="me@myhost.mydomain"

第 3 步: 安装你的CA和创建第一个服务器证书


cd /etc/openvpn/easy-rsa/ ## move to the easy-rsa directory
sudo chown -R root:admin .  ## make this directory writable by the system administrators
sudo chmod g+w . ## make this directory writable by the system administrators
source ./vars ## execute your new vars file
./clean-all  ## Setup the easy-rsa directory (Deletes all keys)
./build-dh  ## takes a while consider backgrounding
./pkitool --initca ## creates ca cert and key
./pkitool --server server ## creates a server cert and key
cd keys
openvpn --genkey --secret ta.key  ## Build a TLS key
sudo cp server.crt server.key ca.crt dh1024.pem ta.key ../../

    

证书机构已经建立,其所需的密钥在 /etc/openvpn/ 目录中

服务器的配置

默认为所有服务器从指定的 /etc/openvpn/ 中的 *.conf 文件开始启动. 因此, 我们只需要在 /etc/openvpn/ 中创建一个新的 server.conf 文件. 首先,我们将创建一个针对openvpn服务器使用的新脚本.

sudo vi /etc/openvpn/up.sh

新脚本需像下面这样:

#!this bogus line has been added so the following line will display properly!
#!/bin/sh

BR=$1
DEV=$2
MTU=$3
/sbin/ifconfig $DEV mtu $MTU promisc up
/usr/sbin/brctl addif $BR $DEV

现在,我们要建立一个 "down" 脚本.

sudo vi /etc/openvpn/down.sh

像下面这样:

#!this bogus line has been added so the following line will display properly!
#!/bin/sh

BR=$1
DEV=$2

/usr/sbin/brctl delif $BR $DEV
/sbin/ifconfig $DEV down

然后,给两个脚本加上执行权限.

sudo chmod +x /etc/openvpn/up.sh /etc/openvpn/down.sh

现在配置 openvpn.

sudo vi /etc/openvpn/server.conf
mode server
tls-server

local <your ip address> ## ip/hostname of server
port 1194 ## default openvpn port
proto udp



#bridging directive
dev tap0 ## If you need multiple tap devices, add them here
up "/etc/openvpn/up.sh br0"
down "/etc/openvpn/down.sh br0"

persist-key
persist-tun

#certificates and encryption
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh1024.pem
tls-auth ta.key 0 # This file is secret

cipher BF-CBC        # Blowfish (default)
comp-lzo

#DHCP Information
ifconfig-pool-persist ipp.txt
server-bridge 192.168.1.10 255.255.255.0 192.168.1.100 192.168.1.110
push "dhcp-option DNS your.dns.ip.here"
push "dhcp-option DOMAIN yourdomain.com"
max-clients 10 ## set this to the max number of clients that should be connected at a time

#log and security
user nobody
group nogroup
keepalive 10 120
status openvpn-status.log
verb 3

别忘了重新启动或运行下面的命令. 这将重新启动openvpn和加载新的配置.

sudo /etc/init.d/openvpn restart

客户端的连接

这部分涉及到创建客户端证书和密钥文件并设置一个客户端配置文件. 文件将被用在客户端平台的 OpenVPN 中. 所描述的配置文件将被用在 OpenVPN 安装包 OpenVPN GUI for Windows 用户和 Tunnelblick for Mac OS X 用户. 详细的说明参考它们各自的网页.它也兼容 OpenVPN 用户.

生成密钥和证书

客户端的证书和服务器端的生成过程是类似的.这里假定你已建立 /etc/openvpn/easy-rsa/ 目录和更新 /etc/openvpn/easy-rsa/vars 文件,像上面描述的一样. 你应该已经设置好了您的证书颁发机构,创建了一个服务器的证书和密钥.

cd /etc/openvpn/easy-rsa/ ## move to the easy-rsa directory
source ./vars             ## execute the vars file
./pkitool client          ## create a cert and key named "client"
客户端的配置

客户端的配置是改编自 OpenVPN 2.0 样本配置文件. 对于 Windows 用户, 文件必须命名为 client.ovpn 对于其他的操作系统, 文件需命名为 client.conf. 可以用 vi 创建或者其他文本编辑器创建. 假定客户端配置文件只有一个 TUN/TAP 设备被配置.


# Specify that this is a client
client

# Bridge device setting
dev tap

# Host name and port for the server (default port is 1194)
# note: replace with the correct values your server set up
remote your.server.example.com 1194

# Client does not need to bind to a specific local port
nobind


# Keep trying to resolve the host name of OpenVPN server.
resolv-retry infinite

# Preserve state across restarts
persist-key
persist-tun

# SSL/TLS parameters - files created previously
ca ca.crt
cert client.crt
key client.key

# Since we specified the tls-auth for server, we need it for the client
# note: 0 = server, 1 = client
tls-auth ta.key 1

# Specify same cipher as server
cipher BF-CBC

# Use compression
comp-lzo

# Log verbosity (to help if there are problems)
verb 3

把客户端的 client.ovpn (或者 client.conf) 配置文件和证书与密钥文件一起放在 openvpn 配置目录. 上面的建立后,下面的这些文件也需要放在配置目录中.

client.ovpn
ca.crt
client.crt
client.key
ta.key

(对于 OpenVPN GUI for Windows用户, 文件默认位置是 C:\Program Files\OpenVPN\config.) (对于 Tunnelblick for Mac OS X用户, 文件默认位置是 ~username/Library/openvpn.

为 OpenVPN 配置 Firestarter

Firestarter 需要在客户端和服务器端进行一些配置,来允许服务像 SAMBA 通过 VPN 信道.此外建立GUI界面时,还需要编辑 /etc/firestarter/user-pre 文件. 我用的这些说明摘自: http://www.howtoadvice.com/FirestarterVPN/ 此外,虽然教程不讨论它,我认为有必要保存原始user-pre文件副本,然后由于权限的问题重命名原始文件和重命名复制文件到user-pre.

其他资源