如何在 Debian 12 上安装 WireGuard

2023年 10月 14日 18.9k 0

在日益关注在线安全和隐私的时代,建立虚拟专用网络(VPN)已成为最重要的需求。WireGuard 是一种现代且高效的 VPN 协议,为互联网上的安全通信提供了简化的解决方案。

Wireguard-logo

在 Debian 12 书虫上安装 WireGuard

第 1 步。在我们安装任何软件之前,通过在终端中运行以下命令来确保您的系统是最新的非常重要:apt

sudo apt update
sudo apt upgrade

此命令将刷新存储库,允许您安装最新版本的软件包。

第 2 步。安装依赖项。

可以使用以下命令安装这些依赖项:

sudo apt install linux-headers-$(uname -r) wget

第 3 步。在 Debian 12 上安装 WireGuard。

WireGuard 不包含在默认的 Debian 12 存储库中。我们需要添加 WireGuard 存储库来访问所需的包:

sudo add-apt-repository ppa:wireguard/wireguard

现在我们已经添加了存储库,请更新包列表以包含 WireGuard:

sudo apt update

让我们安装 WireGuard 并加载内核模块:

sudo apt install wireguard
sudo modprobe wireguard

要验证是否正确安装了 WireGuard,让我们检查模块的状态并确保 WireGuard 工具可用:

# Verify the WireGuard module is loaded
lsmod | grep wireguard

# Check if WireGuard tools are installed
wg --version

第 4 步。配置线卫

安装WireGuard后,让我们逐步配置它。

  • 生成密钥对
  • WireGuard 使用密钥对进行加密和身份验证。我们需要为服务器和客户端生成密钥对:

    服务器密钥对:

    # Generate the server's private key
    wg genkey > server-private.key
    
    # Derive the server's public key from the private key
    wg pubkey  server-public.key

    客户端密钥对:

    # Generate the client's private key
    wg genkey > client-private.key
    
    # Derive the client's public key from the private key
    wg pubkey  client-public.key
  • 配置服务器
  • 为 WireGuard 服务器创建配置文件。替换为服务器的公共 IP 地址:

    sudo nano /etc/wireguard/wg0.conf

    添加以下配置,将 替换为之前生成的实际密钥:

    [Interface]
    Address = 10.0.0.1/24
    ListenPort = 51820
    PrivateKey = 
    
    [Peer]
    PublicKey = 
    AllowedIPs = 10.0.0.2/32

    启用 IP 转发以允许流量通过服务器:

    # Enable IP forwarding
    echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
    sudo sysctl -p

    设置防火墙规则以允许 WireGuard 流量:

    # Allow WireGuard through the firewall
    sudo iptables -A INPUT -i wg0 -j ACCEPT
    sudo iptables -A FORWARD -i wg0 -j ACCEPT
    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  • 配置客户端
  • 为 WireGuard 客户端创建配置文件。替换为服务器的公钥:

    nano client.conf

    添加以下配置:

    [Interface]
    PrivateKey = 
    Address = 10.0.0.2/24
    DNS = 8.8.8.8
    
    [Peer]
    PublicKey = 
    Endpoint = :51820
    AllowedIPs = 0.0.0.0/0
  • 启动 WireGuard 接口
  • 让我们启动服务器和客户端的 WireGuard 接口:

    # Start the server interface
    sudo wg-quick up /etc/wireguard/wg0.conf
    
    # Start the client interface
    sudo wg-quick up ./client.conf
  • 检查 WireGuard 接口的状态
  • 为确保一切顺利运行,请检查 WireGuard 接口的状态:

    # Check the server interface
    sudo wg show
    
    # Check the client interface
    wg show client

    第5步。测试WireGuard VPN。

    现在配置了 WireGuard,让我们在服务器和客户端之间建立连接:

    sudo wg-quick up ./client.conf

    要确认 VPN 是否正常工作,请尝试从客户端 ping 服务器,反之亦然:

    在客户端上:

    ping 10.0.0.1

    在服务器上:

    ping 10.0.0.2

    通过从客户端访问 WhatIsMyIP.com 等网站来确保您的真实 IP 地址被隐藏。

    感謝您使用本教鬥在 Debian 12 Bookworm 上安裝最新版本的 WireGuard。有关其他帮助或有用信息,我们建议您查看官方 WireGuard 网站。

    相关文章

    服务器端口转发,带你了解服务器端口转发
    服务器开放端口,服务器开放端口的步骤
    产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
    如何使用 WinGet 下载 Microsoft Store 应用
    百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
    百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

    发布评论