在当今网络环境中,保障通信安全变得越来越重要。WireGuard 是一款现代、快速、安全的虚拟私有网络(VPN)协议,以其简洁性和高性能广受好评。本文将详细讲解如何在 RockyLinux 系统上完成 WireGuard 配置,即使你是 Linux 新手,也能轻松上手。
WireGuard 是一种开源的 VPN 协议,使用最先进的加密技术(如 Curve25519、ChaCha20、Poly1305 等),代码精简、运行高效,适用于各种设备和操作系统。相比 OpenVPN 或 IPsec,WireGuard 更加轻量且易于配置。
首先,我们需要启用 EPEL 和 ELRepo 仓库,因为 WireGuard 内核模块不在默认仓库中。
# 安装 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
运行以下命令检查是否成功安装:
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(服务器公钥)。
创建主配置文件 /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
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.confsudo sysctl -p
sudo firewall-cmd --permanent --add-port=51820/udpsudo firewall-cmd --permanent --add-masqueradesudo firewall-cmd --reload
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
通过本教程,你已经成功在 RockyLinux 上完成了 WireGuard 配置,搭建了一个安全、高效的个人 VPN 服务。WireGuard 不仅性能优异,而且配置简单,非常适合家庭或小型企业使用。掌握 RockyLinux 搭建 VPN 技术后,你可以随时随地安全访问内网资源。
希望这篇 WireGuard 安装教程 对你有所帮助!如果你有任何疑问,欢迎在评论区留言交流。
关键词:RockyLinux WireGuard配置, WireGuard安装教程, RockyLinux搭建VPN, WireGuard网络加密
本文由主机测评网于2025-12-17发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025129009.html