如何在Debian 9上设置OpenVPN服务器

介绍

想要在连接到不受信任的网络(例如酒店的WiFi或咖啡店)时,通过智能手机或笔记本电脑安全可靠地访问互联网? 虚拟专用网络 (VPN)允许您私下安全地遍历不受信任的网络,就像您在专用网络上一样。 流量从VPN服务器出现并继续前往目的地。

HTTPS连接结合使用时,此设置可让您保护无线登录和交易。 您可以绕过地理限制和审查,并屏蔽您的位置以及来自不受信任网络的任何未加密的HTTP流量。

OpenVPN是一个功能齐全的开源安全套接字层(SSL)VPN解决方案,适用于各种配置。 在本教程中,您将在Debian 9服务器上设置OpenVPN服务器,然后从Windows,macOS,iOS和/或Android配置对它的访问。 本教程将使每个安装程序的安装和配置步骤尽可能简单。

注意:如果您计划在DigitalOcean Droplet上设置OpenVPN服务器,请注意我们与许多托管服务提供商一样,收取带宽超额费用。 因此,请注意服务器处理的流量。

有关详细信息,请参阅此页面

先决条件

要完成本教程,您需要访问Debian 9服务器来托管您的OpenVPN服务。 在开始本指南之前,您需要使用sudo权限配置非root用户。 您可以按照我们的Debian 9初始服务器设置指南来设置具有适当权限的用户。 链接的教程还将设置一个防火墙 ,假设该防火墙在本指南中已经到位。

此外,您还需要一台单独的计算机作为证书颁发机构(CA)。 虽然技术上可以将您的OpenVPN服务器或本地计算机用作您的CA,但不建议这样做,因为它会将您的VPN打开到某些安全漏洞。 根据官方OpenVPN文档 ,您应将CA放在专用于导入和签署证书请求的独立计算机上。 出于这个原因,本指南假设您的CA位于单独的Debian 9服务器上,该服务器还具有sudo权限和基本防火墙的非root用户。

请注意,如果在配置这些服务器时禁用密码身份验证,则在本指南中稍后在它们之间传输文件时可能会遇到困难。 要解决此问题,您可以在每台服务器上重新启用密码身份验证。 或者,您可以为每个服务器生成SSH密钥对,然后将OpenVPN服务器的公共SSH密钥添加到CA计算机的authorized_keys文件中,反之亦然。 有关如何执行这些解决方案的说明,请参见如何在Debian 9上设置SSH密钥

当您具备这些先决条件后,您可以继续本教程的第1步。

第1步 - 安装OpenVPN和EasyRSA

首先,更新VPN服务器的软件包索引并安装OpenVPN。 OpenVPN在Debian的默认存储库中可用,因此您可以使用apt进行安装:

sudo apt update
sudo apt install openvpn

OpenVPN是TLS / SSL VPN。 这意味着它利用证书来加密服务器和客户端之间的流量。 要颁发受信任的证书,您将设置自己的简单证书颁发机构(CA)。 为此,我们将从项目的官方GitHub存储库下载最新版本的EasyRSA,我们将用它来构建我们的CA公钥基础结构(PKI)。

如前提条件中所述,我们将在独立服务器上构建CA. 这种方法的原因是,如果攻击者能够渗透到您的服务器,他们将能够访问您的CA私钥并使用它来签署新证书,使他们能够访问您的VPN。 因此,从独立计算机管理CA有助于防止未经授权的用户访问您的VPN。 另请注意,建议您在不用于签名密钥时关闭CA服务器作为进一步的预防措施。

要开始构建CA和PKI基础结构,请使用以下命令在CA计算机和OpenVPN服务器上的官方GitHub项目中安装最新版本的EasyRSA:

wget -P ~/ https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.4/EasyRSA-3.0.4.tgz

然后解压缩tarball:

cd ~
tar xvf EasyRSA-3.0.4.tgz

您已成功在服务器和CA计算机上安装了所有必需的软件。 继续配置EasyRSA使用的变量并设置CA目录,从中生成服务器和客户端访问VPN所需的密钥和证书。

第2步 - 配置EasyRSA变量和构建CA.

EasyRSA安装了一个配置文件,您可以编辑该文件以定义CA的许多变量。

CA计算机上 ,导航到EasyRSA目录:

cd ~/EasyRSA-3.0.4/

在此目录中是一个名为vars.example的文件。 制作此文件的副本,并命名没有文件扩展名的副本vars

cp vars.example vars

使用首选文本编辑器打开此新文件:

nano vars

查找为新证书设置字段默认值的设置。 它看起来像这样:

〜/ EasyRSA-3.0.4 /瓦尔
. . .

