基于Wireguard技术的虚拟个人网络搭建
发布于 2022-12-04
内容引用&参考youtube视频
Section titled “内容引用&参考youtube视频”https://www.youtube.com/watch?v=rj1yxHQj8G0
手动安装Wireguard
Section titled “手动安装Wireguard”安装Wireguard(以ubuntu20.04为基础)
Section titled “安装Wireguard(以ubuntu20.04为基础)”#root权限sudo -i
#安装wireguard软件apt install wireguard resolvconf -y
#开启IP转发echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.confsysctl -p进入配置存储路径,调整目录权限
Section titled “进入配置存储路径,调整目录权限”cd /etc/wireguard/chmod 0777 /etc/wireguard
#调整目录默认权限umask 077生成服务器秘钥
Section titled “生成服务器秘钥”#生成私钥wg genkey > server.key
#通过私钥生成公钥wg pubkey < server.key > server.key.pub生成客户端(client1)秘钥
Section titled “生成客户端(client1)秘钥”#生成私钥wg genkey > client1.key
#通过私钥生成公钥wg pubkey < client1.key > client1.key.pub显示所有生成的秘钥
Section titled “显示所有生成的秘钥”cat server.key && cat server.key.pub && cat client1.key && cat client1.key.pub自动创建服务器配置文件
Section titled “自动创建服务器配置文件”echo "[Interface]PrivateKey = $(cat server.key) # 填写本机的privatekey 内容Address = 10.0.8.1 #本机虚拟局域网IP
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEPostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE#注意eth0需要为本机网卡名称
ListenPort = 50814 # 监听端口DNS = 8.8.8.8MTU = 1420[Peer]PublicKey = $(cat client1.key.pub) #自动client1的公钥AllowedIPs = 10.0.8.10/32 #客户端所使用的IP" > wg0.conf设置服务器开机自启动
Section titled “设置服务器开机自启动”systemctl enable wg-quick@wg0启动wireguard
Section titled “启动wireguard”#启动wg0wg-quick up wg0#关闭wg0wg-quick down wg0手动创建服务器配置文件(待完成)
Section titled “手动创建服务器配置文件(待完成)”nano /etc/wireguard/wg0.confwireguard客户端下载地址
Section titled “wireguard客户端下载地址”https://www.wireguard.com/install/客户端配置(以client1为例)
Section titled “客户端配置(以client1为例)”[Interface]PrivateKey = 6M8HEZioew+vR3i53sPc64Vg40YsuMzh4vI1Lkc88Xo= #此处为client1的私钥Address = 10.0.8.10 #此处为peer规定的客户端IPMTU = 1500
[Peer]PublicKey = Tt5WEa0Vycf4F+TTjR2TAHDfa2onhh+tY8YOIT3cKjI= #此处为server的公钥AllowedIPs = 10.0.8.0/24 #此处为允许的服务器IPEndpoint = 114.132.56.178:50814 #服务器对端IP+端口增加服务器客户端节点client2
Section titled “增加服务器客户端节点client2”#生成私钥wg genkey > client2.key
#通过私钥生成公钥wg pubkey < client2.key > client2.key.pub
#将peer公钥加入wg0.conf配置echo "[Peer]PublicKey = $(cat client2.key.pub) #自动client1的公钥AllowedIPs = 10.0.8.11/32 #客户端Client2所使用的IP" >> wg0.confDocker安装Wireguard(推荐,一键配置,极度舒适)
Section titled “Docker安装Wireguard(推荐,一键配置,极度舒适)”通过容器安装wg-easy
Section titled “通过容器安装wg-easy”docker run -d \ --name=wg-easy \ -e WG_HOST=123.123.123.123 (🚨这里输入服务器的公网IP) \ -e PASSWORD=passwd123 (🚨这里输入你的密码) \ -e WG_DEFAULT_ADDRESS=10.0.8.x (🚨默认IP地址)\ -e WG_DEFAULT_DNS=114.114.114.114 (🚨默认DNS)\ -e WG_ALLOWED_IPS=10.0.8.0/24 (🚨允许连接的IP段)\ -e WG_PERSISTENT_KEEPALIVE=25 (🚨重连间隔)\ -v ~/.wg-easy:/etc/wireguard \ -p 51820:51820/udp \ -p 51821:51821/tcp \ --cap-add=NET_ADMIN \ --cap-add=SYS_MODULE \ --sysctl="net.ipv4.conf.all.src_valid_mark=1" \ --sysctl="net.ipv4.ip_forward=1" \ --restart unless-stopped \ weejewel/wg-easy更新容器命令
Section titled “更新容器命令”docker stop wg-easydocker rm wg-easydocker pull weejewel/wg-easyCentos7安装
Section titled “Centos7安装”- Method1
$ sudo yum install yum-utils epel-release$ sudo yum-config-manager --setopt=centosplus.includepkgs=kernel-plus --enablerepo=centosplus --save$ sudo sed -e 's/^DEFAULTKERNEL=kernel$/DEFAULTKERNEL=kernel-plus/' -i /etc/sysconfig/kernel$ sudo yum install kernel-plus wireguard-tools$ sudo reboot- Method2
$ sudo yum install epel-release elrepo-release$ sudo yum install yum-plugin-elrepo$ sudo yum install kmod-wireguard wireguard-tools- Method3
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm$ sudo 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$ sudo yum install wireguard-dkms wireguard-tools- 设置为服务自动启动
systemctl enable wg-quick@wg0 --now 发布于 2022-12-04