当前位置 往事如风 Linux 正文 下一篇:

十步搭建 OpenVPN

我们支持保护隐私,不为我们有自己的秘密需要保护,只是我们认为保护隐私应该成为一项基本人权。所以我们坚信无论谁在什么时候行使这项权利,都应该不受拘束的获取必须的工具和服务。OpenVPN就是这样一种服务并且有多种工具(客户端) 来让我们利用并享受这种服务。
通过与一个OpenVPN服务器建立连接,我们基本上在我们的设备和远端运行OpenVPN的主机之间建立了一个安全的通信通道。尽管在两个端点之间的通信可能被截获,但是信息是经过高强度加密的所以实际上它对于攻击者没什么用。OpenVPN除了扮演加密通信通道的调解人,我们也可以通过设置使服务器扮演互联网网关的角色。通过这种方式,我们可以连接任何不安全的Wifi,然后迅速的链接到远程的OpenVPN服务器,然后在不需要考虑偷窥的人或者无聊的管理员的情况下运行需要上网的程序。(注意:OpenVPN服务器旁还是需要信任的管理员的。)
这篇文章将一步一步的教会你如何在Ubuntu Server 14.04 LTS上安装OpenVPN。OpenVPN所在的主机可能是云上的一台VPS,一台在我们家里某台电脑上运行的虚拟机,或者是一个老到你都快忘了的设备。

第一步 准备系统

我们需要Ubuntu Server主机的一个命令行终端,比如通过SSH从远程访问它。首先需要更新它的本地仓库数据:

sub0@delta:~$ sudo apt-get update

进行操作系统和已安装的包的升级,输入:

sub0@delta:~$ sudo apt-get dist-upgrade

如果升级了新内核,那就需要重启。当更新完成后,就该安装OpenVPN了:

sub0@delta:~$ sudo apt-get -y install openvpn easy-rsa dnsmasq

注意,我们用apt-get安装了三个包:

• openvpn提供了OpenVPN的核心
• easy-rsa包含了一些有用的密钥管理脚本
• dnsmasq是当我们的OpenVPN所在的主机将扮演客户端的路由器时会用到的域名服务器

第二步 生成证书和私钥

这是安装OpenVPN中最重要和最关键的一步,目的是建立公钥基础设施(PKI)。包括如下内容:

• 为OpenVPN服务器创建一个证书(公钥)和一个私钥
• 为每个OpenVPN客户端创建证书和私钥
• 建立一个证书颁发机构(CA)并创建证书和私钥。这个私钥用来给OpenVPN服务器和客户端的证书签名

从最后一个做起,我们先建立一个目录:

sub0@delta:~$ sudo mkdir /etc/openvpn/easy-rsa

然后把easy-rsa的文件拷过去:

