Ubuntu安装Wireguard操作说明

说明

	该笔记仅用于学习交流使用

环境说明

Ubuntu版本:4.15.0-43-generic #46~16.04.1-Ubuntu SMP Fri Dec 7 13:31:08 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Wireguard介绍

Wireguard官方链接地址:https://www.wireguard.com/
Wireguard源代码由开发者自我托管地址: https://git.zx2c4.com/WireGuard/
Wireguard GitHub 的镜像仓库:https://github.com/WireGuard
Wireguard支持系统:https://www.wireguard.com/install/

安装Wireguard

add-apt-repository ppa:wireguard/wireguard
apt-get update
apt-get install wireguard

配置Wireguard Server

**设置创建文件的默认权限**
		umask 077
**生成服务端Privatekey和Pubkey** 	 
		wg genkey | tee sprivatekey | wg pubkey > spublickey 
**生成客户端端Privatekey和Pubkey** 	 
		 wg genkey | tee cprivatekey | wg pubkey > cpublickey 
**开启服务端ubuntu的转发功能** 
		找到这一行 #net.ipv4.ip_forward = 1 去掉注释符 “#” 修改为
			net.ipv4.ip_forward = 1
		立即生效指令如下
			sysctl -p

创建Wireguard Server配置文件

vi /etc/wireguard/wg0.conf ,添加以下内容
[Interface]
	Address = 10.0.0.1/24
	SaveConfig = true
	PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o  ens3   -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o  ens3  -j MASQUERADE
	PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o  ens3  -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o  ens3  -j MASQUERADE
	ListenPort = 51820
	PrivateKey =   sprivatekey文件内容 
[Peer]
	PublicKey = cpublickey文件内容 
	AllowedIPs = 10.0.0.0/24

启动Wireguard Server

wg-quick up wg0

配置Wireguard Client

vi /etc/wireguard/wg0.conf ,添加以下内容
[Interface]
	ListenPort = 51821
	Address = 10.0.0.2
	PrivateKey =  cprivatekey文件内容 
	DNS = 8.8.8.8
[Peer]
	PublicKey =  spublickey文件内容                  
	AllowedIPs = 0.0.0.0/0
	Endpoint = 公网IP:端口号
	PersistentKeepalive = 25

启动Wireguard Client

wg-quick up wg0

Wireguard Client添加主机路由表

route add 公网IP dev ens3

启动之后使用wg指令查看,显示如下表示连接成功

在这里插入图片描述

注意事项

===》Wireguard支持的内核版本需要大于等于3.10
===》ens3为ubuntu的可上网网口
===》开启server端转发修改/etc/sysctl.conf文件,修改之后立即生效指令sysctl -p
			net.ipv4.ip_forward=1
			net.ipv6.conf.all.forwarding=1

猜你喜欢

转载自blog.csdn.net/wgl307293845/article/details/85854214