KVM 端口转发 (Ubunto 20.04)

KVM 端口转发 (Ubunto 20.04)

我的计算机全新安装了Ubuntu 20.04.3 LTS.

我已经安装了kvm:

sudo apt -y install bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm

我使用 virt-manager 创建了一个新的虚拟机,名为“ubuntu20.04”。

 Id   Name          State
-----------------------------
 7    ubuntu20.04   running

工作正常。它具有默认的网络设置:

 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

我已编辑网络文件,使用以下命令为虚拟机设置固定 IP 地址:

> virsh net-edit default

<network>
  <name>default</name>
  <uuid>57bd2ef8-5ef9-405e-b631-efdd10a12ca8</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:7c:ae:96'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
      <host mac='52:54:00:21:02:54' name='ubuntu20.04' ip='192.168.122.101'/>
    </dhcp>
  </ip>
</network>

我可以毫无问题地使用 ssh 虚拟机:ssh 192.168.122.101

我的主机有公共IP 192.168.1.85。

ifconfig
enp3s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.85  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::9e0a:64e6:7542:cdc4  prefixlen 64  scopeid 0x20<link>
        ether 0c:4d:e9:d4:35:20  txqueuelen 1000  (Ethernet)
        RX packets 306030  bytes 451106893 (451.1 MB)
        RX errors 0  dropped 3303  overruns 0  frame 0
        TX packets 19624  bytes 2100797 (2.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 19  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1792  bytes 176567 (176.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1792  bytes 176567 (176.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:7c:ae:96  txqueuelen 1000  (Ethernet)
        RX packets 13458  bytes 837489 (837.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68353  bytes 428375346 (428.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe21:254  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:21:02:54  txqueuelen 1000  (Ethernet)
        RX packets 152  bytes 15868 (15.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 718  bytes 44209 (44.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

目标:我现在的目标是从本地网络中的其他计算机访问虚拟机的 ssh。我想通过将进入主机端口 2222 (192.168.1.85: 2222) 的所有流量重定向到虚拟机 (192.168.122.101:22) 来实现此目的。

我有(Ubuntu安装后默认):

net.ipv4.ip_forward = 1

我尝试按照几个网站上的说明进行操作,但没有成功。例如,我已经关注了本教程,因为它基于使用 UFW,默认情况下,UFW 在 ubuntu 中处于活动状态。基本上该方法基于:

  1. 添加一个钩子文件(qemu):

/etc/libvirt/hooks/qemu

包含以下内容:

v=$(/sbin/iptables -L FORWARD -n -v | /usr/bin/grep 192.168.122.0/24 | /usr/bin/wc -l)
# avoid duplicate as this hook get called for each VM
#[ $v -le 2 ] && 

if [ $v -le 2]
then
        /sbin/iptables -I FORWARD 1 -o virbr0 -m state -s 192.168.1.0/24 -d 192.168.122.0/24 --state N>
fi
  1. 并编辑文件

/etc/ufw/before.rules

添加(在顶部)以下内容:

:PREROUTING ACCEPT [0:0]
-A PREROUTING -d 192.168.1.85 -p tcp --dport 2222 -j DNAT --to-destination 192.168.122.101:22 -m comment --comment "VM2/OpenBSD SSH port forwarding"
COMMIT

最后,按照这些说明,我得到了以下 iptables:

sudo iptables-save -t nat
# Generated by iptables-save v1.8.4 on Sat Jan 29 16:20:08 2022
*nat
:PREROUTING ACCEPT [268:56889]
:INPUT ACCEPT [129:22767]
:OUTPUT ACCEPT [1213:187394]
:POSTROUTING ACCEPT [1213:187394]
:LIBVIRT_PRT - [0:0]
-A PREROUTING -d 192.168.1.85/32 -p tcp -m tcp --dport 2222 -m comment --comment "VM2/OpenBSD SSH port forwarding" -j DNAT --to-destination 192.168.122.101:22
-A PREROUTING -d 192.168.1.85/32 -p tcp -m tcp --dport 2222 -m comment --comment "VM2/OpenBSD SSH port forwarding" -j DNAT --to-destination 192.168.122.101:22
-A PREROUTING -d 192.168.1.85/32 -p tcp -m tcp --dport 2222 -m comment --comment "VM2/OpenBSD SSH port forwarding" -j DNAT --to-destination 192.168.122.101:22
-A PREROUTING -d 192.168.1.85/32 -p tcp -m tcp --dport 2222 -m comment --comment "VM2/OpenBSD SSH port forwarding" -j DNAT --to-destination 192.168.122.101:22
-A POSTROUTING -j LIBVIRT_PRT
-A LIBVIRT_PRT -s 192.168.122.0/24 -d 224.0.0.0/24 -j RETURN
-A LIBVIRT_PRT -s 192.168.122.0/24 -d 255.255.255.255/32 -j RETURN
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
-A LIBVIRT_PRT -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE
COMMIT

这似乎与教程中获得的内容兼容。但是,当我尝试使用转发的端口登录 VM-shh 时,出现错误,如下所示:

ssh 192.168.1.85 -p 2222
ssh: connect to host 192.168.1.85 port 2222: Connection refused

预先感谢您的任何帮助!

相关内容