sub0@delta:~$ sudo cp -r /usr/share/easy-rsa/* /etc/openvpn/easy-rsa

在我们创建CA的私钥之前,我们先编辑/etc/openvpn/easy-rsa/vars(我们喜欢用nano编辑器,不过这只是我们的喜好,你爱用什么用什么):

sub0@delta:~$ sudo nano /etc/openvpn/easy-rsa/vars

在文件的尾部,我们设置主证书和密钥的信息:

    export KEY_COUNTRY="GR"
    export KEY_PROVINCE="Central Macedonia"
    export KEY_CITY="Thessaloniki"
    export KEY_ORG="Parabing Creations"
    export KEY_EMAIL="nobody@parabing.com"
    export KEY_CN="VPNsRUS"
    export KEY_NAME="VPNsRUS"
    export KEY_OU="Parabing"
    export KEY_ALTNAMES="VPNsRUS"

你可以根据自己的情况设置不同的值。特别注意最后KEY_ALTNAMES这一行,尽管这不是原本vars文件中有的但是我们还是把它加到文件的尾部,不然build-ca脚本会运行失败。
保存更改,我们得按[CTRL+O]然后按[Enter]。想退出nano,请按[CTRL+X]。现在,我们要获得root访问权限,继续生成主证书和私钥(LCTT译注:请注意命令行账户发生了改变):

    sub0@delta:~$ sudo su
    root@delta:/home/sub0# cd /etc/openvpn/easy-rsa
    root@delta:/etc/openvpn/easy-rsa# source vars
    NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
    root@delta:/etc/openvpn/easy-rsa# sh clean-all
    root@delta:/etc/openvpn/easy-rsa# sh build-ca
    Generating a 1024 bit RSA private key
    ...++++++
    ................++++++
    writing new private key to 'ca.key'
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [GR]:
    State or Province Name (full name) [Central Macedonia]:
    Locality Name (eg, city) [Thessaloniki]:
    Organization Name (eg, company) [Parabing Creations]:
    Organizational Unit Name (eg, section) [Parabing]:
    Common Name (eg, your name or your server's hostname) [VPNsRUS]:
    Name [VPNsRUS]:
    Email Address [nobody@parabing.com]:
    root@delta:/etc/openvpn/easy-rsa#

在我们的例子中,所有问题的答案都选择了默认的。在运行了build-ca脚本后,我们就获得了主证书文件(keys/ca.crt)和对应的私钥(keys/ca.key)。私钥必须不计代价的保密。
第三步 生成OpenVPN服务器的证书和私钥
在我们为OpenVPN服务器生成证书和密钥之前,我们得给他起个名。我决定把它叫”delta”,然后运行build-key-server脚本来获取证书和密钥:

    root@delta:/etc/openvpn/easy-rsa# sh build-key-server delta
    Generating a 1024 bit RSA private key
    ....++++++
    ...++++++
    writing new private key to 'delta.key'
     -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [GR]:
    State or Province Name (full name) [Central Macedonia]:
    Locality Name (eg, city) [Thessaloniki]:
    Organization Name (eg, company) [Parabing Creations]:
    Organizational Unit Name (eg, section) [Parabing]:
    Common Name (eg, your name or your server's hostname) [delta]:
    Name [VPNsRUS]:deltaVPN
    Email Address [nobody@parabing.com]:

    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
    Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
    Check that the request matches the signature
    Signature ok
    The Subject's Distinguished Name is as follows
    countryName :PRINTABLE:'GR'
    stateOrProvinceName :PRINTABLE:'Central Macedonia'
    localityName :PRINTABLE:'Thessaloniki'
    organizationName :PRINTABLE:'Parabing Creations'
    organizationalUnitName:PRINTABLE:'Parabing'
    commonName :PRINTABLE:'delta'
    name :PRINTABLE:'deltaVPN'
    emailAddress :IA5STRING:'nobody@parabing.com'
    Certificate is to be certified until Apr 7 08:06:02 2024 GMT (3650 days)
    Sign the certificate? [y/n]:y

    1 out of 1 certificate requests certified, commit? [y/n]y
    Write out database with 1 new entries
    Data Base Updated
    root@delta:/etc/openvpn/easy-rsa#

当脚本成功运行完的时候,我们就得到了服务器的证书(keys/delta.crt)和私钥(keys/delta.key)。注意服务器证书被CA的私钥签名了。
第四步 生成Diffie-Hellman参数
幸亏有了Diffie-Hellman参数,我们才能在不安全的通信通道里安全的交换密钥。为了生成它我们需要键入:

    root@delta:/etc/openvpn/easy-rsa# sh build-dh
    Generating DH parameters, 2048 bit long safe prime, generator 2
    This is going to take a long time
    .......................+.....................................+..
    ...........................+..+.....................+...........
    ..............................................+.................
    .......................+........................................
    ................................................+...............
    .......................................++*++*++*
    root@delta:/etc/openvpn/easy-rsa#

证书,私钥和包含Diffie-Hellman参数的文件已生成,它们都储存在/etc/openvpn/easy-rsa/keys,所以我们到现在为止已经有如下五个文件了:

  1. ca.crt – 证书颁发机构(CA)的证书
    1. ca.key – CA的私钥
    2. delta.crt – OpenVPN服务器的证书
    3. delta.key – OpenVPN服务器的私钥
    4. dh2048.pem – Diffie-Hellman参数文件
    5. 你自己的OpenVPN服务器命名可能和我们的不同。现在我们需要拷贝除了ca.key的文件到/etc/openvpn:
    root@delta:/etc/openvpn/easy-rsa# cd keys
    root@delta:/etc/openvpn/easy-rsa/keys# cp ca.crt delta.crt delta.key dh2048.pem /etc/openvpn
    root@delta:/etc/openvpn/easy-rsa/keys# cd ..
    root@delta:/etc/openvpn/easy-rsa#

第五步 为OpenVPN客户端生成证书和私钥
试想我们的笔记本要连接OpenVPN服务器。为了实现这个很常见的情况,我们首先需要为客户端(比如:我们的笔记本)生成证书和私钥,在/etc/openvpn/easy-rsa有一个脚本帮我们完成这项工作:

    root@delta:/etc/openvpn/easy-rsa# source vars
    NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
    root@delta:/etc/openvpn/easy-rsa# ./build-key laptop
    Generating a 1024 bit RSA private key
    .......................................++++++
    ...................................................................................................++++++
    writing new private key to 'laptop.key'
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [GR]:
    State or Province Name (full name) [Central Macedonia]:
    Locality Name (eg, city) [Thessaloniki]:
    Organization Name (eg, company) [Parabing Creations]:
    Organizational Unit Name (eg, section) [Parabing]:
    Common Name (eg, your name or your server's hostname) [laptop]:
    Name [VPNsRUS]:
    Email Address [nobody@parabing.com]:

    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
    Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
    Check that the request matches the signature
    Signature ok
    The Subject's Distinguished Name is as follows
    countryName :PRINTABLE:'GR'
    stateOrProvinceName :PRINTABLE:'Central Macedonia'
    localityName :PRINTABLE:'Thessaloniki'
    organizationName :PRINTABLE:'Parabing Creations'
    organizationalUnitName:PRINTABLE:'Parabing'
    commonName :PRINTABLE:'laptop'
    name :PRINTABLE:'VPNsRUS'
    emailAddress :IA5STRING:'nobody@parabing.com'
    Certificate is to be certified until Apr 7 18:00:51 2024 GMT (3650 days)
    Sign the certificate? [y/n]:y

    1 out of 1 certificate requests certified, commit? [y/n]y
    Write out database with 1 new entries
    Data Base Updated
    root@delta:/etc/openvpn/easy-rsa#

我们为密钥选取的名字是”laptop”,当build-key脚本运行完之后,我们就得到了在keys/laptop.crt的证书和在keys/laptop.key的私钥。有了这两个文件和CA的证书,我们得把这三个文件拷贝到用户有(比如用户sub0)权访问的地方。比如我们可以在用户的home目录中新建一个目录并把三个文件拷贝过去:

    root@delta:/etc/openvpn/easy-rsa# mkdir /home/sub0/ovpn-client
    root@delta:/etc/openvpn/easy-rsa# cd keys
    root@delta:/etc/openvpn/easy-rsa/keys# cp ca.crt laptop.crt laptop.key /home/sub0/ovpn-client
    root@delta:/etc/openvpn/easy-rsa/keys# chown -R sub0:sub0 /home/sub0/ovpn-client
    root@delta:/etc/openvpn/easy-rsa/keys# cd ..
    root@delta:/etc/openvpn/easy-rsa#

ovpn-client文件夹必须安全的拷贝到我们的笔记本电脑上。我们可以给多个客户端分发这三个文件。当然了,等我们需要一个不一样的证书-私钥对的时候只要再次运行build-key脚本即可。

参考文献

十步搭建 OpenVPN

本文来自网络,不代表往事如风立场,转载请注明出处:https://www.pastlikewind.com/2020/02/03/1107/

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

返回顶部