在 Linux 中安装和配置 OpenVPN 服务器的最简单方法

本指南描述了如何在基于 RPM 和 DEB 的系统中安装和配置 OpenVPN 服务器。 在本指南中,我们将使用一个名为 openvpn-安装 自动化整个 OpenVPN 服务器安装和配置过程。 即使您以前没有使用过 OpenVPN,此脚本也可以帮助您在几分钟内设置自己的 VPN 服务器。

让我们开始吧。

在 Linux 中安装和配置 OpenVPN 服务器

出于本指南的目的,我将使用两个运行 CentOS 7 64 位版本的系统。 一个充当 OpenVPN 服务器,另一个充当 openVPN 客户端。 以下是我的测试箱详细信息。

OpenVPN 服务器:

  • 操作系统 : CentOS 7 64bit 最小版
  • 知识产权 : 192.168.43.150/24
  • 主机名 : vpnserver.ostechnix.local

OpenVPN 客户端:

  • 操作系统 : CentOS 7 64bit 最小版
  • 知识产权 : 192.168.43.199/24

首先,我们将看到服务器端配置。

OpenVPN 服务器安装和配置

从其 GitHub 页面下载 openvpn-install 脚本。

wget https://git.io/vpn -O openvpn-install.sh

然后,以 root 用户身份使用以下命令运行脚本:

bash openvpn-install.sh

您将被要求回答一系列问题。 相应地回答他们。

确保 VPN 服务器的 IP 地址正确。 如果您使用多个 IP 地址,请输入您希望 OpenVPN 侦听的网络接口的 IP。

Welcome to this quick 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 I need to know the IPv4 address of the network interface you want OpenVPN
 listening to.
 IP address: 192.168.43.150

选择您要使用的协议。 我想使用 tcp 端口,因此我选择了 2 号。

Which protocol do you want for OpenVPN connections?
 1) UDP (recommended)
 2) TCP
Protocol [1-2]: 2

Enter 端口号。

What port do you want OpenVPN listening to?
Port: 1194

Enter 您想与 VPN 一起使用的 DNS 服务器详细信息。 我想使用 Google DNS 解析器,所以我选择了选项 2。

Which DNS do you want to use with the VPN?
 1) Current system resolvers
 2) Google
 3) OpenDNS
 4) NTT
 5) Hurricane Electric
 6) Verisign
DNS [1-6]: 2

我们已经到了最后一步。 Enter 您的客户端证书名称。 名称应该是单个单词,并且不应包含任何特殊字符。

Finally, tell me your name for the client certificate
Please, use one word only, no special characters
Client name: client

按 ENTER 键开始安装 OPENVPN 服务器。

Okay, that was all I needed. We are ready to setup your OpenVPN server now
Press any key to continue...

Npw,这个脚本将开始安装所有必要的软件包来设置 OpenVPN 服务器。 Anslo,它将创建所有必要的密钥和证书以向 VPN 客户端进行身份验证。 这将需要几分钟。

最后,脚本会询问您是否有任何外部 IP 地址。 如果您没有,只需将其留空并按 ENTER 键即可忽略。

If your server is NATed (e.g. LowEndSpirit), I need to know the external IP
If that's not the case, just ignore this and leave the next field blank
External IP: 

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服务器安装和配置完成。 正如您在最后一个输出中看到的,客户端配置详细信息存储在一个文件中 /root/client.ovpn. 您需要将此文件复制到所有 VPN 客户端系统。

我将 client.ovpn 文件复制到我的 VPN 客户端

scp client.ovpn [email protected]:/etc/openvpn/

接下来,我们需要配置 OpenVPN 客户端。

下载免费视频:“渗透测试方法培训课程(价值 99 美元!)免费”

OpenVPN 客户端配置

确保您已从 VPN 服务器系统复制了 client.ovpn 文件。 我已经将此文件复制到我的 VPN 客户端系统的 /etc/openvpn/ 目录中。

使用分发包管理器安装 OpenVPN 包。

yum install openvpn

接下来,运行以下命令与 VPN 服务器建立安全连接。

openvpn --config /etc/openvpn/client.ovpn

样本输出:

Wed Apr 5 18:50:44 2017 Unrecognized option or missing parameter(s) in /etc/openvpn/client.ovpn:14: block-outside-dns (2.3.14)
Wed Apr 5 18:50:44 2017 OpenVPN 2.3.14 x86_64-redhat-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Dec 7 2016
Wed Apr 5 18:50:44 2017 library versions: OpenSSL 1.0.1e-fips 11 Feb 2013, LZO 2.06
Wed Apr 5 18:50:44 2017 Control Channel Authentication: tls-auth using INLINE static key file
Wed Apr 5 18:50:44 2017 Outgoing Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication
Wed Apr 5 18:50:44 2017 Incoming Control Channel Authentication: Using 160 bit message hash 'SHA1' for HMAC authentication
Wed Apr 5 18:50:44 2017 Socket Buffers: R=[87380->87380] S=[16384->16384]
Wed Apr 5 18:50:44 2017 Attempting to establish TCP connection with [AF_INET]192.168.43.150:1194 [nonblock]
Wed Apr 5 18:50:45 2017 TCP connection established with [AF_INET]192.168.43.150:1194
Wed Apr 5 18:50:45 2017 TCPv4_CLIENT link local: [undef]
Wed Apr 5 18:50:45 2017 TCPv4_CLIENT link remote: [AF_INET]192.168.43.150:1194
Wed Apr 5 18:50:45 2017 TLS: Initial packet from [AF_INET]192.168.43.150:1194, sid=c6fb554e 362eb192
Wed Apr 5 18:50:45 2017 VERIFY OK: depth=1, CN=ChangeMe
Wed Apr 5 18:50:45 2017 Validating certificate key usage
Wed Apr 5 18:50:45 2017 ++ Certificate has key usage 00a0, expects 00a0
Wed Apr 5 18:50:45 2017 VERIFY KU OK
Wed Apr 5 18:50:45 2017 Validating certificate extended key usage
Wed Apr 5 18:50:45 2017 ++ Certificate has EKU (str) TLS Web Server Authentication, expects TLS Web Server Authentication
Wed Apr 5 18:50:45 2017 VERIFY EKU OK
Wed Apr 5 18:50:45 2017 VERIFY OK: depth=0, CN=server
Wed Apr 5 18:50:45 2017 Data Channel Encrypt: Cipher 'AES-256-CBC' initialized with 256 bit key
Wed Apr 5 18:50:45 2017 Data Channel Encrypt: Using 160 bit message hash 'SHA1' for HMAC authentication
Wed Apr 5 18:50:45 2017 Data Channel Decrypt: Cipher 'AES-256-CBC' initialized with 256 bit key
Wed Apr 5 18:50:45 2017 Data Channel Decrypt: Using 160 bit message hash 'SHA1' for HMAC authentication
Wed Apr 5 18:50:45 2017 Control Channel: TLSv1.2, cipher TLSv1/SSLv3 DHE-RSA-AES256-GCM-SHA384, 2048 bit RSA
Wed Apr 5 18:50:45 2017 [server] Peer Connection Initiated with [AF_INET]192.168.43.150:1194
Wed Apr 5 18:50:48 2017 SENT CONTROL [server]: 'PUSH_REQUEST' (status=1)
Wed Apr 5 18:50:48 2017 PUSH: Received control message: 'PUSH_REPLY,redirect-gateway def1 bypass-dhcp,dhcp-option DNS 8.8.8.8,dhcp-option DNS 8.8.4.4,route-gateway 10.8.0.1,topology subnet,ping 10,ping-restart 120,ifconfig 10.8.0.2 255.255.255.0'
Wed Apr 5 18:50:48 2017 OPTIONS IMPORT: timers and/or timeouts modified
Wed Apr 5 18:50:48 2017 OPTIONS IMPORT: --ifconfig/up options modified
Wed Apr 5 18:50:48 2017 OPTIONS IMPORT: route options modified
Wed Apr 5 18:50:48 2017 OPTIONS IMPORT: route-related options modified
Wed Apr 5 18:50:48 2017 OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified
Wed Apr 5 18:50:48 2017 ROUTE_GATEWAY 192.168.43.1/255.255.255.0 IFACE=enp0s3 HWADDR=08:00:27:28:98:6b
Wed Apr 5 18:50:48 2017 TUN/TAP device tun0 opened
Wed Apr 5 18:50:48 2017 TUN/TAP TX queue length set to 100
Wed Apr 5 18:50:48 2017 do_ifconfig, tt->ipv6=0, tt->did_ifconfig_ipv6_setup=0
Wed Apr 5 18:50:48 2017 /usr/sbin/ip link set dev tun0 up mtu 1500
Wed Apr 5 18:50:48 2017 /usr/sbin/ip addr add dev tun0 10.8.0.2/24 broadcast 10.8.0.255
Wed Apr 5 18:50:48 2017 /usr/sbin/ip route add 192.168.43.150/32 dev enp0s3
Wed Apr 5 18:50:48 2017 /usr/sbin/ip route add 0.0.0.0/1 via 10.8.0.1
Wed Apr 5 18:50:48 2017 /usr/sbin/ip route add 128.0.0.0/1 via 10.8.0.1
Wed Apr 5 18:50:48 2017 Initialization Sequence Completed

现在,检查是否 tun0(VPN 接口)已创建,并使用“ip addr”命令检查 VPN 接口 IP 地址:

ip addr

样本输出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 08:00:27:28:98:6b brd ff:ff:ff:ff:ff:ff
 inet 192.168.43.199/24 brd 192.168.43.255 scope global dynamic enp0s3
 valid_lft 42359sec preferred_lft 42359sec
 inet6 fe80::a00:27ff:fe28:986b/64 scope link 
 valid_lft forever preferred_lft forever
3: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
 link/none 
 inet 10.8.0.2/24 brd 10.8.0.255 scope global tun0
 valid_lft forever preferred_lft forever

正如您在上面的输出中看到的,我们的 VPN 服务器自动为 VPN 客户端分配了一个 IP 地址 10.8.0.2。

现在,尝试从您的 VPN 客户端系统 ping VPN 服务器:

ping -c3 10.8.0.1

样本输出:

PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.
64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=1.05 ms
64 bytes from 10.8.0.1: icmp_seq=2 ttl=64 time=1.94 ms
64 bytes from 10.8.0.1: icmp_seq=3 ttl=64 time=2.49 ms

--- 10.8.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 1.057/1.832/2.495/0.594 ms

恭喜! 我们现在已经在 CentOS 中成功安装和配置了 OpenVPN 服务器和客户端。 对于基于 DEB 的系统,如 Ubuntu 和 Linux Mint,此方法相同。 与手动安装不同,此脚本使 openvpn 的安装和配置更加容易。

干杯!

资源:

感谢您的光临!

帮助我们帮助您:

祝你有美好的一天!!