#set_var EASYRSA_REQ_COUNTRY    "US"
#set_var EASYRSA_REQ_PROVINCE   "California"
#set_var EASYRSA_REQ_CITY       "San Francisco"
#set_var EASYRSA_REQ_ORG        "Copyleft Certificate Co"
#set_var EASYRSA_REQ_EMAIL      "[email protected]"
#set_var EASYRSA_REQ_OU         "My Organizational Unit"

. . .

取消注释这些行并将突出显示的值更新为您喜欢的任何值,但不要将它们留空:

〜/ EasyRSA-3.0.4 /瓦尔
. . .

set_var EASYRSA_REQ_COUNTRY    "US"
set_var EASYRSA_REQ_PROVINCE   "NewYork"
set_var EASYRSA_REQ_CITY       "New York City"
set_var EASYRSA_REQ_ORG        "DigitalOcean"
set_var EASYRSA_REQ_EMAIL      "[email protected]"
set_var EASYRSA_REQ_OU         "Community"

. . .

完成后,保存并关闭文件。

在EasyRSA目录中有一个名为easyrsa的脚本,它被调用来执行构建和管理CA所涉及的各种任务。 使用init-pki选项运行此脚本以启动CA服务器上的公钥基础结构:

./easyrsa init-pki
Output. . .
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /home/sammy/EasyRSA-3.0.4/pki

在此之后,再次使用build-ca选项调用easyrsa脚本。 这将构建CA并创建两个重要文件 - ca.crtca.key它们构成SSL证书的公共和私有方。

  • ca.crt是CA的公共证书文件,在OpenVPN的上下文中,服务器和客户端用于相互通知他们是同一个信任网络的一部分而不是执行中间人攻击的人。 因此,您的服务器和所有客户端都需要ca.crt文件的副本。
  • ca.key是CA机器用于为服务器和客户端签名密钥和证书的私钥。 如果攻击者获得对您的CA以及ca.key文件的访问权限,他们将能够签署证书请求并获得对VPN的访问权限,从而阻碍其安全性。 这就是为什么您的ca.key文件应在您的CA计算机上,并且理想情况下,在不将证书请求作为额外安全措施进行签名时,您的CA计算机应保持脱机状态。

如果您不希望每次与CA交互时都提示输入密码,则可以使用nopass选项运行build-ca命令,如下所示:

./easyrsa build-ca nopass

在输出中,系统会要求您确认CA的通用名称

Output. . .
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:

通用名称是用于在证书颁发机构的上下文中引用此计算机的名称。 您可以为CA的公用名输入任何字符串,但为简单起见,请按ENTER接受默认名称。

这样,您的CA就位,并且已准备好开始签署证书请求。

第3步 - 创建服务器证书,密钥和加密文件

现在您已准备好CA,您可以从服务器生成私钥和证书请求,然后将请求转移到您的CA进行签名,从而创建所需的证书。 您还可以自由创建加密过程中使用的一些其他文件。

首先导航到OpenVPN服务器上的EasyRSA目录:

cd EasyRSA-3.0.4/

从那里,使用init-pki选项运行easyrsa脚本。 虽然您已经在CA计算机上运行此命令,但是必须在此处运行它,因为您的服务器和CA将具有单独的PKI目录:

./easyrsa init-pki

然后再次调用easyrsa脚本,这次使用gen-req选项,后跟机器的通用名称。 同样,这可能是你喜欢的任何东西,但它可以有助于使它具有描述性。 在本教程中,OpenVPN服务器的通用名称将只是“服务器”。 一定要包括nopass选项。 如果不这样做,将对请求文件进行密码保护,这可能导致以后的权限问题:

注意 :如果您在此处选择“服务器”以外的名称,则必须调整以下某些说明。 例如,将生成的文件复制到/etc/openvpn目录时,您必须替换正确的名称。 您还必须稍后修改/etc/openvpn/server.conf文件以指向正确的.crt.key文件。

./easyrsa gen-req server nopass

这将为服务器创建一个私钥,并创建一个名为server.req的证书请求文件。 将服务器密钥复制到/etc/openvpn/目录:

sudo cp ~/EasyRSA-3.0.4/pki/private/server.key /etc/openvpn/

使用安全方法(如下面的示例中的SCP),将server.req文件传输到CA计算机:

scp ~/EasyRSA-3.0.4/pki/reqs/server.req sammy@your_CA_ip:/tmp

接下来,在CA计算机上 ,导航到EasyRSA目录:

cd EasyRSA-3.0.4/

再次使用easyrsa脚本,按照文件路径及其通用名称导入server.req文件:

./easyrsa import-req /tmp/server.req server

然后通过使用sign-req选项运行easyrsa脚本,然后是请求类型和公用名来sign-req 请求 请求类型可以是clientserver ,因此对于OpenVPN服务器的证书请求,请务必使用server请求类型:

