环境说明
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 eth0-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 eth0-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 = 10.0.0.2/24 Endpoint = 公网IP:端口号 PersistentKeepalive = 25
PS:一定注意AllowedIPs写客户端准备分配的IP,不要写0.0.0.0/0 不然所有流量都过去了,ssh都登录不上
启动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