在Ubuntu 20.04/18.04/16.04上安装和配置OpenVPN服务器

时间:2020-02-23 14:38:04  来源:igfitidea点击:

我们是否希望安全地和安全地访问互联网,同时利用Wi-Fi接入点(如Wi-Fi接入点)的开放和不受信任的网络?
OpenVPN是一个功能齐全的开源安全套接字层(SSL)VPN解决方案,支持各种配置。
通过利用虚拟专用网络(VPN),我们可以安全地牢固地遍历不受信任的网络,就像我们在LAN内一样安全地遍历不受信任的网络。

在本教程中,将介绍Ubuntu 20.04/18.04/16.04上安装了OpenVPN服务器的简单方法,并准备好客户开始使用它。
我知道OpenVPN设置通过手动过程可能会挑战,特别是对于Linux和VPN没有经历的新用户。

在Ubuntu 20.04/18.04/16.04上安装和配置OpenVPN服务器

这种方法将与Debian家族分布以及Red Hat系列良好。
本教程特定于Ubuntu 20.04/18.04/16.04,但设置过程将类似于其他分布。
这是一种脚本的方式,所以任何具有基本Linux知识的人都可以遵循。

设置准备工作

在开始在Ubuntu服务器上安装任何包之前,我们始终建议确保更新所有系统包:

sudo apt update
sudo apt upgrade

在Ubuntu 20.04/18.04/16.04上安装和配置OpenVPN服务器

更新系统后,我们可以开始Ubuntu 20.04/18.04/16.04系统上的OpenVPN服务器的安装和配置。
我们将使用OpenVPN安装脚本,即使我们之前没有使用OpenVPN,也可以在不超过一分钟内设置自己的VPN服务器。
它被设计为尽可能不引人注目和普及。

按照以下步骤安装并运行OpenVPN服务器:

第1步:安装git

通过运行命令来安装git:

sudo apt-get install git

第2步:克隆OpenVPN-Install存储库

现在克隆了 openvpn-install使用Git工具安装在步骤1中的存储库:

$cd ~
$git clone https://github.com/Nyr/openvpn-install.git
Cloning into 'openvpn-install'...
remote: Counting objects: 345, done.
remote: Total 345 (delta 0), reused 0 (delta 0), pack-reused 345
Receiving objects: 100% (345/345), 99.15 KiB | 681.00 KiB/s, done.
Resolving deltas: 100% (170/170), done.

第3步:改变 openvpn-install并运行OpenVPN安装程序

CD到目录 openvpn-install由克隆创建并运行安装程序脚本。

$cd openvpn-install/
$ls -1
LICENSE.txt
README.md
openvpn-install.sh
$chmod +x openvpn-install.sh
$sudo ./openvpn-install.sh

我们将收到几次提示更改或者确认安装的默认设置

Welcome to this OpenVPN "road warrior" installer!
I need to ask you a few questions before starting the setup.
You can leave the default options and just press enter if you are ok with them.
First, provide the IPv4 address of the network interface you want OpenVPN
listening to.
IP address: 192.168.10.2
Which protocol do you want for OpenVPN connections?
1) UDP (recommended)
2) TCP
Protocol [1-2]: 1
What port do you want OpenVPN listening to?
Port: 1194
Which DNS do you want to use with the VPN?
1) Current system resolvers
2) 1.1.1.1
3) Google
4) OpenDNS
5) Verisign
DNS [1-5]: 1
Finally, tell me your name for the client certificate.
Please, use one word only, no special characters.
Client name: client
Okay, that was all I needed. We are ready to set up your OpenVPN server now.
Press any key to continue... <Enter>

<Enter>在回答所有问题后要启动安装过程:如果安装成功,我们应该在最后获得成功消息:

sing configuration from ./openssl-easyrsa.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'client'
Certificate is to be certified until May  4 07:53:27 2028 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
Using configuration from ./openssl-easyrsa.cnf
An updated CRL has been created.
CRL file: /etc/openvpn/easy-rsa/pki/crl.pem
394
Finished!
Your client configuration is available at: /root/client.ovpn
If you want to add more clients, you simply need to run this script again!

主要OpenVPN服务器配置文件是, /etc/openvpn/server.conf我们可以自由调整和调整喜好。

$cat  /etc/openvpn/server.conf 
port 1194
proto udp
dev tun
sndbuf 0
rcvbuf 0
ca ca.crt
cert server.crt
key server.key
dh dh.pem
auth SHA512
tls-auth ta.key 0
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
cipher AES-256-CBC
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
crl-verify crl.pem

一种 tun0在安装过程中将创建虚拟接口。
这是由OpenVPN客户端子网使用的。
确认其存在:

$ip ad | grep tun0
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100
    inet 10.8.0.1/24 brd 10.8.0.255 scope global tun0

此接口的默认子网是。 10.8.0.0/24.将分配OpenVPN服务器 10.8.0.1IP地址:

$ip route | grep tun0
10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.1

要测试这一点,请使用:

$sudo apt-get install traceroute

然后:

$traceroute 10.8.0.1
traceroute to 10.8.0.1 (10.8.0.1), 30 hops max, 60 byte packets
 1  node-01.theitroad.com (10.8.0.1)  0.050 ms  0.018 ms  0.019 ms

步骤4:生成OpenVPN用户配置文件(.ovpn文件)

完成步骤1到3后,VPN服务器已准备好使用。
我们需要生成用户使用的VPN配置文件。
我们用于安装的相同脚本将用于此。
它管理用户配置文件的创建和撤销。

# ./openvpn-install.sh 
Looks like OpenVPN is already installed.
What do you want to do?
   1) Add a new user
   2) Revoke an existing user
   3) Remove OpenVPN
   4) Exit
Select an option [1-4]: 1
Tell me a name for the client certificate.
Please, use one word only, no special characters.
Client name: josphat.mutai
Generating a 2048 bit RSA private key
...+++
.............................................................................................................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/josphat.mutai.key.8dsSsOTWPe'
----
Using configuration from ./openssl-easyrsa.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'josphat.mutai'
Certificate is to be certified until May  4 08:10:32 2028 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
Client josphat.mutai added, configuration is available at: /root/josphat.mutai.ovpn

从输出中可以确认我的个人资料的位置, /root/josphat.mutai.ovpn我们需要将此配置文件复制到用户。
还提供了相关私钥的位置 /etc/openvpn/easy-rsa/pki/private/josphat.mutai.key.8dsSsOTWPe

步骤5:从客户端连接到OpenVPN服务器

我们可以使用我们选择的VPN客户端在操作系统上配置OpenVPN客户端。
对于那些想要使用官方OpenVPN客户端的人,请转到下载页面并获取最新版本然后安装它。

一旦安装在Windows上,导航到带有的目录 ovpn配置文件,右键单击文件名,然后选择"在此配置文件上启动OpenVPN"

对于Linux用户,我们可以使用NetworkManager和OpenVPN插件连接到OpenVPN服务器。