树莓派-搭建wireguard服务

一、简介

官网:https://www.wireguard.com/

WireGuard® is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN. WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike, fit for many different circumstances. Initially released for the Linux kernel, it is now cross-platform (Windows, macOS, BSD, iOS, Android) and widely deployable. It is currently under heavy development, but already it might be regarded as the most secure, easiest to use, and simplest VPN solution in the industry.
WireGuard是一个非常简单但利用最先进的加密技术快速和现代的VPN。它的目标是比IPsec更快、更简单、更精简和更有用,同时避免大量令人头痛的问题。它打算比OpenVPN性能更好。WireGuard被设计为一种通用的VPN,可以在嵌入式接口和超级计算机上运行,适合许多不同的情况。它最初是为Linux内核发布的,现在是跨平台的(Windows, macOS, BSD, iOS, Android),并且可以广泛部署。它目前正在大力开发,但它可能已经被认为是业界最安全、最容易使用和最简单的VPN解决方案。

内网穿透(frp)和虚拟专用网(wireguard):

  • 内网穿透(frp):需要公网IP,每个端设备需要基于公网IP和特定端口进行访问;
  • 虚拟专用网(wireguard):需要公网IP,每个端设备会被分配一个虚拟网IP,不同设备之间可以直接基于虚拟网IP进行访问。

二、服务器端部署

部署环境:阿里云(Ubuntu20.04)

1、安装wireguard
sudo apt install wireguard

注:解决"RTNETLINK answers: Operation not supported"问题

sudo apt install wireguard-dkms wireguard-tools linux-headers-$(uname -r)
2、修改系统配置
  • 开启IPV4流量转发
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
3、创建密钥对

Wireguard网络中每一个接入设备都一个对应密钥对(私钥由各端自己保留,公钥需要添加到服务端配置文件中)。

wg genkey | tee server.key | wg pubkey > server.key.pub
wg genkey | tee client_01.key | wg pubkey > client_01.key.pub
wg genkey | tee client_02.key | wg pubkey > client_02.key.pub
wg genkey | tee client_xx.key | wg pubkey > client_xx.key.pub

注:密钥对可以使用服务器创建再分配,也可以各个设备端使用相同的方法生成。

4、创建配置文件

创建配置文件:

touch /etc/wireguard/wg0.conf

修改配置文件:

  • [Interface]: 主要为服务端的配置
    • PrivateKey :服务端的私钥,客户端需要使用对应的公钥实现通信。(从安全角度该私钥需要严格保密)
    • Address:服务端分配的虚拟网的地址。
    • PostUp/PostDown:配置网络数据包进出设备及转发的控制,使用Linux iptables进行配置,需要注意根据实际环境修改命令中的物理网卡名(eth0)和虚拟网卡名(wg0)。
    • ListenPort:Wireguard服务端监听UDP端口,一般默认为9000。
    • MTU:最大传输单元MTU,是指网络能够传输的最大数据包大小,默认为1420。
  • [Peer]: 客户端部分的配置,每个Peer对应一个客户端设备
    • PublicKey:客户端设备的公钥
    • AllowedIPs :允许接入网络的客户端IP

配置文件示例:

[Interface]
PrivateKey = KB16EseuTq7Ax1zeuEob5PctLm/v3w7N+BoD28eAOks=
Address = 10.0.8.1/24

PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

ListenPort = 9000
MTU = 1420

[Peer]
PublicKey = cAHcJbh4Pl3CNeZ5/eZRP5RslpeiOCzgfFcKwkM0eUw=
AllowedIPs = 10.0.8.10/32

[Peer]
PublicKey = gOqWWD7dFEn2GoTfOYthvZgWX08Eoc8B1sVgUmBQUWo=
AllowedIPs = 10.0.8.11/32

[Peer]
PublicKey = 8LWZxQ4quXQ16jGS6vnv+uvqXN87altNCtsugrc9ons=
AllowedIPs = 10.0.8.12/32
5、服务管理

安装wireguard后,会创建wg-quick@.service文件,我们可以通过systemd对wireguard功能进行管理,服务名:wg-quick@<eth_name>, 以虚拟网卡wg0为例,对应的操作命令为:

# 启动服务
sudo systemctl start wg-quick@wg0
# 停止服务
sudo systemctl stop wg-quick@wg0
# 重启服务
sudo systemctl restart wg-quick@wg0
# 查看状态
sudo systemctl status wg-quick@wg0

# 使能自启动
sudo systemctl enable wg-quick@wg0
# 关闭自启动
sudo systemctl disable wg-quick@wg0

三、树莓派端部署