./easyrsa sign-req server server

在输出中,系统会要求您验证请求是否来自可信来源。 键入yes然后按ENTER确认:

You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 3650 days:

subject=
    commonName                = server


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes

如果您加密了CA密钥,此时将提示您输入密码。

接下来,使用安全方法将签名证书传回VPN服务器:

scp pki/issued/server.crt sammy@your_server_ip:/tmp

在注销CA计算机之前,还要将ca.crt文件传输到服务器:

scp pki/ca.crt sammy@your_server_ip:/tmp

接下来,重新登录到OpenVPN服务器并将server.crtca.crt文件复制到/etc/openvpn/目录中:

sudo cp /tmp/{server.crt,ca.crt} /etc/openvpn/

然后导航到EasyRSA目录:

cd EasyRSA-3.0.4/

从那里,通过键入以下内容创建一个强大的Diffie-Hellman密钥,以便在密钥交换期间使用:

./easyrsa gen-dh

这可能需要几分钟才能完成。 完成后,生成HMAC签名以增强服务器的TLS完整性验证功能:

sudo openvpn --genkey --secret ta.key

命令完成后,将两个新文件复制到/etc/openvpn/目录:

sudo cp ~/EasyRSA-3.0.4/ta.key /etc/openvpn/
sudo cp ~/EasyRSA-3.0.4/pki/dh.pem /etc/openvpn/

这样,就生成了服务器所需的所有证书和密钥文件。 您已准备好创建客户端计算机将用于访问OpenVPN服务器的相应证书和密钥。

第4步 - 生成客户端证书和密钥对

虽然您可以在客户端计算机上生成私钥和证书请求,然后将其发送到要签名的CA,但本指南概述了在服务器上生成证书请求的过程。 这样做的好处是我们可以创建一个脚本,该脚本将自动生成包含所有必需密钥和证书的客户端配置文件。 这使您可以避免必须将密钥,证书和配置文件传输到客户端,并简化加入VPN的过程。

我们将为本指南生成单个客户端密钥和证书对。 如果您有多个客户端,则可以为每个客户端重复此过程。 但请注意,您需要为每个客户端的脚本传递唯一的名称值。 在本教程中,第一个证书/密钥对称为client1

首先在主目录中创建目录结构以存储客户端证书和密钥文件:

mkdir -p ~/client-configs/keys

由于您将客户端的证书/密钥对和配置文件存储在此目录中,因此现在应将其权限锁定为安全措施:

chmod -R 700 ~/client-configs

接下来,导航回EasyRSA目录并运行带有gen-reqnopass选项的easyrsa脚本,以及客户端的通用名称:

cd ~/EasyRSA-3.0.4/
./easyrsa gen-req client1 nopass

ENTER确认通用名称。 然后,将client1.key文件复制到之前创建的/client-configs/keys/目录:

cp pki/private/client1.key ~/client-configs/keys/

接下来,使用安全方法将client1.req文件传输到CA计算机:

scp pki/reqs/client1.req sammy@your_CA_ip:/tmp

登录到您的CA计算机,导航到EasyRSA目录,然后导入证书请求:

ssh sammy@your_CA_IP
cd EasyRSA-3.0.4/
./easyrsa import-req /tmp/client1.req client1

然后像在上一步中为服务器所做的那样签署请求。 但是,这一次,请务必指定client请求类型:

./easyrsa sign-req client client1

在提示符下,输入yes以确认您要签署证书请求并且它来自可信来源:

OutputType the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes

同样,如果您加密了CA密钥,系统会在此处提示您输入密码。

这将创建名为client1.crt的客户端证书文件。 将此文件传回服务器:

scp pki/issued/client1.crt sammy@your_server_ip:/tmp

SSH回到您的OpenVPN服务器并将客户端证书复制到/client-configs/keys/目录:

cp /tmp/client1.crt ~/client-configs/keys/

接下来,将ca.crtta.key文件复制到/client-configs/keys/目录:

sudo cp EasyRSA-3.0.4/ta.key ~/client-configs/keys/
sudo cp /etc/openvpn/ca.crt ~/client-configs/keys/

这样,您的服务器和客户端的证书和密钥都已生成并存储在服务器上的相应目录中。 仍然需要对这些文件执行一些操作,但这些操作将在稍后的步骤中进行。 现在,您可以继续在服务器上配置OpenVPN。

第5步 - 配置OpenVPN服务

既然已经生成了客户端和服务器的证书和密钥,您就可以开始配置OpenVPN服务以使用这些凭据。

首先将示例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 nano /etc/openvpn/server.conf

