安装
Debian
echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable apt update apt install wireguard
Ubuntu
$ sudo add-apt-repository ppa:wireguard/wireguard $ sudo apt-get update $ sudo apt-get install wireguard
CentOS
$ sudo curl -Lo /etc/yum.repos.d/wireguard.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo $ sudo yum install epel-release $ sudo yum install wireguard-dkms wireguard-tools
其它
其它操作系统请参考:https://www.wireguard.com/install/#installation
配置
开启BBR(可选)
以CentOS-7为例:
添加镜像源
sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
安装最新内核RPM包
sudo yum --enablerepo=elrepo-kernel install kernel-ml -y
查看当前安装的内核包
rpm -qa | grep kernel
查看GRUB2的启动条目
sudo egrep ^menuentry /etc/grub2.cfg | cut -f 2 -d \'
配置对应的内核,0表示第一个内核
sudo grub2-set-default 1
重启系统,运行以下命令
echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf sudo sysctl -p
查看启用状态
lsmod | grep bbr
生成密钥
进入/etc/wiregurad目录
cd /etc/wiregurad
WireGuard需要base64编码的公钥和私钥,使用以下命令生成一个私钥:
wg genkey > privatekey
从私钥派生公钥:
wg pubkey <privatekey> publickey
命令一条龙:
wg genkey | tee privatekey | wg pubkey> publickey
此时目录中已经生成了公钥与私钥
# ls server_private.key server_public.key
生成主配置文件,使用wg0 虚拟接口名称
export vpn_net_pre='10.66.66' export vpn_ser_port=6443 cat >/etc/wireguard/wg0.conf<<EOF [Interface] Address = $vpn_net_pre.1/24 ListenPort = $vpn_ser_port PrivateKey = $(cat /etc/wireguard/server_private.key) PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE SaveConfig = true [Peer] AllowedIPs = $vpn_net_pre.22/24 PublicKey = glnmUF56rWRCJBGDpdejmf3d+vlQFz3icsLCbm8ubyE= PersistentKeepalive = 30 EOF
注意Peer 端的PublicKey 是对端的公钥。
参数说明
Address:Server虚拟网卡的IP地址
MTU:一般不用改,1500没问题
ListenPort:6443是监听的端口
PrivateKey:本机生成的私钥
PostUp:脚本启动后执行的iptables规则--主要SNAT规则。
PostDown:停止的时候自动删除iptables规则。
PublicKey:Client端的公钥
AllowedIPs:可以写多个,用逗号隔开,默认写Client端的IP
PersistentKeepalive:用来保持连接检查的,每过25s会自动检查连通性,如果IP有变化,也是通过这个自动更新endpoint。
SaveConfig: 自动保存配置,Wireguard在运行使用命令行添加节点时,自动将添加的节点保存至配置文件中。
启动服务
初始化并启动服务
wg-quick up wg0
停止服务
wg-quick down wg0
推荐使用systemd 来控制服务
systemctl start wg-quick@wg0 systemctl enable wg-quick@wg0 systemctl status wg-quick@wg0 ● wg-quick@wg0.service - WireGuard via wg-quick(8) for wg0 Loaded: loaded (/usr/lib/systemd/system/wg-quick@.service; disabled; vendor preset: disabled) Active: active (exited) since Sun 2019-01-24 00:22:16 CST; 9s ago Docs: man:wg-quick(8) man:wg(8) https://www.wireguard.com/ https://www.wireguard.com/quickstart/ https://git.zx2c4.com/WireGuard/about/src/tools/man/wg-quick.8 https://git.zx2c4.com/WireGuard/about/src/tools/man/wg.8 Process: 20514 ExecStart=/usr/bin/wg-quick up %i (code=exited, status=0/SUCCESS) …… [1]: Started WireGuard via wg-quick(8) for wg0. Hint: Some lines were ellipsized, use -l to show in full.
客户端
[Interface] Address = 10.66.66.2/32 PrivateKey = cLZw6eZR2LRUJZmlGjsBFGJrSrznw/Ag5VZILLZBbmY= DNS = 8.8.8.8 [Peer] AllowedIPs = 10.66.66.1/32, 39.108.115.xx/32, 114.114.114.114, 8.8.8.8 Endpoint = 47.90.100.xx:6443 PublicKey = glnmUF56rWRCJBGDpdejmf3d+vlQFz3icsLCbm8ubyE=
在Peer项的 AllowedIPs 填写上需要走VPN的IP段,使用0.0.0.0/0
表示全部的流量起VPN
客户端配置生成后,可以将在服务端使用以下命令行添加客户端点:
# wg set wg0 peer 客户端公钥证书内容 allowed-ips 10.66.66.2/32
启动后可以查看VPN状态
# wg interface: wg0 public key: 0jkKDBbKV5HbpGS50yCMuPS12EjZRYpFOt95ka0YllU= private key: (hidden) listening port: 40112 peer: glnmUF56rWRCJBGDpdejmf3d+vlQFz3icsLCbm8ubyE= endpoint: 47.90.100.xx:6443 allowed ips: 39.108.115.xx/32, 10.66.66.1/32
其它
注意事项
公钥和私钥一定要配置正确,否则不能正常通讯
AllowedIPs 为需要走VPN隧道的IP,Wireguard自动更改本机的route策略让它走VPN隧道,不要随意写0.0.0.0/0。
Server端一定要写ListenPort,且能正常访问。
Client端不需要写ListenPort,这个让他自己生成一个就可以了。
PersistentKeepalive 建立配置,这个可以让他保持通讯,如果IP发生变化,也可以及时更新
可以适当使用最新内核并启用bbr算法。
文献参考
[2] https://angristan.xyz/how-to-setup-vpn-server-wireguard-nat-ipv6/
[4] https://computingforgeeks.com/connecting-to-algo-vpn-server-from-linux-and-android-devices/
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://sg.hqyman.cn/post/9543.html 非本站原创文章欢迎转载,原创文章需保留本站地址!
休息一下~~