[i=s] 本帖最后由 fhy 于 2025-4-2 16:08 编辑 [/i]<br />
<br />
<p>现在有一个需求:飞牛主机192.168.1.94 里边运行Win虚拟机 192.168.122.73<br />
想访问宿主192.168.1.94 端口 3389 映射到虚拟机 192.168.122.73的3389端口<br />
首先虚拟机需要开启nat模式,可考论坛大牛帖子。<br />
操作过程:</p>
<pre><code class="language-bash"># 1. 开启内核IP转发功能(临时生效)
sudo sysctl -w net.ipv4.ip_forward=1
2. 添加NAT端口转发规则
sudo iptables -t nat -A PREROUTING -p tcp --dport 3389 -j DNAT --to-destination 192.168.122.73:3389
3. 允许转发流量
sudo iptables -I FORWARD -d 192.168.122.73/32 -p tcp --dport 3389 -j ACCEPT
4. 设置地址伪装(MASQUERADE)
sudo iptables -t nat -A POSTROUTING -s 192.168.122.0/24 -j MASQUERADE
5. 保存规则
sudo apt install iptables-persistent
sudo netfilter-persistent save
</code></pre>
<p>(非必需)永久启用IP转发(修改配置文件):</p>
<pre><code class="language-bash">echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
</code></pre> |