1、安装wireguard
sudo apt install wireguard
2、创建配置文件

创建配置文件:

touch /etc/wireguard/wg0.conf

配置文件示例:

[Interface]
PrivateKey = 0CxPRLJiY26DLS2+Rtu8JooBeLdKnY+VkrkvhiTRC3s=
Address = 10.0.8.13/24

[Peer]
PublicKey = WLAVpXsK5+BMYwfqbY/Bt7G14psHJE6TwMkXoZ7tJEo=
AllowedIPs = 10.0.8.0/24
Endpoint = 100.200.200.200:9000
PersistentKeepalive = 60
3、启动服务
# 启动服务
sudo systemctl start wg-quick@wg0
# 停止服务
sudo systemctl stop wg-quick@wg0
# 重启服务
sudo systemctl restart wg-quick@wg0
# 查看状态
sudo systemctl status wg-quick@wg0

# 使能自启动
sudo systemctl enable wg-quick@wg0
# 关闭自启动
sudo systemctl disable wg-quick@wg0

四、Windows/Android端部署

Wireguard处理Linux支持外,也开发了Windows/Android等设备端的应用,支持这类系统设备使用UI界面的方式配置连接到Wireguard网络。下载链接:https://www.wireguard.com/install/

配置项与Linux设备一致,Windows端支持“新建隧道->从文件导入隧道”的方式直接导入.conf文件建立隧道(连接)。
在这里插入图片描述
注:如果拦截所有流量走VPN,可以配置“AllowedIPs = 0.0.0.0/1, 128.0.0.0/1”。

移动端除了导入文件的方式外,也可以使用**“qrencode -t ansiutf8 <client_config>.conf”**创建二维码后扫描添加。
在这里插入图片描述

五、测试

  • 服务端状态查看
    客户端设备连接前执行“sudo wg”:
interface: wg0
  public key: iPU3HCbreBTMrpox+Ku6WU5xf5Rb3lEc9O9knGpNHVM=
  private key: (hidden)
  listening port: 9000

peer: DkqRdBMW9m6VvlfXEacRVajA3NLujXVtV9OvSEZo4RU=
  allowed ips: 10.0.8.10/32

peer: IOoa6sdwBMgno9rYMvsVZxbgvCfbfN/XEUBKw6Irjlg=
  allowed ips: 10.0.8.11/32

peer: 1Oruos/kMsVoy7lrf7SIy/2cVTNcH5VWJaccYpKtZz0=
  allowed ips: 10.0.8.12/32

peer: Qss20/1vcwm1Ir71vGxXsAq02l+zk1h4heBwpviq5Co=
  allowed ips: 10.0.8.13/32

客户端设备连接后执行“sudo wg”:

interface: wg0
  public key: iPU3HCbreBTMrpox+Ku6WU5xf5Rb3lEc9O9knGpNHVM=
  private key: (hidden)
  listening port: 9000

peer: Qss20/1vcwm1Ir71vGxXsAq02l+zk1h4heBwpviq5Co=
  endpoint: 222.70.222.174:50867
  allowed ips: 10.0.8.13/32
  latest handshake: 14 seconds ago
  transfer: 180 B received, 92 B sent

peer: DkqRdBMW9m6VvlfXEacRVajA3NLujXVtV9OvSEZo4RU=
  allowed ips: 10.0.8.10/32

peer: x/uU+r9+Z9WFgMYJBVyzi8swUUf6kOW1f9bYi7kv/A0=
  allowed ips: 10.0.8.11/32

peer: 1Oruos/kMsVoy7lrf7SIy/2cVTNcH5VWJaccYpKtZz0=
  allowed ips: 10.0.8.12/32
  • 检查设备间通信
PING 10.0.8.13 (10.0.8.13) 56(84) bytes of data.
64 bytes from 10.0.8.13: icmp_seq=1 ttl=64 time=116 ms
64 bytes from 10.0.8.13: icmp_seq=2 ttl=64 time=11.9 ms
64 bytes from 10.0.8.13: icmp_seq=3 ttl=64 time=11.5 ms
64 bytes from 10.0.8.13: icmp_seq=4 ttl=64 time=10.1 ms
64 bytes from 10.0.8.13: icmp_seq=5 ttl=64 time=11.2 ms
64 bytes from 10.0.8.13: icmp_seq=6 ttl=64 time=15.1 ms
64 bytes from 10.0.8.13: icmp_seq=7 ttl=64 time=10.2 ms
64 bytes from 10.0.8.13: icmp_seq=8 ttl=64 time=10.8 ms
64 bytes from 10.0.8.13: icmp_seq=9 ttl=64 time=10.8 ms
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

long7066

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值