• 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
    

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


  • 相关阅读:
    论文阅读 (71):Optimal Margin Distribution Machine for Multi-Instance Learning
    数据治理-数据仓库和商务智能
    高并发系统架构设计之实战篇:消息流设计之拉模式
    django基于pythonweb的大学生兼职系统设计与分析
    Java 异常处理、继承、重写/重载
    干货!英语常用口语1000句大全(完整版)!
    程序员的七夕浪漫时刻
    技术学习:Python(18)|爬虫篇|解析器BeautifulSoup4(一)
    复习十:栈与递归的实现
    深圳高级中学国际课程体系IBDP成绩
  • 原文地址:https://blog.csdn.net/wdhqwe520/article/details/126065104