个人工具

“OpenVPN server”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
OpenVPN Server Installation
客户端证书
 
(未显示2个用户的11个中间版本)
第8行: 第8行:
  
 
==== OpenVPN 服务器的安装 ====
 
==== OpenVPN 服务器的安装 ====
*Install OpenVPN:
+
*安装 OpenVPN:
  sudo apt-get install openvpn
+
  sudo apt-get install openvpn easy-rsa
  
==== Server certificates ====
+
==== 服务器证书 ====
*Create the OpenVPN server certificates.
+
*创建OpenVPN服务器证书.
:*Copy the easy-rsa directory to /etc/openvpn. This will ensure that any changes to the scripts will not be lost when the package is updated.
+
:*复制 easy-rsa 目录到 /etc/openvpn. 这个操作确保在包更新后脚本内容不会丢失.
  
 
  sudo mkdir /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/
+
  cp -r /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/
  
:*Edit /etc/openvpn/easy-rsa/vars and adjust the variables for your environment:
+
:*编辑 /etc/openvpn/easy-rsa/vars 根据实际情况在其中修改相应的环境变量:
  
 
  export KEY_COUNTRY="US"
 
  export KEY_COUNTRY="US"
  export KEY_PROVINCE="CA"
+
  export KEY_PROVINCE="NC"
  export KEY_CITY="''MyCity''"
+
  export KEY_CITY="Winston-Salem"
  export KEY_ORG="''MyCompany''"
+
  export KEY_ORG="Example Company"
  export KEY_EMAIL="''webmaster@mycompany.com''"
+
  export KEY_EMAIL="steve@example.com"
 +
export KEY_CN=MyVPN
 +
export KEY_NAME=MyVPN
 +
export KEY_OU=MyVPN
  
:*Run the scripts to create the server certificates:
 
  
  cd /etc/openvpn/easy-rsa/easy-rsa
+
:*运行脚本,创建服务器证书:
 +
 
 +
  cd /etc/openvpn/easy-rsa
 
  source vars
 
  source vars
 
  ./clean-all
 
  ./clean-all
第38行: 第42行:
 
  sudo cp server.crt server.key ca.crt dh1024.pem ta.key /etc/openvpn/
 
  sudo cp server.crt server.key ca.crt dh1024.pem ta.key /etc/openvpn/
  
==== Client Certificates ====
+
==== 客户端证书 ====
*A VPN clients requires a certificate in order to authenticate itself to the VPN server.
+
*为了能让VPN客户端在运行时能够通过服务器认证,需要生成一个客户端证书.
*Create the certificate:
+
*创建证书:
  
 
  cd /etc/openvpn/easy-rsa/
 
  cd /etc/openvpn/easy-rsa/
第46行: 第50行:
 
  ./pkitool ''hostname''
 
  ./pkitool ''hostname''
  
:Note: Replace ''hostname'' with the actual hostname of the client machine that will be connecting to the VPN.
+
:说明: 用需要连接到VPN服务器的客户端计算机名替换 ''hostname''(不含双引号).
  
*Copy the certificate files that have been created to the client:
+
*拷贝前面生成的下述认证文件到客户端:
:* /etc/openvpn/easy-rsa/''hostname''.ovpn
+
 
:* /etc/openvpn/easy-rsa/ca.crt
 
:* /etc/openvpn/easy-rsa/ca.crt
 
:* /etc/openvpn/easy-rsa/''hostname''.crt
 
:* /etc/openvpn/easy-rsa/''hostname''.crt
 
:* /etc/openvpn/easy-rsa/''hostname''.key
 
:* /etc/openvpn/easy-rsa/''hostname''.key
:* /etc/openvpn/easy-rsa/ta.key
 
  
:Note: Use the files that correspond to your client machine's ''hostname''.
+
:说明: 请拷贝和你的客户端机器名("hostname")相符的文件.
  
==== Server Configuration ====
+
==== 服务器配置 ====
  
*On the OpenVPN server, modify /etc/openvpn/server.conf from the example file:  
+
*在OpenVPN服务器上修改 /etc/openvpn/server.conf
 +
*可以通过示例文件修改:  
  
 
  sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
 
  sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
 
  sudo gzip -d /etc/openvpn/server.conf.gz
 
  sudo gzip -d /etc/openvpn/server.conf.gz
  
*Edit etc/openvpn/server.conf:
+
*修改 etc/openvpn/server.conf:
 
  sudo nano /etc/openvpn/server.conf
 
  sudo nano /etc/openvpn/server.conf
  
:*Changing the following options to resemble:
+
:*根据实际情况更改下述选项:
  
 
  local 172.18.100.101
 
  local 172.18.100.101
第79行: 第82行:
 
  group nogroup
 
  group nogroup
  