通过查找tls-auth指令找到HMAC部分。 此行应已取消注释,但如果不是,则删除“ ; ”以取消注释。 在此行下方,添加key-direction参数,设置为“0”:

/etc/openvpn/server.conf
tls-auth ta.key 0 # This file is secret
key-direction 0

接下来,通过查找注释掉的cipher行找到有关加密密码的部分。 AES-256-CBC密码提供了良好的加密级别,并得到了很好的支持。 同样,这一行应该已经取消注释,但如果不是,那么只需删除它前面的“ ; ”:

/etc/openvpn/server.conf
cipher AES-256-CBC

在此下方,添加auth指令以选择HMAC消息摘要算法。 为此, SHA256是一个不错的选择:

/etc/openvpn/server.conf
auth SHA256

接下来,找到包含dh指令的行,该指令定义Diffie-Hellman参数。 由于最近对EasyRSA进行了一些更改,因此Diffie-Hellman密钥的文件名可能与示例服务器配置文件中列出的文件名不同。 如有必要,请通过删除2048更改此处列出的文件名,以使其与您在上一步中生成的密钥对齐:

/etc/openvpn/server.conf
dh dh.pem

最后,找到usergroup设置并删除每个开头的“ ; ”以取消注释这些行:

/etc/openvpn/server.conf
user nobody
group nogroup

到目前为止,您对示例server.conf文件所做的更改对于OpenVPN的运行是必要的。 下面列出的更改是可选的,但许多常见用例也需要它们。

(可选)推送DNS更改以通过VPN重定向所有流量

上面的设置将在两台计算机之间创建VPN连接,但不会强制任何连接使用隧道。 如果您希望使用VPN路由所有流量,则可能需要将DNS设置推送到客户端计算机。

server.conf文件中有一些指令,您必须更改这些指令才能启用此功能。 找到redirect-gateway部分并从redirect-gateway行的开头删除分号“ ; ”以取消注释:

/etc/openvpn/server.conf
push "redirect-gateway def1 bypass-dhcp"

在此下方,找到dhcp-option部分。 再次,从两行前面删除“ ; ”以取消注释:

/etc/openvpn/server.conf
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"

这将帮助客户重新配置其DNS设置,以将VPN隧道用作默认网关。

(可选)调整端口和协议

默认情况下,OpenVPN服务器使用端口1194和UDP协议来接受客户端连接。 如果由于客户端可能存在的限制性网络环境而需要使用其他端口,则可以更改port选项。 如果您不在OpenVPN服务器上托管Web内容,则端口443是一种流行的选择,因为它通常允许通过防火墙规则。

/etc/openvpn/server.conf
# Optional!
port 443

通常,协议也限于该端口。 如果是这样,将proto从UDP更改为TCP:

/etc/openvpn/server.conf
# Optional!
proto tcp

如果将协议切换为TCP,则需要将explicit-exit-notify指令的值从1更改为0 ,因为此指令仅由UDP使用。 使用TCP时未能执行此操作将导致启动OpenVPN服务时出错:

/etc/openvpn/server.conf
# Optional!
explicit-exit-notify 0

如果您不需要使用其他端口和协议,则最好将这两个设置保留为默认值。

(可选)指向非默认凭据

如果在之前的./build-key-server命令中选择了其他名称,请修改您看到的certkey行以指向相应的.crt.key文件。 如果您使用默认名称“server”,则已正确设置:

/etc/openvpn/server.conf
cert server.crt
key server.key

完成后,保存并关闭文件。

在完成对服务器的任何更改后,您的特定用例需要对OpenVPN配置进行更改,您可以开始对服务器的网络进行一些更改。

第6步 - 调整服务器网络配置

服务器的网络配置有一些方面需要调整,以便OpenVPN可以正确地通过VPN路由流量。 第一种是IP转发 ,一种确定应该路由IP流量的方法。 这对于服务器将提供的VPN功能至关重要。

通过修改/etc/sysctl.conf文件来调整服务器的默认IP转发设置:

sudo nano /etc/sysctl.conf

在里面,查找设置net.ipv4.ip_forward的注释行。 从行的开头删除“ ”字符以取消注释此设置:

/etc/sysctl.conf中
net.ipv4.ip_forward=1

完成后保存并关闭文件。

要读取文件并调整当前会话的值,请键入:

sudo sysctl -p
Outputnet.ipv4.ip_forward = 1

如果您按照先决条件中列出的Debian 9初始服务器设置指南进行操作,则应该具有UFW防火墙。 无论您是使用防火墙阻止不需要的流量(您几乎总是应该这样做),对于本指南,您需要一个防火墙来操纵进入服务器的一些流量。 必须修改某些防火墙规则以启用伪装,这是一种iptables概念,它提供动态动态网络地址转换(NAT)以正确路由客户端连接。

