• Debian 搭建 WireGuard 服务端


    CONTENTS

    1. 1. 准备工作
    2. 2. 安装 WireGuard
    3. 3. 配置服务端
    4. 4. 客户端配置
    5. 5. 服务端再配置
    6. 6. References

    本文以 Debian 10 为例,介绍如何搭建 WireGuard 服务端,并说明对应的客户端搭建方法和配置文件的格式。Ubuntu 20.04 系统与之大同小异。

    注:请先参照 Debian & Ubuntu 服务器的初始化配置 一文对服务器进行各种必要的配置。本文以 sammy 用户为例,进行 WireGuard 的部署,并默认已按初始化配置文章对服务器进行了配置。


    准备工作

    安装步骤所需软件包:

    1
    2
    
    sudo apt update
    sudo apt install apt-transport-https vim -y
    

    安装 WireGuard

    添加 backports 源:

    1
    
    sudo sh -c "echo 'deb https://deb.debian.org/debian buster-backports main contrib non-free' > /etc/apt/sources.list.d/buster-backports.list"
    

    安装软件包:

    1
    2
    
    sudo apt update
    sudo apt -t buster-backports install wireguard -y
    

    配置服务端

    切换到 root 用户:

    1
    
    sudo -i
    

    创建私钥、公钥:

    1
    2
    3
    
    cd /etc/wireguard
    umask 077
    wg genkey | tee privatekey | wg pubkey > publickey
    

    记录私钥、公钥:

    1
    2
    
    cat privatekey    # 服务端私钥
    cat publickey    # 服务端公钥
    

    创建配置文件,并添加内容:

    1
    2
    
    exit    # 退出 root 用户
    sudo vim /etc/wireguard/wg0.conf
    

    /etc/wireguard/wg0.conf

    1
    2
    3
    4
    5
    6
    7
    
    [Interface]
    Address = 10.0.0.1/24
    SaveConfig = true
    PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
    ListenPort = 自定义端口
    PrivateKey = 服务器私钥
    

    防火墙配置:

    1
    
    sudo ufw allow 自定义端口/udp
    

    启动服务:

    1
    2
    
    sudo systemctl enable wg-quick@wg0
    sudo systemctl start wg-quick@wg0
    

    查看服务状态:

    1
    
    sudo systemctl status wg-quick@wg0
    

    查看实际效果:

    1
    2
    
    sudo wg
    sudo ip a show wg0
    

    至此,服务端的配置大体完成,稍后还需要在客户端配置后,在服务端添加客户端的节点信息。

    客户端配置

    Debian 10 下客户端的安装流程、私钥公钥生成方法,和服务端的步骤类似,此处不再赘述。

    创建配置文件,并添加内容:

    1
    
    sudo vim /etc/wireguard/wg0.conf
    

    /etc/wireguard/wg0.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    [Interface]
    PrivateKey = 客户端私钥
    Address = 10.0.0.2/24
    DNS = 8.8.8.8
     
    [Peer]
    PublicKey = 服务端公钥
    AllowedIPs = 0.0.0.0/0
    Endpoint = 服务器 IP 地址:服务器自定义端口
    

    启动服务:

    1
    2
    
    sudo systemctl enable wg-quick@wg0
    sudo systemctl start wg-quick@wg0
    

    查看服务状态:

    1
    
    sudo systemctl status wg-quick@wg0
    

    服务端再配置

    在服务器上:

    1
    2
    
    sudo systemctl stop wg-quick@wg0
    sudo vim /etc/wireguard/wg0.conf
    

    增加 [Peer] 信息,修改后总体如下:

    /etc/wireguard/wg0.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    [Interface]
    Address = 10.0.0.1/24
    SaveConfig = true
    PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
    ListenPort = 服务端自定义端口
    PrivateKey = 服务端私钥
    
    [Peer]
    PublicKey = 客户端公钥
    AllowedIPs = 10.0.0.2/32
    

    启动服务:

    1
    
    sudo systemctl start wg-quick@wg0
    

    至此,服务端、客户端配置已完成。


  • 相关阅读:
    web网页设计期末课程大作业——汉中印象旅游景点介绍网页设计与实现19页面HTML+CSS+JavaScript
    vue重修004上部
    Java ClassNotFoundException异常解决指南
    动态规划——leetcode5、最长回文子串
    如何用 DDD 给 DDD 建模,破解 DDD 的魔法?
    数据结构——单链表的实现
    10年的老测试告诉你八大测试用例设计方法
    4. SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南
    微信支付后页面跳转
    代码随想录day38 || 动态规划理论基础 || 斐波那契数 || 爬楼梯 || 最小花费爬楼梯
  • 原文地址:https://blog.csdn.net/wdhqwe520/article/details/126065104