在 Ubuntu 22.04 上设置 WireGuard 和 IPsec VPN 服务器 |在 Ubuntu 22.04 上设置 WireGuard 和 IPsec VPN 服务器 |在 Ubuntu 22.04 上设置 WireGuard 和 IPsec VPN 服务器 |在 Ubuntu 22.04 上设置 WireGuard 和 IPsec VPN 服务器 |
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

在 Ubuntu 22.04 上设置 WireGuard 和 IPsec VPN 服务器 |

发表 admin at 2025年2月28日
类别
  • 未分类
标签

VPN 代表虚拟专用网络。它是一种允许用户通过互联网创建与另一个网络的安全加密连接的技术。 VPN 通过加密所有通过它的流量来提供隐私和匿名性,使任何人都难以拦截或跟踪用户的在线活动。

VPN 有多种类型,包括站点到站点 VPN、远程访问 VPN、移动 VPN、云 VPN、混合 VPN

WireGuard 是一种相对较新且现代的开源 VPN 协议,旨在提供快速、安全且简单的 VPN 连接。此 VPN 协议可用于各种 VPN 类型,例如远程访问 VPN 和站点到站点 VPN。它已集成到许多操作系统和平台中,包括 Linux、Android、iOS 和 macOS。 Wireguard 的设计注重性能、极简主义和简单性,旨在易于配置和使用。

与其他 VPN 协议相比,WireGuard 的代码库较小,这使得审核和维护更加容易。它使用先进的加密算法来提供安全连接,并且可以在 IPv4 和 IPv6 网络上运行。 WireGuard 的设计还比其他 VPN 协议更快、更高效,从而提高网络性能并减少延迟。

Algo VPN 是一组 Ansible 脚本,可以更轻松地在本地或云端运行 WireGuard 和 IPsec VPN。 Algo VPN 由 Trail of Bits 设计,旨在简化 Wireguad VPN 安装。它允许用户从任何设备进行连接,包括 Android、Linux、OSX、iOS 等。

与 Algo VPN 相关的功能包括:

  • 它可用于安装到多个云平台,例如 DigitalOcean、Amazon Lightsail、Amazon EC2、Vultr、Microsoft Azure、Google Compute Engine、Scaleway、OpenStack、CloudStack、Hetzner Cloud、Linode 或您自己的 Ubuntu 服务器(适用于更高级的用户) )
  • 支持适用于 iOS、macOS、Linux、Android 和 Windows 10 的 WireGuard
  • 包括用于添加和删除用户的帮助程序脚本
  • 仅支持适用于 iOS、macOS 和 Linux 的具有强加密(AES-GCM、SHA2 和 P-256)的 IKEv2
  • 它生成 Apple 配置文件以自动配置 iOS 和 macOS 设备
  • 为 iOS、macOS、Android 和 Windows WireGuard 客户端生成 .conf 文件和 QR 代码
  • 基于当前版本的 Ubuntu 和 StrongSwan

#1.安装所需的软件包

对于本指南,您需要在云中本地运行的 Ubuntu 22.04|20.04|18.04。由于 Algo 是 Ansible 脚本,因此您需要安装 Python 3.8 或更高版本。

这可以使用以下命令来完成:

sudo apt update
sudo apt install -y --no-install-recommends  python3-virtualenv libffi-dev libssl-dev git

安装完成后,检查Python版本:

$ python3 --version
Python 3.10.6

另外,克隆 Algo 存储库:

sudo su -
git clone https://github.com/trailofbits/algo.git

安装 Algo 依赖项:

cd algo
python3 -m pip install -U pip virtualenv
python3 -m virtualenv --python=$(which python3) env
source env/bin/activate
python3 -m pip install -r requirements.txt

#2.配置 WireGuard 和 IPsec VPN 服务器

Algo 允许您在远程主机上运行 Ansible 脚本或在本地计算机上执行脚本。对于本指南,我们将在本地计算机上运行脚本。

但在此之前,我们需要对 Algo 进行一些配置。这些包括:

在 Algo 上创建用户