在打开防火墙配置文件以添加伪装规则之前,必须先找到计算机的公共网络接口。 为此,请键入:

ip route | grep default

您的公共接口是此命令输出中的字符串“dev”后面的字符串。 例如,此结果显示名为eth0的接口,其突出显示如下:

Outputdefault via 203.0.113.1 dev eth0 onlink

当您具有与默认路由关联的接口时,请打开/etc/ufw/before.rules文件以添加相关配置:

sudo nano /etc/ufw/before.rules

通常使用ufw命令添加UFW规则。 但是,在加载传统的UFW规则之前,会读取并before.rulesbefore.rules文件中列出的规则。 在文件顶部,添加下面突出显示的行。 这将为nat表中的POSTROUTING链设置默认策略,并伪装来自VPN的任何流量。 请记住使用上面命令中找到的接口替换下面-A POSTROUTING行中的eth0

/etc/ufw/before.rules
#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
#   ufw-before-input
#   ufw-before-output
#   ufw-before-forward
#

# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0] 
# Allow traffic from OpenVPN client toeth0(change to the interface you discovered!)
-A POSTROUTING -s 10.8.0.0/8 -oeth0-j MASQUERADE
COMMIT
# END OPENVPN RULES

# Don't delete these required lines, otherwise there will be errors
*filter
. . .

完成后保存并关闭文件。

接下来,您还需要告诉UFW默认允许转发数据包。 为此,请打开/etc/default/ufw文件:

sudo nano /etc/default/ufw

在里面,找到DEFAULT_FORWARD_POLICY指令并将值从DROP更改为ACCEPT

在/ etc /默认/ UFW
DEFAULT_FORWARD_POLICY="ACCEPT"

完成后保存并关闭文件。

接下来,调整防火墙本身以允许流量到OpenVPN。 如果未更改/etc/openvpn/server.conf文件中的端口和协议,则需要打开到端口1194 UDP流量。 如果您修改了端口和/或协议,请替换此处选择的值。

如果您在遵循先决条件教程时忘记添加SSH端口,请将其添加到此处:

sudo ufw allow 1194/udp
sudo ufw allow OpenSSH

添加这些规则后,禁用并重新启用UFW以重新启动它并从您修改的所有文件加载更改:

sudo ufw disable
sudo ufw enable

您的服务器现在配置为正确处理OpenVPN流量。

第7步 - 启动和启用OpenVPN服务

您终于准备好在您的服务器上启动OpenVPN服务。 这是使用systemd实用程序systemctl

通过在systemd单元文件名后指定配置文件名作为实例变量来启动OpenVPN服务器。 服务器的配置文件名为/etc/openvpn/ server .conf ,因此在调用时将@server添加到单元文件的末尾:

sudo systemctl start openvpn@server

键入以下内容,仔细检查服务是否已成功启动:

sudo systemctl status openvpn@server

如果一切顺利,您的输出将如下所示:

Output● [email protected] - OpenVPN connection to server
   Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2016-05-03 15:30:05 EDT; 47s ago
     Docs: man:openvpn(8)
           https://community.openvpn.net/openvpn/wiki/Openvpn23ManPage
           https://community.openvpn.net/openvpn/wiki/HOWTO
  Process: 5852 ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid (code=exited, sta
 Main PID: 5856 (openvpn)
    Tasks: 1 (limit: 512)
   CGroup: /system.slice/system-openvpn.slice/[email protected]
           └─5856 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/server.conf --writepid /run/openvpn/server.pid

您还可以通过键入以下内容来检查OpenVPN tun0接口是否可用:

ip addr show tun0

这将输出一个配置的接口:

Output4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever

启动服务后,启用它以便在启动时自动启动:

sudo systemctl enable openvpn@server

您的OpenVPN服务现已启动并运行。 但是,在开始使用它之前,必须先为客户端计算机创建配置文件。 本教程已经讨论了如何为客户端创建证书/密钥对,在下一步中,我们将演示如何创建一个可以轻松生成客户端配置文件的基础结构。

第8步 - 创建客户端配置基础结构

为OpenVPN客户端创建配置文件可能会有所涉及,因为每个客户端都必须有自己的配置,并且每个客户端都必须与服务器配置文件中列出的设置保持一致。 此步骤不是编写只能在一个客户端上使用的单个配置文件,而是概述了构建客户端配置基础结构的过程,您可以使用该过程来动态生成配置文件。 您将首先创建一个“基本”配置文件,然后构建一个脚本,该脚本允许您根据需要生成唯一的客户端配置文件,证书和密钥。

首先创建一个新目录,在该目录中将客户端配置文件存储在先前创建的client-configs目录中:

mkdir -p ~/client-configs/files

接下来,将示例客户端配置文件复制到client-configs目录中以用作基本配置:

cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf

在文本编辑器中打开此新文件:

nano ~/client-configs/base.conf

在里面,找到remote指令。 这将客户端指向您的OpenVPN服务器地址 - 您的OpenVPN服务器的公共IP地址。 如果您决定更改OpenVPN服务器正在监听的端口,您还需要将1194更改为您选择的端口:

〜/客户CONFIGS / base.conf
. . .
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote your_server_ip 1194
. . .

确保协议与您在服务器配置中使用的值匹配:

〜/客户CONFIGS / base.conf
proto udp

接下来,通过删除每行开头的“ ; ”来取消注释usergroup指令:

〜/客户CONFIGS / base.conf
# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup

找到设置cacertkey的指令。 注释掉这些指令,因为您将很快在文件中添加证书和密钥:

〜/客户CONFIGS / base.conf
# 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 ca.crt
#cert client.crt
#key client.key

镜像您在/etc/openvpn/server.conf文件中设置的cipherauth设置:

〜/客户CONFIGS / base.conf
cipher AES-256-CBC
auth SHA256

接下来,在文件中的某处添加key-direction指令。 必须将此值设置为“1”才能使VPN在客户端计算机上正常运行:

〜/客户CONFIGS / base.conf
key-direction 1

最后,添加一些注释掉的行。 虽然您可以在每个客户端配置文件中包含这些指令,但您只需为随/etc/openvpn/update-resolv-conf文件一起提供的Linux客户端启用它们。 此脚本使用resolvconf实用程序更新Linux客户端的DNS信息。

〜/客户CONFIGS / base.conf
# script-security 2
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf

如果您的客户/etc/openvpn/update-resolv-conf在运行Linux并且具有/etc/openvpn/update-resolv-conf文件,请在生成后从客户端的配置文件中取消注释这些行。

完成后保存并关闭文件。

接下来,创建一个简单的脚本,该脚本将使用相关的证书,密钥和加密文件编译基本配置,然后将生成的配置放在~/client-configs/files目录中。 ~/client-configs目录中打开一个名为make_config.sh的新文件:

nano ~/client-configs/make_config.sh

在里面,添加以下内容,确保将sammy更改为服务器的非root用户帐户的内容:

〜/客户CONFIGS / make_config.sh
#!/bin/bash

# First argument: Client identifier

KEY_DIR=/home/sammy/client-configs/keys
OUTPUT_DIR=/home/sammy/client-configs/files
BASE_CONFIG=/home/sammy/client-configs/base.conf

cat ${BASE_CONFIG} \
    <(echo -e '<ca>') \
    ${KEY_DIR}/ca.crt \
    <(echo -e '</ca>\n<cert>') \
    ${KEY_DIR}/${1}.crt \
    <(echo -e '</cert>\n<key>') \
    ${KEY_DIR}/${1}.key \
    <(echo -e '</key>\n<tls-auth>') \
    ${KEY_DIR}/ta.key \
    <(echo -e '</tls-auth>') \
    > ${OUTPUT_DIR}/${1}.ovpn

完成后保存并关闭文件。

在继续之前,请务必键入以下内容将此文件标记为可执行文件:

chmod 700 ~/client-configs/make_config.sh

此脚本将复制您创建的base.conf文件,收集您为客户端创建的所有证书和密钥文件,提取其内容,将其附加到基本配置文件的副本,然后导出所有这些内容到新的客户端配置文件中。 这意味着,不必分别管理客户端的配置,证书和密钥文件,所有必需的信息都存储在一个地方。 这样做的好处是,如果您将来需要添加客户端,您只需运行此脚本即可快速创建配置文件,并确保所有重要信息都存储在一个易于访问的位置。

请注意,每次添加新客户端时,都需要为其生成新的密钥和证书,然后才能运行此脚本并生成其配置文件。 您将在下一步中使用此脚本进行练习。

第9步 - 生成客户端配置

如果您按照指南进行操作,则在第4步中分别创建了一个客户端证书和名为client1.crtclient1.key密钥。您可以通过进入~/client-configs目录生成这些凭据的配置文件,运行您在上一步结束时创建的脚本:

cd ~/client-configs
sudo ./make_config.sh client1

这将在~/client-configs/files目录中创建一个名为client1.ovpn ~/client-configs/files

ls ~/client-configs/files
Outputclient1.ovpn

您需要将此文件传输到计划用作客户端的设备。 例如,这可能是您的本地计算机或移动设备。

While the exact applications used to accomplish this transfer will depend on your device's operating system and your personal preferences, a dependable and secure method is to use SFTP (SSH file transfer protocol) or SCP (Secure Copy) on the backend. This will transport your client's VPN authentication files over an encrypted connection.

Here is an example SFTP command using the client1.ovpn example which you can run from your local computer (macOS or Linux). It places the .ovpn file in your home directory:

sftp sammy@your_server_ip:client-configs/files/client1.ovpn ~/

Here are several tools and tutorials for securely transferring files from the server to a local computer:

Step 10 — Installing the Client Configuration

This section covers how to install a client VPN profile on Windows, macOS, Linux, iOS, and Android. None of these client instructions are dependent on one another, so feel free to skip to whichever is applicable to your device.

The OpenVPN connection will have the same name as whatever you called the .ovpn file. In regards to this tutorial, this means that the connection is named client1.ovpn , aligning with the first client file you generated.

视窗

Installing

Download the OpenVPN client application for Windows from OpenVPN's Downloads page . Choose the appropriate installer version for your version of Windows.

注意
OpenVPN needs administrative privileges to install.

After installing OpenVPN, copy the .ovpn file to:

C:\Program Files\OpenVPN\config

When you launch OpenVPN, it will automatically see the profile and makes it available.

You must run OpenVPN as an administrator each time it's used, even by administrative accounts. To do this without having to right-click and select Run as administrator every time you use the VPN, you must preset this from an administrative account. This also means that standard users will need to enter the administrator's password to use OpenVPN. On the other hand, standard users can't properly connect to the server unless the OpenVPN application on the client has admin rights, so the elevated privileges are necessary.

To set the OpenVPN application to always run as an administrator, right-click on its shortcut icon and go to Properties . At the bottom of the Compatibility tab, click the button to Change settings for all users . In the new window, check Run this program as an administrator .

Connecting

Each time you launch the OpenVPN GUI, Windows will ask if you want to allow the program to make changes to your computer. Click Yes . Launching the OpenVPN client application only puts the applet in the system tray so that you can connect and disconnect the VPN as needed; it does not actually make the VPN connection.

Once OpenVPN is started, initiate a connection by going into the system tray applet and right-clicking on the OpenVPN applet icon. This opens the context menu. Select client1 at the top of the menu (that's your client1.ovpn profile) and choose Connect .

A status window will open showing the log output while the connection is established, and a message will show once the client is connected.

Disconnect from the VPN the same way: Go into the system tray applet, right-click the OpenVPN applet icon, select the client profile and click Disconnect .

苹果系统

Installing

Tunnelblick is a free, open source OpenVPN client for macOS. You can download the latest disk image from the Tunnelblick Downloads page . Double-click the downloaded .dmg file and follow the prompts to install.

Towards the end of the installation process, Tunnelblick will ask if you have any configuration files. For simplicity, answer No and let Tunnelblick finish. Open a Finder window and double-click client1.ovpn . Tunnelblick will install the client profile. Administrative privileges are required.

Connecting

Launch Tunnelblick by double-clicking Tunnelblick in the Applications folder. Once Tunnelblick has been launched, there will be a Tunnelblick icon in the menu bar at the top right of the screen for controlling connections. Click on the icon, and then the Connect menu item to initiate the VPN connection. Select the client1 connection.

Linux的

Installing

If you are using Linux, there are a variety of tools that you can use depending on your distribution. Your desktop environment or window manager might also include connection utilities.

The most universal way of connecting, however, is to just use the OpenVPN software.

On Ubuntu or Debian, you can install it just as you did on the server by typing:

sudo apt update
sudo apt install openvpn

On CentOS you can enable the EPEL repositories and then install it by typing:

sudo yum install epel-release
sudo yum install openvpn

Configuring

Check to see if your distribution includes an /etc/openvpn/update-resolv-conf script:

ls /etc/openvpn
Outputupdate-resolv-conf

Next, edit the OpenVPN client configuration file you transfered:

nano client1.ovpn

If you were able to find an update-resolv-conf file, uncomment the three lines you added to adjust the DNS settings:

client1.ovpn
script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf

If you are using CentOS, change the group directive from nogroup to nobody to match the distribution's available groups:

client1.ovpn
group nobody

保存并关闭文件。

Now, you can connect to the VPN by just pointing the openvpn command to the client configuration file:

sudo openvpn --config client1.ovpn

This should connect you to your VPN.

iOS

Installing

From the iTunes App Store, search for and install OpenVPN Connect , the official iOS OpenVPN client application. To transfer your iOS client configuration onto the device, connect it directly to a computer.

The process of completing the transfer with iTunes is outlined here. Open iTunes on the computer and click on iPhone > apps . Scroll down to the bottom to the File Sharing section and click the OpenVPN app. The blank window to the right, OpenVPN Documents , is for sharing files. Drag the .ovpn file to the OpenVPN Documents window.

iTunes showing the VPN profile ready to load on the iPhone

Now launch the OpenVPN app on the iPhone. You will receive a notification that a new profile is ready to import. Tap the green plus sign to import it.

The OpenVPN iOS app showing new profile ready to import

Connecting

OpenVPN is now ready to use with the new profile. Start the connection by sliding the Connect button to the On position. Disconnect by sliding the same button to Off .

注意
The VPN switch under Settings cannot be used to connect to the VPN. If you try, you will receive a notice to only connect using the OpenVPN app.

The OpenVPN iOS app connected to the VPN

Android的

Installing

Open the Google Play Store. Search for and install Android OpenVPN Connect , the official Android OpenVPN client application.

You can transfer the .ovpn profile by connecting the Android device to your computer by USB and copying the file over. Alternatively, if you have an SD card reader, you can remove the device's SD card, copy the profile onto it and then insert the card back into the Android device.

Start the OpenVPN app and tap the menu to import the profile.

The OpenVPN Android app profile import menu selection

Then navigate to the location of the saved profile (the screenshot uses /sdcard/Download/ ) and select the file. The app will make a note that the profile was imported.

The OpenVPN Android app selecting VPN profile to import

Connecting

To connect, simply tap the Connect button. You'll be asked if you trust the OpenVPN application. Choose OK to initiate the connection. To disconnect from the VPN, go back to the OpenVPN app and choose Disconnect .

The OpenVPN Android app ready to connect to the VPN

Step 11 — Testing Your VPN Connection (Optional)

Note: This method for testing your VPN connection will only work if you opted to route all your traffic through the VPN in Step 5.

Once everything is installed, a simple check confirms everything is working properly. Without having a VPN connection enabled, open a browser and go to DNSLeakTest .

The site will return the IP address assigned by your internet service provider and as you appear to the rest of the world. To check your DNS settings through the same website, click on Extended Test and it will tell you which DNS servers you are using.

Now connect the OpenVPN client to your server's VPN and refresh the browser. A completely different IP address (that of your VPN server) should now appear, and this is how you appear to the world. Again, DNSLeakTest's Extended Test will check your DNS settings and confirm you are now using the DNS resolvers pushed by your VPN.

Step 12 — Revoking Client Certificates

Occasionally, you may need to revoke a client certificate to prevent further access to the OpenVPN server.

To do so, navigate to the EasyRSA directory on your CA machine:

cd EasyRSA-3.0.4/

Next, run the easyrsa script with the revoke option, followed by the client name you wish to revoke:

./easyrsa revoke client2

This will ask you to confirm the revocation by entering yes :

OutputPlease confirm you wish to revoke the certificate with the following subject:

subject=
    commonName                = client2


Type the word 'yes' to continue, or any other input to abort.
  Continue with revocation: yes

After confirming the action, the CA will fully revoke the client's certificate. However, your OpenVPN server currently has no way to check whether any clients' certificates have been revoked and the client will still have access to the VPN. To correct this, create a certificate revocation list (CRL) on your CA machine:

./easyrsa gen-crl

This will generate a file called crl.pem . Securely transfer this file to your OpenVPN server:

scp ~/EasyRSA-3.0.4/pki/crl.pem sammy@your_server_ip:/tmp

On your OpenVPN server, copy this file into your /etc/openvpn/ directory:

sudo cp /tmp/crl.pem /etc/openvpn

Next, open the OpenVPN server configuration file:

sudo nano /etc/openvpn/server.conf

At the bottom of the file, add the crl-verify option, which will instruct the OpenVPN server to check the certificate revocation list that we've created each time a connection attempt is made:

/etc/openvpn/server.conf
crl-verify crl.pem

保存并关闭文件。

Finally, restart OpenVPN to implement the certificate revocation:

sudo systemctl restart openvpn@server

The client should no longer be able to successfully connect to the server using the old credential.

To revoke additional clients, follow this process:

  1. Revoke the certificate with the ./easyrsa revoke client_name command
  2. Generate a new CRL
  3. Transfer the new crl.pem file to your OpenVPN server and copy it to the /etc/openvpn directory to overwrite the old list.
  4. Restart the OpenVPN service.

You can use this process to revoke any certificates that you've previously issued for your server.

结论

You are now securely traversing the internet protecting your identity, location, and traffic from snoopers and censors. If at this point you no longer need to issue certificates, it's recommended that you turn off your CA machine or otherwise disconnect it from the internet until you need to add or revoke certificates. This will help to prevent attackers from gaining access to your VPN.

To configure more clients, you only need to follow steps 4 and 9-11 for each additional device. To revoke access to clients, just follow step 12 .

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