Wireguard异地组网,实现任意网络【远程桌面连接】

网络结构拓扑
image-20241122142210013

1、wireguard异地组网部分

此部分内容请自行百度

需要注意的是:云服务器需要开启IP转发,否则远程桌面无法通过wireguard连接

启用 IP 转发:

sudo sysctl -w net.ipv4.ip_forward=1

配置 NAT 转发规则:

sudo firewall-cmd --zone=public --add-masquerade --permanent

重新加载防火墙

sudo firewall-cmd --reload

2、服务端配置

防火墙类型:使用 iptables

  1. 确保 iptables 服务正常运行:

    1. sudo systemctl status iptables
      
  2. 如果服务未启用或未运行,启用并启动服务

    1. sudo systemctl enable iptables
      sudo systemctl start iptables
      
  3. 添加 DNATMASQUERADE 规则并保存

    1. sudo iptables -t nat -A PREROUTING -p tcp --dport 33891 -j DNAT --to-destination 192.168.10.101:3389
      sudo iptables -t nat -A PREROUTING -p tcp --dport 33892 -j DNAT --to-destination 192.168.3.66:3389
      sudo iptables -t nat -A PREROUTING -p tcp --dport 33893 -j DNAT --to-destination 192.168.1.177:3389
      
      sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
      
      # 针对 WireGuard 内网网段伪装流量
      sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE
      sudo iptables -t nat -A POSTROUTING -s 192.168.3.0/24 -o eth0 -j MASQUERADE
      sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
      
      sudo iptables-save | sudo tee /etc/sysconfig/iptables > /dev/null
      
  4. 禁用 Firewalld(如果启用)

    1. 如果系统使用了 firewalld,它会覆盖 iptables 规则。可以禁用 firewalld 并改用 iptables

      sudo systemctl stop firewalld
      sudo systemctl disable firewalld
      sudo systemctl mask firewalld
      sudo systemctl enable iptables
      sudo systemctl start iptables
      
  5. 重启后检查规则是否生效

    # 重启命令
    sudo reboot
    # 等待30s后,重连服务端,运行查看规则配置是否生效
    sudo iptables -t nat -L -n -v
    
  6. 直接在任意环境下得电脑打开CMD,输入:mstsc 回车,打开远程桌面连接:公网IP+33891 连接 PC1

防火墙类型:使用 firewalld

  1. 启用 firewalld 并禁用 iptables

    1. 启动并启用 firewalld

      sudo systemctl start firewalld
      sudo systemctl enable firewalld
      

      如果在上述两个命令执行时出现:

      sudo systemctl start firewalld
      Failed to start firewalld.service: Unit is masked.

      那是因为屏蔽了:firewalld 服务

      # 解除屏蔽 firewalld 服务
      sudo systemctl unmask firewalld
      
    2. 禁用 iptables

      sudo systemctl stop iptables
      sudo systemctl disable iptables
      
  2. 配置端口转发(NAT规则)

    前面讲到的 iptables 中添加的端口转发规则,可以通过 firewalldrich rules 来实现。

    1. 添加端口转发规则

    2. # 将 33891 端口转发到 192.168.10.101:3389
      # 需要同时指定 toaddr(目标 IP 地址)和 toport(目标端口)
      
      sudo firewall-cmd --zone=public --add-forward-port=port=33891:proto=tcp:toaddr=192.168.10.101:toport=3389 --permanent
      
      sudo firewall-cmd --zone=public --add-forward-port=port=33892:proto=tcp:toaddr=192.168.3.66:toport=3389 --permanent
      
      sudo firewall-cmd --zone=public --add-forward-port=port=33893:proto=tcp:toaddr=192.168.1.177:toport=3389 --permanent
      
  3. 重启防火墙

    sudo firewall-cmd --reload
    
  4. 验证配置

    使用以下命令检查配置是否生效

    sudo firewall-cmd --zone=public --list-all
    

    你应该看到类似以下的转发规则:

    forward-ports: port=33891:proto=tcp:toaddr=192.168.10.101:toport=3389
                  port=33892:proto=tcp:toaddr=192.168.3.66:toport=3389
                  port=33893:proto=tcp:toaddr=192.168.1.177:toport=3389
    
  5. 这将确保从端口 338913389233892 到目标地址和端口(192.168.10.101:3389192.168.3.66:3389192.168.1.177:3389)的端口转发正确设置。

至此,应该就能使用任意电脑远程到服务器IP+对应端口连接到局域网得PC了

散会!

实现效果:

image-20241122153621622

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值