跳转到主要内容
你行网

Main navigation

  • 首页
  • 文章
  • 教程
  • 视频
  • 下载
  • 社区
  • 网站导航
User account menu
  • 登录

面包屑

  1. 首页

WireGuard 使用命令

由 hrs, 3 三月, 2025

1. 启动 WireGuard 接口
使用 wg-quick 工具启动配置文件中的 WireGuard 接口:

bash
复制
sudo wg-quick up <配置文件名称>
例如:

bash
复制
sudo wg-quick up wg0
配置文件通常位于 /etc/wireguard/ 目录下,例如 /etc/wireguard/wg0.conf。

启动后,WireGuard 会自动配置网络接口、路由和 DNS。

2. 停止 WireGuard 接口
停止正在运行的 WireGuard 接口:

bash
复制
sudo wg-quick down <配置文件名称>
例如:

bash
复制
sudo wg-quick down wg0
3. 查看 WireGuard 接口状态
使用 wg 命令查看当前 WireGuard 接口的状态和对等节点信息:

bash
复制
sudo wg
输出示例:

复制
interface: wg0
 public key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 private key: (hidden)
 listening port: 51820

peer: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
 endpoint: 203.0.113.1:51820
 allowed ips: 0.0.0.0/0
 latest handshake: 1 minute ago
 transfer: 1.23 MiB received, 456.78 MiB sent
4. 生成密钥对
WireGuard 使用公钥和私钥进行加密通信。可以使用以下命令生成密钥对:

生成私钥:

bash
复制
wg genkey
从私钥生成公钥:

bash
复制
echo "<私钥>" | wg pubkey
例如:

bash
复制
wg genkey | tee privatekey | wg pubkey > publickey
privatekey 文件保存私钥。

publickey 文件保存公钥。

5. 手动添加对等节点
使用 wg set 命令手动添加对等节点(Peer):

bash
复制
sudo wg set <接口名称> peer <对等节点公钥> allowed-ips <允许的IP范围> endpoint <对等节点地址:端口>
例如:

bash
复制
sudo wg set wg0 peer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx allowed-ips 10.0.0.2/32 endpoint 203.0.113.1:51820
6. 删除对等节点
使用 wg set 命令删除对等节点:

bash
复制
sudo wg set <接口名称> peer <对等节点公钥> remove
例如:

bash
复制
sudo wg set wg0 peer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx remove
7. 修改 WireGuard 配置
WireGuard 的配置文件通常位于 /etc/wireguard/ 目录下。你可以直接编辑配置文件:

bash
复制
sudo nano /etc/wireguard/wg0.conf
配置文件示例:

ini
复制
[Interface]
PrivateKey = <你的私钥>
Address = 10.0.0.1/24
ListenPort = 51820
DNS = 8.8.8.8

[Peer]
PublicKey = <对等节点公钥>
AllowedIPs = 10.0.0.2/32
Endpoint = 203.0.113.1:51820
修改后,重新加载配置:

bash
复制
sudo wg-quick down wg0
sudo wg-quick up wg0
8. 查看网络接口信息
使用 ip 命令查看 WireGuard 接口的网络信息:

bash
复制
ip addr show <接口名称>
例如:

bash
复制
ip addr show wg0
9. 查看路由表
WireGuard 会自动添加路由规则。你可以使用以下命令查看路由表:

bash
复制
ip route
或者查看特定接口的路由:

bash
复制
ip route show dev wg0
10. 启用持久化配置
WireGuard 的配置在重启后不会自动保存。如果需要持久化配置,可以将配置写入配置文件,并使用 systemd 启用服务:

bash
复制
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
11. 调试 WireGuard
如果遇到问题,可以使用以下命令调试:

查看日志:

bash
复制
sudo journalctl -u wg-quick@wg0
使用调试模式启动:

bash
复制
sudo WG_QUICK_USERSPACE_IMPLEMENTATION=boringtun WG_SUDO=1 wg-quick up wg0
12. 卸载 WireGuard
如果需要卸载 WireGuard,可以使用以下命令:

bash
复制
sudo apt remove wireguard
sudo apt autoremove

标签

  • WireGuard
  • 登录或注册以发表评论

评论

相关文章

文章标签

CSS
Excel
ubuntu
php
五笔
vim
Linux
HTML
command
drupal 7
composer
centos
Mysql
drupal 8
模块(module)
VPN
drush
JavaSrcipt
apache
drupal 9
英语
Drupal
SEO
nerdtree
network
果树种植
surround
drupal10
主题(theme)
phpmyadmin
ssh
权限
yum
二进制
声音
ssl
selinux
HTML5
用户
Dreamweaver
password
flameshot
颜色
翻译
uploadprogress
DDEV
视图(views)
firewalld
kernel
gzip
快捷键
xdebug
错误
ckeditor
mbstring
C语言
简介
源地址
php扩展
电脑基础
certbot
git
apt
压缩解压
表单
电子秤
WireGuard
httpd
邮件验证
breadcrumb
AI
superfish
diff
重定向
网站备份
摄影
tagclouds
docker
java
csharp
短信验证
adsense
drupal9
电脑技巧
simple_adsense
算法
RSS
youtube
版本号
JavaScript
badblocks
端口
cookie
VUE
网站运营
计算机基础
gimp
form
mod_expires 模块
zip
nginx
http验证
重定向,301
curl
netstat
input
赚钱
ip
dns
RFID
标签优化
drupal11
maven
drupal 11
npm
哈希值
字段
更多
RSS源
本站由阿里云强力驱动,幸运券优惠链接
Copyright © 2019 - 2021 你行网 版权所有  粤ICP备19072650号