配置存储在config.cfg文件中。使用您最喜欢的编辑器打开文件:

vim config.cfg

在该文件中,指定安装期间要创建的用户。对于本指南,我们将有 4 位用户,如下所示:

users:
  - phone
  - linux
  - windows

禁用系统解析

为了让 dnsmasq 工作,我们需要禁用解析服务:

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

删除到resolv.conf的符号链接:

sudo unlink /etc/resolv.conf

现在创建一个新的conf文件,如下所示:

$ sudo vim /etc/resolv.conf
ameserver 8.8.8.8

#3。安装 WireGuard 和 IPsec VPN 服务器

完成上述配置后,您就可以开始 Wireguard 部署了。安装会经历一系列的问题。

开始安装:

./algo

请按如下方式进行。

PLAY [localhost] *****************************************************************************

TASK [Gathering Facts] ***********************************************************************
ok: [localhost]

TASK [Playbook dir stat] *********************************************************************
ok: [localhost]

TASK [Ensure Ansible is not being run in a world writable directory] *************************
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
..
[Cloud prompt]
What provider would you like to use?
    1. DigitalOcean
    2. Amazon Lightsail
    3. Amazon EC2
    4. Microsoft Azure
    5. Google Compute Engine
    6. Hetzner Cloud
    7. Vultr
    8. Scaleway
    9. OpenStack (DreamCompute optimised)
    10. CloudStack (Exoscale optimised)
    11. Linode
    12. Install to existing Ubuntu 18.04 or 20.04 server (for more advanced users)
  
Enter the number of your desired provider
:
12

允许 macOS/iOS 客户端在连接到蜂窝网络/Wi-Fi 时启用“VPN On Demand”。

[Cellular On Demand prompt]
Do you want macOS/iOS clients to enable "Connect On Demand" when connected to cellular networks?
[y/N]
:
y
[Wi-Fi On Demand prompt]
Do you want macOS/iOS clients to enable "Connect On Demand" when connected to Wi-Fi?
[y/N]
:
y

提供受信任的 WiFi 网络列表:

List the names of trusted Wi-Fi networks (if any) that macOS/iOS clients exclude from using the VPN
 (e.g., your home network. Comma-separated value, e.g., HomeNet,OfficeWifi,AlgoWiFi)
 :
HomeNet

进行其他几个配置,如下所示:

[Retain the PKI prompt]
Do you want to retain the keys (PKI)? (required to add users in the future, but less secure)
[y/N]
:
y
[DNS adblocking prompt]
Do you want to enable DNS ad blocking on this VPN server?
[y/N]
:
y
[SSH tunneling prompt]
 Do you want each user to have their own account for SSH tunneling?
 [y/N]
 :
 y
...
Local installation might break your server. Use at your own risk.

Proceed? Press ENTER to continue or CTRL+C and A to abort...:

提供您的公共 IP 以进行证书验证。

Enter the public IP address or domain name of your server: (IMPORTANT! This is used to verify the certificate)
[localhost]
:
Provide_Your_PublicIP/Domain_name

坐下来观看 Ansible 部署。部署完成后,您将看到:

现在,您将在 configs/IP_Address/wireguard/ 中创建连接配置文件。查看个人资料:

$ ls  configs/localhost/wireguard/
apple  linux.conf  linux.png  phone.conf  phone.png  windows.conf  windows.png

您还可以查看任何个人资料的内容:

$ cat configs/localhost/wireguard/linux.conf 
[Interface]
PrivateKey = qH7djC3t4njG6W2bxnSteNAUzyyuhpdQ21u+CHKjhXw=
Address = 10.49.0.3 
DNS =  172.28.237.162 

[Peer]
PublicKey = shbJpOr2Y2wNwzQyXwQJruac4JsYISlTefNC43l2RVY=
PresharedKey = fagxwHc1CaRAs80ii5Rx33THZDl56LqyAd388u/z7og=
AllowedIPs = 0.0.0.0/0,::/0
Endpoint = **********.compute-1.amazonaws.com:51820