:Notes:
+
:说明:
::local: is the IP address of the bridge interface.
+
::local: 指桥接接口的IP地址.
::server-bridge: needed when the configuration uses bridging. The 172.18.100.101 255.255.255.0 portion is the bridge interface and mask. The IP range 172.18.100.105 172.18.100.200 is the range of IP addresses that will be assigned to clients.
+
::server-bridge: 使用桥接模式时需要配置. 172.18.100.101 255.255.255.0 这一部分是桥接接口的IP和掩码. 172.18.100.105 172.18.100.200 是要分配给客户端的IP地址池;
::push: directives to add networking options for clients.
+
::push: 为客户端指定需要添加的IP配置.
::user and group: configure which user and group the openvpn daemon executes as.
+
::user and group: 配置OpenVPN后台服务所依赖的用户和组;
::Replace all IP addresses and domain names above with those of your network.
+
::根据实际网络情况替换上述所有的IP地址和域名k.
  
*Create helper scripts to add the tap interface to the bridge.
+
*创建脚本,添加TAP接口到网桥.
:* Create /etc/openvpn/up.sh:
+
:* 创建 /etc/openvpn/up.sh:
 
  sudo nano /etc/openvpn/up.sh
 
  sudo nano /etc/openvpn/up.sh
  
::Add the lines:
+
::添加以下内容:
  
 
  #!/bin/sh
 
  #!/bin/sh
第100行: 第103行:
 
  /usr/sbin/brctl addif $BR $DEV
 
  /usr/sbin/brctl addif $BR $DEV
  
:*Create /etc/openvpn/down.sh:
+
:*创建 /etc/openvpn/down.sh:
 
  sudo nano /etc/openvpn/down.sh
 
  sudo nano /etc/openvpn/down.sh
  
::Add the lines:
+
::添加以下内容:
  
 
  #!/bin/sh
 
  #!/bin/sh
第113行: 第116行:
 
  /sbin/ifconfig $DEV down
 
  /sbin/ifconfig $DEV down
  
:*Make the scripts executable:
+
:*设置脚本权限为可执行:
 
  sudo chmod 755 /etc/openvpn/down.sh
 
  sudo chmod 755 /etc/openvpn/down.sh
 
  sudo chmod 755 /etc/openvpn/up.sh
 
  sudo chmod 755 /etc/openvpn/up.sh
  
:*Restart OpenVpn:
+
:*重启 OpenVpn服务:
 
  sudo /etc/init.d/openvpn restart
 
  sudo /etc/init.d/openvpn restart
  
==== Client Configuration ====
+
====客户端配置 ====
*Copy the example client configuration file:
+
*复制示例文件:
 
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn
 
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn
  
*Edit the client configuration file:
+
*修改配置文件:
 
  sudo nano /etc/openvpn/client.conf  
 
  sudo nano /etc/openvpn/client.conf  
  
:*Change it to resemble:
+
:*参考下面的内容进行修改:
  
 
  dev tap
 
  dev tap
第135行: 第138行:
 
  tls-auth ta.key 1
 
  tls-auth ta.key 1
  
:Note: Replace ''vpn.mycompany.com'' with the hostname of your VPN server, and ''hostname''.* with the actual certificate and key filenames that correspond to the client.
+
:说明: 用你VPN服务器的服务器名替换''vpn.mycompany.com'', 根据你使用的.crt和.key文件名(这两个文件名应该一致)替换"hostname".
  
*Restart OpenVpn:
+
*重启 OpenVpn服务:
 
  sudo /etc/init.d/openvpn restart
 
  sudo /etc/init.d/openvpn restart
  
*Connect the VPN client to the remote LAN through the OpenVPN server.
+
*通过VPN服务器远程连接到LAN.
  
