背景 为了方便访问内网和黑群晖NAS,不想直接通过端口转发直接暴露到公网。
TOP
PVE 我使用的是PVE的LXC容器环境,模板是Debian11代号bullseye。
CT模板源 1 2 3 4 #备份源文件 cp /usr/share/perl5/PVE/APLInfo.pm /usr/share/perl5/PVE/APLInfo.pm.back #修清华改源 sed -i 's|http://download.proxmox.com|https://mirrors.tuna.tsinghua.edu.cn/proxmox|g' /usr/share/perl5/PVE/APLInfo.pm
CT模板
创建CT 常规配置
选择下载的CT模板
给20G够了,不进行大文件的存储
CPU核心拉满
内存和交换空间各1GB
静态IPv4
DNS
允许root远程登陆 从PVE的控制台登录
1 2 #修改ssh配置 nano /etc/ssh/sshd_config
修改光标行
ctrl+x退出,按Y保存
回车,不对文件名进行修改
1 2 #重启服务 systemctl restart ssh
重启服务完成,可以使用ssh远程工具进行连接。
Debian源 更换中科大源
1 2 #配置/etc/apt/sources.list nano /etc/apt/sources.list
1 2 3 4 5 6 7 8 9 10 11 deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free deb-src https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free deb https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free deb https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free deb-src https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
1 2 3 4 #更新数据库 apt update #更新系统 apt upgrade -y
安装服务 通过apt安装服务
1 2 #wirguard服务本体 iptables防火墙,用来做动态伪装 net-tools网络命令工具 apt install wireguard iptables net-tools -y
基础配置 开启转发和密钥对创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #开启IP转发 echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf sysctl -p #进入配置目录 cd /etc/wireguard #建立服务端钥匙存放目录 mkdir server cd server #生成服务端的私钥 wg genkey > server.key #利用私钥生成公钥 wg pubkey < server.key > server.pub #查看服务端的私钥和公钥 cat server.key && cat server.pub #返回配置目录 cd /etc/wireguard #建立客户端1钥匙存放目录 mkdir client1 cd client1 #生成客户端的私钥 wg genkey > client1.key #利用私钥生成公钥 wg pubkey < client1.key > client1.pub #查看客户端的私钥和公钥 cat server.key && cat server.pub
服务端配置 配置虚拟wireguard网卡文件(/etc/wireguard/ 文件好像只能在这个目录下,systemctl创建的守护进程读取的是这个目录)
1 2 3 4 #返回配置目录 cd /etc/wireguard #配置wg0.conf文件(文件名就是虚拟网卡名称,wg0) nano wg0.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [Interface] #服务端私钥 PrivateKey = OEP7ukrA8dlxxxxdfqIxytmSHpxxxxSeXtDr3Tr4OG4= #服务端虚拟网卡地址 Address = 10.0.128.1/32 #服务端监听端口(UDP) ListenPort = 50814 #开启虚拟网卡转发数据包,对去往内网网段eth0的数据包使用IP伪装(SNAT) PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE #添加客户端 [Peer] #客户端client1的公钥 PublicKey = ZBxt5ArYwhsgxxxxrtikLp/gPhxxxxSthRe8hwx0nUyU= #客户端client1使用的ip AllowedIPs = 10.0.128.10/32
1 2 3 4 5 6 7 8 9 10 [Interface] PrivateKey = OEP7ukrA8dlxxxxdfqIxytmSHpxxxxSeXtDr3Tr4OG4= Address = 10.0.128.1/32 ListenPort = 50814 PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE [Peer] PublicKey = ZBxt5ArYwhsgxxxxrtikLp/gPhxxxxSthRe8hwx0nUyU= AllowedIPs = 10.0.128.10/32
启动服务端 利用systemctl创建守护进程
1 2 3 4 #返回配置目录 cd /etc/wireguard #开机自启和开启服务 systemctl enable --now wg-quick@wg0
启动成功,wg0就是wireguard的虚拟网卡。
客户端配置 生成客户端需要的conf
1 2 3 4 #进入客户端存放目录 cd /etc/wireguard/client1 #配置client1.conf文件 nano client1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [Interface] #客户端私钥 PrivateKey = QKZEuxC4W0QMPUJ/mxxxxXK48K9LxxxxQKzy/58o4aW3E= #客户端使用的IP(服务端设置的) Address = 10.0.128.10/32 #使用内网的dns解析(可以不设置) DNS = 192.168.1.130 [Peer] #服务端公钥 PublicKey = Q/HcK8M/xOU6xxxx6orG24GOwasvyxtwCOfOqIj5fTQ= #转发的IP网段,差不多就是路由表(这里设置了建立连接的网段和内网网段) AllowedIPs = 10.0.128.0/24,192.168.1.0/24 #服务端的IP和端口(我使用的是域名解析,配合ddns食用) Endpoint = xx.xx.xx:50814
1 2 3 4 5 6 7 8 9 [Interface] PrivateKey = QKZEuxC4W0QMPUJ/mxxxxXK48K9LxxxxQKzy/58o4aW3E= Address = 10.0.128.10/32 DNS = 192.168.1.130 [Peer] PublicKey = Q/HcK8M/xOU6xxxx6orG24GOwasvyxtwCOfOqIj5fTQ= AllowedIPs = 10.0.128.0/24,192.168.1.0/24 Endpoint = xx.xx.xx:50814
端口转发 使用OpenWrt进行端口映射
客户端 下载 官网
Win 使用手机热点上网,模拟异地连入家庭内网
导入client1的conf
连接
SMB测试 从本地复制到内网
Android 使用数据流量上网,模拟异地连入家庭内网
连接 导入客户端conf进行连接
访问内网 openwrt
黑群晖WEB
Arch 使用手机热点上网,模拟异地连入家庭内网
安装服务 使用arcrh的pacman管理器进行安装
1 2 #wireguard-tools 包含了服务本体 sudo pacman -S wireguard-tools
配置虚拟网卡 客户端配置虚拟网卡文件
1 2 3 4 5 6 7 #切换root身份,方便操作 sudo -i #进入wireguard配置目录 cd /etc/wireguard/ #下载服务端的client1 #查看client1.conf cat client1.conf
启动虚拟网卡 使用wireguard的命令进行启动,因为,不需要一直连接,所以不采用守护进程。
1 2 3 4 5 6 #通过wireguard的命令进行启动 wg-quick up client1 #通过wireguard的命令进行关闭 wg-quick down client1 #使用ifconfig查看网卡 ifconfig
1 2 #ping服务端测试 ping 10.0.128.1
Docker 使用Docker Hub的weejewel/wg-easy的环境,网页端提供创建客户,支持二维码和下载配置文件。
docker-cli 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 docker run -d \ --name=wg-easy \ #这里输入服务器的公网IP或者域名 -e WG_HOST=123.123.123.123 \ #这里输入你的密码web密码 -e PASSWORD=passwd123 \ #默认IP地址段 -e WG_DEFAULT_ADDRESS=10.0.128.x \ #默认DNS -e WG_DEFAULT_DNS=192.168.1.130 \ #允许连接的IP段,转发的路由 -e WG_ALLOWED_IPS=10.0.128.0/24,192.168.1.0/24 \ #重新连接间隔 -e WG_PERSISTENT_KEEPALIVE=25 \ -v /root/Docker/wg-easy:/etc/wireguard \ #隧道接口,需要进行转发 -p 51820:51820/udp \ #web接口 -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
docker-compose docker-compose.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 version: '3.3' services: wg-easy: container_name: wg-easy environment: - WG_HOST=123.123.123.123 - PASSWORD=passwd123 - WG_DEFAULT_ADDRESS=10.0.128.x - WG_DEFAULT_DNS=192.168.1.130 - WG_ALLOWED_IPS=10.0.128.0/24,192.168.1.0/24 - WG_PERSISTENT_KEEPALIVE=2 volumes: - './wg-easy:/etc/wireguard' ports: - 51820:51820/udp - 51821:51821/tcp sysctls: - net.ipv4.conf.all.src_valid_mark=1 - net.ipv4.ip_forward=1 cap_add: - NET_ADMIN - SYS_MODULE restart: unless-stopped image: weejewel/wg-easy
1 2 #启动 docker compose up -d
端口转发 使用OpenWrt进行端口映射,仅转发隧道端口,防止web暴露在公网被爆破。
使用 web界面
新建客户端
支持二维码扫描和下载客户端配置文件