KVM:端口转发至客户虚拟机

KVM:端口转发至客户虚拟机

我在 Debian wheezy 上使用 KVM。我想使用 ssh 命令连接到客户机ssh 1.2.3.4 -p 10122。我所做的是:

root@host$ iptables -t nat -A PREROUTING -p TCP --dport 10122 -j DNAT --to-destination 192.168.122.208:22

iptables-save的结果:

# Generated by iptables-save v1.4.14 on Sat Nov 16 01:03:28 2013
*nat
:PREROUTING ACCEPT [64:9345]
:INPUT ACCEPT [64:9345]
:OUTPUT ACCEPT [112:7287]
:POSTROUTING ACCEPT [112:7287]
-A PREROUTING -p tcp -m tcp --dport 10122 -j DNAT --to-destination 192.168.122.208:22
-A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535
-A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p udp -j MASQUERADE --to-ports 1024-65535
-A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE
COMMIT
# Completed on Sat Nov 16 01:03:28 2013
# Generated by iptables-save v1.4.14 on Sat Nov 16 01:03:28 2013
*mangle
:PREROUTING ACCEPT [16331:11525510]
:INPUT ACCEPT [16327:11525270]
:FORWARD ACCEPT [4:240]
:OUTPUT ACCEPT [15827:1491721]
:POSTROUTING ACCEPT [15827:1491721]
-A POSTROUTING -o virbr0 -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
COMMIT
# Completed on Sat Nov 16 01:03:28 2013
# Generated by iptables-save v1.4.14 on Sat Nov 16 01:03:28 2013
*filter
:INPUT ACCEPT [7865:6008003]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [7723:676522]
-A INPUT -i virbr0 -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -i virbr0 -p tcp -m tcp --dport 53 -j ACCEPT
-A INPUT -i virbr0 -p udp -m udp --dport 67 -j ACCEPT
-A INPUT -i virbr0 -p tcp -m tcp --dport 67 -j ACCEPT
-A FORWARD -d 192.168.122.0/24 -o virbr0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -s 192.168.122.0/24 -i virbr0 -j ACCEPT
-A FORWARD -i virbr0 -o virbr0 -j ACCEPT
-A FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
-A FORWARD -d 192.168.122.208/32 -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Sat Nov 16 01:03:28 2013

但它不起作用:

me@dauphine:~ $ ssh [email protected] -v
OpenSSH_6.2p2 Ubuntu-6ubuntu0.1, OpenSSL 1.0.1e 11 Feb 2013
debug1: Reading configuration data /home/me/.ssh/config
debug1: /home/me/.ssh/config line 1: Applying options for *
debug1: /home/me/.ssh/config line 61: Applying options for flora.vm01
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to flora.me.net [1.2.3.4] port 10122.
debug1: connect to address 1.2.3.4 port 10122: Connection refused
ssh: connect to host flora.me.net port 10122: Connection refused

答案1

在这里找到答案https://serverfault.com/a/170641/88934. 还有一条规则:

iptables -I FORWARD -m state -d 10.0.0.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT

默认的 KVM NAT 配置提供了类似的规则,但它省略了接受传入连接所必需的 NEW 状态。

相关内容