==== Other resources ====
+
==== 其它资源 ====
* [https://help.ubuntu.com/9.10/serverguide/C/openvpn.html Ubuntu 9.10 Server Guide (Karmic Koala) -- OpenVPN server]
+
* [https://help.ubuntu.com/14.04/serverguide/openvpn.html Ubuntu 14.04 服务器指南  OpenVPN]

2016年7月6日 (三) 10:23的最新版本

OpenVPN

我们可以把处于LAN环境下的特定计算机设置为一个VPN服务器,这个服务器可以让处于LAN之外的远程客户端和LAN上的计算机进行加密通信(通过VPN服务器接入). 当两台终端建立VPN 连接通道(服务端和客户端)时,OpenVPN 使用 PKI(Public Key Infrastructure)对其进行加密.

VPN 服务器的硬件需求取决于最大的并发通信隧道的数量.通俗的来讲,最为普通配置的电脑可以胜任一台并发连接数不大于10的VPN服务器.而一台不大于100个并发连接数的VPN服务器则需要较高的内存和CPU配置.网络连接也是一个限制因素,所以,高配的网卡也是必须的(至少支持千兆网卡).

使用桥接模式

OpenVPN 服务器通常使用桥接模式.一个网络(如:eth0)连接到WAN(互联网),这个连接用来接入VPN请求,另外一个(如:eth1)连接到LAN(局域网),VPN数据的流动在这两个网络间可以理解为被”桥接”了.查看Network Interface Bridging获取详情.

OpenVPN 服务器的安装

  • 安装 OpenVPN:
sudo apt-get install openvpn easy-rsa

服务器证书

  • 创建OpenVPN服务器证书.
  • 复制 easy-rsa 目录到 /etc/openvpn. 这个操作确保在包更新后脚本内容不会丢失.
sudo mkdir /etc/openvpn/easy-rsa/
cp -r /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/
  • 编辑 /etc/openvpn/easy-rsa/vars 根据实际情况在其中修改相应的环境变量:
export KEY_COUNTRY="US"
export KEY_PROVINCE="NC"
export KEY_CITY="Winston-Salem"
export KEY_ORG="Example Company"
export KEY_EMAIL="steve@example.com"
export KEY_CN=MyVPN
export KEY_NAME=MyVPN
export KEY_OU=MyVPN


  • 运行脚本,创建服务器证书:
cd /etc/openvpn/easy-rsa
source vars
./clean-all
./build-dh
./pkitool --initca
./pkitool --server server
cd keys
openvpn --genkey --secret ta.key
sudo cp server.crt server.key ca.crt dh1024.pem ta.key /etc/openvpn/

客户端证书

  • 为了能让VPN客户端在运行时能够通过服务器认证,需要生成一个客户端证书.
  • 创建证书:
cd /etc/openvpn/easy-rsa/
source vars
./pkitool hostname
说明: 用需要连接到VPN服务器的客户端计算机名替换 hostname(不含双引号).
  • 拷贝前面生成的下述认证文件到客户端:
  • /etc/openvpn/easy-rsa/ca.crt
  • /etc/openvpn/easy-rsa/hostname.crt
  • /etc/openvpn/easy-rsa/hostname.key
说明: 请拷贝和你的客户端机器名("hostname")相符的文件.

服务器配置

  • 在OpenVPN服务器上修改 /etc/openvpn/server.conf
  • 可以通过示例文件修改:
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
sudo gzip -d /etc/openvpn/server.conf.gz
  • 修改 etc/openvpn/server.conf:
sudo nano /etc/openvpn/server.conf
  • 根据实际情况更改下述选项:
local 172.18.100.101
dev tap0
server-bridge 172.18.100.101 255.255.255.0 172.18.100.105 172.18.100.200
push "route 172.18.100.1 255.255.255.0"
push "dhcp-option DNS 172.18.100.20"
push "dhcp-option DOMAIN example.com"
tls-auth ta.key 0 # This file is secret
user nobody
group nogroup
说明:
local: 指桥接接口的IP地址.
server-bridge: 使用桥接模式时需要配置. 172.18.100.101 255.255.255.0 这一部分是桥接接口的IP和掩码. 172.18.100.105 172.18.100.200 是要分配给客户端的IP地址池;
push: 为客户端指定需要添加的IP配置.
user and group: 配置OpenVPN后台服务所依赖的用户和组;
根据实际网络情况替换上述所有的IP地址和域名k.
  • 创建脚本,添加TAP接口到网桥.
  • 创建 /etc/openvpn/up.sh:
sudo nano /etc/openvpn/up.sh
添加以下内容:
#!/bin/sh
#
BR=$1
DEV=$2
MTU=$3
/sbin/ifconfig $DEV mtu $MTU promisc up
/usr/sbin/brctl addif $BR $DEV
  • 创建 /etc/openvpn/down.sh:
sudo nano /etc/openvpn/down.sh
添加以下内容:
#!/bin/sh
#
BR=$1
DEV=$2
#
/usr/sbin/brctl delif $BR $DEV
/sbin/ifconfig $DEV down
  • 设置脚本权限为可执行:
sudo chmod 755 /etc/openvpn/down.sh
sudo chmod 755 /etc/openvpn/up.sh
  • 重启 OpenVpn服务:
sudo /etc/init.d/openvpn restart

客户端配置

  • 复制示例文件:

sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn

  • 修改配置文件:
sudo nano /etc/openvpn/client.conf 
  • 参考下面的内容进行修改:
dev tap
remote vpn.mycompany.com 1194
cert hostname.crt
key hostname.key
tls-auth ta.key 1
说明: 用你VPN服务器的服务器名替换vpn.mycompany.com, 根据你使用的.crt和.key文件名(这两个文件名应该一致)替换"hostname".
  • 重启 OpenVpn服务:
sudo /etc/init.d/openvpn restart
  • 通过VPN服务器远程连接到LAN.

其它资源