当前位置:首页 > 系统 > CentOS8安装WireGuard VPN

CentOS8安装WireGuard VPN

系统3年前 (2020-02-01)

CentOS8安装WireGuard VPN

WireGuard客户端和服务端基本是平等的,差别只是谁主动连接谁而已,通信双方都会监听一个UDP端口。双方都需要一对密钥,双方都需要把对方的公钥加进来。

谁主动连接,谁就是客户端,其实就是点对点VPN,新版本内核已经加入此VPN。


安装组件

#CentOS 8
dnf install -y epel-release
dnf config-manager --set-enabled PowerTools
dnf copr enable jdoss/wireguard -y
dnf install -y wireguard-dkms wireguard-tools
dnf install -y dnf-automatic
sed -i 's/apply_updates = no/apply_updates = yes/' /etc/dnf/automatic.conf
systemctl enable --now dnf-automatic.timer

#CentOS 7
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
curl -o /etc/yum.repos.d/jdoss-wireguard-epel-7.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
yum install wireguard-dkms wireguard-tools

CentOS8安装WireGuard VPN


开启IP转发

vim /etc/sysctl.conf
添加
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.ip_forward = 1
net.ipv4.tcp_syncookies = 1
执行sysctl -p

CentOS8安装WireGuard VPN


创建目录并生成密钥

mkdir /etc/wireguard && cd /etc/wireguard
bash -c 'umask 077; touch wg0.conf'
wg genkey | tee server_privatekey | wg pubkey > publickey
cat publickey
cat server_privatekey

CentOS8安装WireGuard VPN


配置虚拟网卡

vim /etc/wireguard/wg0.conf 
添加
[Interface]
Address = 192.168.2.1/24    #虚拟网卡地址
DNS = 8.8.8.8
MTU = 1420
ListenPort = 57000
SaveConfig = true
PrivateKey = GBu3qlu1ZbPVF4UZIVh9EmtVp7dB5Pe8gFvubg99H08=   #服务器私钥


开启服务

systemctl enable wg-quick@wg0.service
systemctl restart wg-quick@wg0.service

CentOS8安装WireGuard VPN

#如果CentOS7升级过内核到5.X版本,重启服务会报错RNETLINK answers: Operation not supported Unable to access interface: Protocol not supported是因为缺少内核头
uname -r
提示5.5.0-1.el7.elrepo.x86_64

dkms status
提示wireguard, 0.0.20200128: added

rpm -q kernel-headers
提示package kernel-headers is not installed

modprobe wireguard
提示modprobe: FATAL: Module wireguard not found.

#解决方法
新内核yum -y --enablerepo=elrepo-kernel install kernel-ml-{devel,headers,tools} perf
原内核yum install kernel-headers-$(uname -r) kernel-devel-$( uname -r) -y

reboot后问题解决


开启端口

firewall-cmd --zone=public --add-port=57000/udp --permanent
firewall-cmd --zone=public --add-masquerade --permanent
firewall-cmd --reload
firewall-cmd --list-all


Windows客户端配置

#下载客户端
https://www.wireguard.com/install/#windows-7-8-81-10-2012-2016-2019

安装后打开客户端,点击Add Tunnel--Add empty tunnel

CentOS8安装WireGuard VPN


CentOS8安装WireGuard VPN

复制客户端的公钥


服务端加入客户端

#wg set wg0 peer 客户端公钥 allowed-ips 客户端虚拟网卡地址
wg set wg0 peer t6BcJtl7iFVfbXcVa++Abn0wW1yU0Nn1WI5wdM/fJHc= allowed-ips 192.168.2.2/32

如果要删除

wg set wg0 peer t6BcJtl7iFVfbXcVa++Abn0wW1yU0Nn1WI5wdM/fJHc= remove


CentOS8安装WireGuard VPN

添加参数

[Interface]
Address = 客户端虚拟网卡地址/32
DNS = 8.8.8.8
PrivateKey = 客户端私钥
[Peer]
PublicKey = 服务端公钥
AllowedIPs = 0.0.0.0/0
Endpoint = 服务端外网地址:57000
PersistentKeepalive = 25


CentOS8安装WireGuard VPN

测试正常


CentOS8安装WireGuard VPN

服务端可以查看到客户端




本站所有文章均可随意转载,转载时请保留原文链接及作者。

本文链接:https://www.vos.cn/os/392.html

相关文章

解压出现gzip: stdin: not in gzip format

解压出现gzip: stdin: not in gzip format

今天解压quagga的tar包的时候一直提示gzip: stdin: not in gzip format的错误查看文件也是tar文件没问题最后试了下去掉z参数就好了tar -xvf&nbs...

修复升级EVE-NG后无法正常打开问题

修复升级EVE-NG后无法正常打开问题

升级流程Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.9.40-eve-ng-ukms+ x86_64) * Documentation:  ...

解决SecureCRT连接linux无配色

解决SecureCRT连接linux无配色

SecureCRT默认ssh连接linux是无配色的,看起来很难受左侧选中需要调整的会话名称->右键->Properties ->Session Options -> Term...

CentOS7安装cacti

CentOS7安装cacti

网上有一种叫cactiez的集成版本,rrd组件一直有问题,版本也太老,于是就想着重新搭建一个环境试试配置环境关闭Selinux vi /etc/selinux/config 将SELI...

CentOS7 iSCSI配置

CentOS7 iSCSI配置

先用5块磁盘组成RAID 6,其中4盘做RAID,1盘做热备,并在组建的阵列提供iSCSI服务yum install mdadm mdadm -Cv /dev...

CentOS6 安装Oracle11gR2

CentOS6 安装Oracle11gR2

使用oracle-rdbms-server-11gR2-preinstall工具,自动安装oracle所需的RPM包,自动创建oracle用户和dba、oinstall组,自动配置/etc/sysct...