当前位置:首页 > RockyLinux > 正文

RockyLinux WireGuard配置(手把手教你搭建安全高效的VPN服务)

在当今网络环境中,保障通信安全变得越来越重要。WireGuard 是一款现代、快速、安全的虚拟私有网络(VPN)协议,以其简洁性和高性能广受好评。本文将详细讲解如何在 RockyLinux 系统上完成 WireGuard 配置,即使你是 Linux 新手,也能轻松上手。

一、什么是 WireGuard?

WireGuard 是一种开源的 VPN 协议,使用最先进的加密技术(如 Curve25519、ChaCha20、Poly1305 等),代码精简、运行高效,适用于各种设备和操作系统。相比 OpenVPN 或 IPsec,WireGuard 更加轻量且易于配置。

RockyLinux WireGuard配置(手把手教你搭建安全高效的VPN服务) WireGuard配置 WireGuard安装教程 RockyLinux搭建VPN WireGuard网络加密 第1张

二、准备工作

  • 一台已安装 RockyLinux 8/9 的服务器(建议最小化安装)
  • 具有 sudo 权限的用户账户
  • 确保防火墙允许 UDP 流量(默认端口为 51820)
  • 本地客户端设备(Windows / macOS / Android / iOS 等)

三、在 RockyLinux 上安装 WireGuard

首先,我们需要启用 EPEL 和 ELRepo 仓库,因为 WireGuard 内核模块不在默认仓库中。

1. 启用必要仓库

# 安装 EPEL 仓库sudo dnf install epel-release -y# 导入并启用 ELRepo 仓库(提供最新内核模块)sudo dnf install https://www.elrepo.org/elrepo-release-$(rpm -E %rhel).el$(rpm -E %rhel).elrepo.noarch.rpm -y# 安装 WireGuard 工具和内核模块sudo dnf install kmod-wireguard wireguard-tools -y  

2. 验证安装

运行以下命令检查是否成功安装:

wg --version  

如果输出类似 wg version 1.0.20210914,说明安装成功。

四、生成密钥对

WireGuard 使用公钥/私钥进行身份验证。我们需要为服务器和每个客户端分别生成密钥对。

# 创建配置目录mkdir -p /etc/wireguardcd /etc/wireguard# 生成服务器私钥和公钥umask 077wg genkey | tee privatekey | wg pubkey > publickey  

执行后,你会在 /etc/wireguard 目录下看到两个文件:privatekey(服务器私钥)和 publickey(服务器公钥)。

五、配置 WireGuard 服务端

创建主配置文件 /etc/wireguard/wg0.conf

[Interface]PrivateKey = <你的服务器私钥>Address = 10.0.0.1/24ListenPort = 51820PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEPostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE  

注意:请将 <你的服务器私钥> 替换为实际内容(可通过 cat privatekey 查看)。同时,请确认你的公网网卡名称是 eth0,如果不是(例如 ens192),请相应修改。

设置文件权限

sudo chmod 600 /etc/wireguard/wg0.conf  

六、启用 IP 转发与防火墙设置

1. 启用 IP 转发

echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.confsudo sysctl -p  

2. 配置防火墙(firewalld)

sudo firewall-cmd --permanent --add-port=51820/udpsudo firewall-cmd --permanent --add-masqueradesudo firewall-cmd --reload  

七、启动 WireGuard 服务

sudo systemctl enable wg-quick@wg0sudo systemctl start wg-quick@wg0  

验证服务状态:

sudo wg show  

八、配置客户端

以 Android 为例,在应用商店安装 “WireGuard” 应用,然后新建一个隧道,填入如下配置:

[Interface]PrivateKey = <客户端私钥>Address = 10.0.0.2/24DNS = 8.8.8.8[Peer]PublicKey = <服务器公钥>Endpoint = <你的服务器公网IP>:51820AllowedIPs = 0.0.0.0/0PersistentKeepalive = 25  

客户端私钥可使用以下命令生成(在任意 Linux 环境):

wg genkey | tee client_privatekey | wg pubkey > client_publickey  

将生成的 client_publickey 添加到服务器的 /etc/wireguard/wg0.conf 文件末尾(在 [Interface] 之后添加):

[Peer]PublicKey = <客户端公钥>AllowedIPs = 10.0.0.2/32  

修改后重启服务:

sudo systemctl restart wg-quick@wg0  

九、常见问题排查

  • 无法连接? 检查服务器防火墙是否放行 UDP 51820 端口。
  • 没有网络? 确认 IP 转发已启用,且 PostUp 规则中的网卡名正确。
  • 密钥错误? 公钥必须由对应私钥生成,不可手动编写。

十、总结

通过本教程,你已经成功在 RockyLinux 上完成了 WireGuard 配置,搭建了一个安全、高效的个人 VPN 服务。WireGuard 不仅性能优异,而且配置简单,非常适合家庭或小型企业使用。掌握 RockyLinux 搭建 VPN 技术后,你可以随时随地安全访问内网资源。

希望这篇 WireGuard 安装教程 对你有所帮助!如果你有任何疑问,欢迎在评论区留言交流。

关键词:RockyLinux WireGuard配置, WireGuard安装教程, RockyLinux搭建VPN, WireGuard网络加密