配置文件包含设置 Wireguard 客户端所需的所有内容。 PNG 文件包含可以扫描以连接到 VPN 服务器的 QR 码。

如果您想在部署后添加用户,请编辑 config.cfg 并添加所需的用户。然后更新配置:

source env/bin/activate
./algo update-users

现在我们将验证该服务器上是否已设置 Wireguard:

sudo wg

示例输出:

#4。配置 Wireguard 客户端

设置 Wireguard 服务器后,我们可以配置 VPN 客户端以连接到它。要安装 Wireguard 客户端,请按照以下步骤操作:

4.1.在 Linux 和 macOS 上设置 WireGuard 客户端

可以通过 Wireguard 安装说明页面在 Linux 和 macOS 上设置 Wireguard 客户端。或者,使用包管理器安装 WireGuard 客户端。

在 Debian/Ubuntu 系统上,还安装以下软件包:

sudo apt install openresolv

安装后,创建配置文件:

sudo vim /etc/wireguard/wgvpn.conf

在该文件中,添加 Wireguaard 服务器上生成的配置文件中的行。例如来自 linux.conf

[Interface]
PrivateKey = qH7djC3t4njG6W2bxnSteNAUzyyuhpdQ21u+CHKjhXw=
Address = 10.49.0.3 
DNS =  172.28.237.162 

[Peer]
PublicKey = shbJpOr2Y2wNwzQyXwQJruac4JsYISlTefNC43l2RVY=
PresharedKey = fagxwHc1CaRAs80ii5Rx33THZDl56LqyAd388u/z7og=
AllowedIPs = 0.0.0.0/0,::/0
Endpoint = *********.compute-1.amazonaws.com:51820

保存文件并重新启动系统:

sudo reboot

系统重新启动后,使用以下命令启动 VPN 接口:

$ sudo wg-quick up wgvpn
[#] ip link add wgvpn type wireguard
[#] wg setconf wgvpn /dev/fd/63
[#] ip -4 address add 10.49.0.3 dev wgvpn
[#] ip link set mtu 1420 up dev wgvpn
[#] resolvconf -a wgvpn -m 0 -x
[#] wg set wgvpn fwmark 51820
[#] ip -6 route add ::/0 dev wgvpn table 51820
[#] ip -6 rule add not fwmark 51820 table 51820
[#] ip -6 rule add table main suppress_prefixlength 0
[#] nft -f /dev/fd/63
[#] ip -4 route add 0.0.0.0/0 dev wgvpn table 51820
[#] ip -4 rule add not fwmark 51820 table 51820
[#] ip -4 rule add table main suppress_prefixlength 0
[#] sysctl -q net.ipv4.conf.all.src_valid_mark=1
[#] nft -f /dev/fd/63

接口启动后,验证它是否正在运行:

sudo wg

示例输出:

4.2.在 Windows 上设置 WireGuard 客户端。

您还可以设置 Windows 客户端。首先,按照 WireGuard 官方页面上的说明安装 Wireguard 客户端。

安装后,启动客户端并单击添加空隧道。

粘贴来自 Wireguard 服务器的任何配置文件的内容。

保存文件并启动界面。

启动后,您将看到以下内容:

4.3.在移动设备上设置 WireGuard 客户端

您可以按照以下指南中提供的帮助在 Android 和 iOS 设备上设置 Wireguard 客户端:

  • 从 Linux 和 Android 设备连接到 Algo VPN 服务器

判决

今天,我们学习了如何使用 Algo VPN 在 Ubuntu 22.04 上设置 WireGuard 和 IPsec VPN 服务器。我们已经看到部署是多么容易,并且还演示了如何将客户端连接到服务器。我希望这对您很重要。

查看更多:

  • 使用 Ansible 部署 WireGuard 和 IPsec VPN 服务器
  • 如何在 Amazon Linux 2 上设置 WireGuard VPN
  • 在 Rocky/AlmaLinux 上安装 StrongSwan VPN 服务器
  • 如何设置和使用 ZeroTier VPN 服务器
©2015-2025 Norria support@norria.com