Linux系统下安装并使用WireGuard

环境要求

  • 一台 Windows 或 Linux 设备充当服务器
  • 多台 Windows 或 Linux 或 Mac 或 Android 或 iOS 设备充当客户端
  • 作为 WireGuard 服务器的设备需要有公网 IP 或者能被客户端访问

1. 安装 WireGuard

首先,在你的 Linux 系统上安装 WireGuard。你可以通过 apt、dnf、yum 或其他包管理器进行安装,具体取决于你的发行版。

1
2
sudo apt update
sudo apt install wireguard

启用 EPEL 仓库并安装 WireGuard:

1
2
sudo yum install epel-release
sudo yum install wireguard-tools

如果你的 CentOS/RHEL 系统使用的是 8 及以上版本,你可能需要额外的仓库支持:

1
sudo dnf install wireguard-tools
1
sudo dnf install wireguard-tools
1
sudo pacman -S wireguard-tools
手动下载Windows安装包

2. 生成密钥对

WireGuard 使用公钥和私钥来进行加密通信。你可以通过以下命令生成密钥对:

1
wg genkey | tee privatekey | wg pubkey > publickey
密钥文件说明

这将在当前目录下生成 privatekeypublickey 文件。

  • 私钥:为机密,不可共享给他人
  • 公钥:可以共享给其他 WireGuard 节点

3. 配置 WireGuard

/etc/wireguard/ 目录下创建一个配置文件(例如 wg0.conf)。

1
sudo vim /etc/wireguard/wg0.conf
1
2
3
4
5
6
7
8
9
[Interface]
PrivateKey = <客户端私钥> # 修改时请将<>一并删去
Address = *.*.*.*/* # 客户端的虚拟IP地址,服务器分配的ip

[Peer]
PublicKey = <服务器公钥>
Endpoint = <服务器IP地址>:51820 # 端口为服务器设置的端口,51820为默认 WireGuard 端口
AllowedIPs = *.*.*.*/* # 允许通过 VPN 网络的对端地址
PersistentKeepalive = 25 # 保持连接的频率
  • [Interface] 部分包含本机配置。你需要用你自己的私钥替换 <你的私钥>,并设置适当的 IP 地址。
  • [Peer] 部分是对端的配置,<对端公钥> 是对方的公钥,AllowedIPs 是允许的 IP 范围。
1
2
3
4
5
6
7
8
9
[Interface]
Address = *.*.*.*/* # 这是服务器的虚拟IP地址
ListenPort = 51820 # WireGuard 默认端口
PrivateKey = <服务器私钥> # 服务器的私钥

[Peer]
PublicKey = <客户端公钥> # 连接的客户端公钥
AllowedIPs = *.*.*.*/.* # 客户端的虚拟IP地址
PersistentKeepalive = 25 # 保持连接的频率
  • Address:为服务器分配一个 IP 地址(通常选择 10.0.0.1/24)。
  • ListenPort:设置 WireGuard 监听的端口(默认是 51820)。
  • PrivateKey:填入你为服务器生成的私钥。
  • [Peer] 部分是客户端的配置,公钥需要在客户端生成并提供给服务器。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Interface]
Address = *.*.*.*/* # 这是服务器的虚拟IP地址
ListenPort = 51820 # WireGuard 默认端口
PrivateKey = <服务器私钥> # 服务器的私钥

[Peer]
#D1
PublicKey = <客户端1公钥> # 连接的客户端公钥
AllowedIPs = *.*.*.*/* # 客户端的虚拟IP地址
PersistentKeepalive = 25 # 保持连接的频率

[Peer]
#D2
PublicKey = <客户端2公钥> # 连接的客户端公钥
AllowedIPs = *.*.*.*/* # 客户端的虚拟IP地址
PersistentKeepalive = 25 # 保持连接的频率

如果有多个客户端,可以在 [Peer] 部分添加多个 PublicKeyAllowedIPs 条目。

4. 启动 WireGuard 服务

使用以下命令启动 WireGuard 接口:

1
sudo wg-quick up wg0

这将启用 wg0 配置文件中的设置并启动 VPN 服务。

5. 配置自动启动

为了让 WireGuard 在系统启动时自动启动,使用以下命令启用服务:

1
sudo systemctl enable wg-quick@wg0

6. 配置防火墙

确保防火墙允许 WireGuard 的 UDP 端口(默认是 51820)

1
2
sudo ufw allow 51820/udp
sudo ufw enable
1
sudo iptables -A INPUT -p udp --dport 51820 -j ACCEPT

如果你使用 iptables,确保保存规则,否则它们会在重启后丢失。

7. 启用 IP 转发(仅服务器)

如果你希望通过 VPN 连接进行路由,需启用 IP 转发:

打开 /etc/sysctl.conf 文件:

1
sudo vim /etc/sysctl.conf

添加以下行以启用 IPv4 和 IPv6 转发:

1
2
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1

使更改生效:

1
sudo sysctl -p

8. 配置 NAT(仅服务器)

如果你想通过 VPN 为客户端提供 Internet 访问,还需要配置 NAT。假设你使用的是 eth0 网络接口(根据实际网络接口名称调整):

使用 iptables 配置 NAT:

1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

保存 iptables 规则(根据你的 Linux 发行版选择适当方法):

1
sudo iptables-save > /etc/iptables/rules.v4
1
sudo service iptables save

9. 测试连接

确保客户端可以成功连接到服务器

使用 ping 命令测试:(从客户端 ping 服务器的虚拟 IP)

1
ping *.*.*.*

文中未提及的常用命令

  • 查看 WireGuard 接口状态:
1
sudo wg
  • 启动 WireGuard 接口:
1
sudo wg-quick up wg0
  • 停止 WireGuard 接口:
1
sudo wg-quick down wg0
  • 查看接口的传输数据:
1
sudo wg show wg0
Icon倘若帮到您,赏根猫条呗~ 嘿嘿
QRCode微信赞赏
QRCode支付宝
本作品由 Pavilion_Cat 于 2024-12-11 17:03:39 发布
作品地址:Linux系统下安装并使用WireGuard
除特别声明外,本站作品均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 Pavilion_Cat's Blog
Logo